![]() |
Making a C function pointer work with C-style stack based calling mechanics in C++
I want to call a pure C style function from a dll in my C++ program. I tried casting my function pointer using reinterpret_cast to __cdecl and still the calling convention of _stdcall seems to be preserved. I am new to Windows C++ programming.
This is my C++ call Code:
reinterpret_cast< Error ( __cdecl*)(int,int)> (GetProcAddress(Mydll::GetInstance()->ReturnDLLInstance(), "add"))(1,10)Code:
Error __cdecl add(int,int); |
Re: Making a C function pointer work with C-style stack based calling mechanics in C+
Usually to call C from C++ you just need to extern "C" the prototype:
Code:
extern "C" |
Re: Making a C function pointer work with C-style stack based calling mechanics in C+
Thanks! But what if I cannot work with the C code.I just have it as a dll. In that case is there a work around??
|
Re: Making a C function pointer work with C-style stack based calling mechanics in C+
extern "C" is C++ code, not C code. This is what you put in the C++ code to make it call the function in the DLL. You still need a prototype, and you extern "C" it to stop the compiler mangling the names, then the linker resolves the call.
|
| All times are GMT +5.5. The time now is 22:25. |