Creating a C function pointer function using a stack-based call method in C ++

I want to call a pure C style function from a dll in my C ++ program. I tried using my function pointer using reinterpret_castto __cdecl, and yet the calling convention _stdcallseems to be preserved. I am new to Windows C ++ programming.

Change Code from Comment

reinterpret_cast< Error ( __cdecl*)(int,int)> (GetProcAddress(Mydll::GetInstance()->ReturnDLLInstance(), "add"))(1,10) 

- my challenge. The actual function syntax seems to have been declared as

Error __cdecl add(int,int);

The debugger gives me an error Runtime Check Error # 0 . I work in windows-c ++

+3
source share
4 answers

extern "C" ...

--- c_code.h ---

void func(int arg);
void (*func_ptr)(int arg);

--- cpp_code.cpp ---

extern "C" void func(int arg);
extern "C" void (*func_ptr)(int arg);

int main()
{
    func(20);
    *func_ptr(20);
}
+6

.

- . (ABI), , . , , , dll . WIN32 API __stdcall, C __cdecl.

Another problem is name manipulation. Since function arguments are part of the function signature in C ++ (to ensure function overloading), this information is included in the character table of your object code. Usually it will be a whole bunch of extra strange characters. C does not require name processing because it does not allow function overloading.

Sometimes in C ++, you want to call C functions (i.e., C function symbols compiled by C, not the C ++ compiler). In this case, you need to define the function in the block extern "C" {}.

Hope this helps you

+3
source

All Articles