使C函数指针在C++中使用基于C风格的堆栈调用机制

ash*_*ash 3 c c++ pointers function visual-c++

我想在我的C++程序中从一个dll调用一个纯C风格的函数.我尝试使用reinterpret_castto来转换我的函数指针,__cdecl并且仍然保持调用约定_stdcall.我是Windows C++编程的新手.

从评论中编辑代码

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

是我的电话.实际的函数语法似乎已声明为

Error __cdecl add(int,int);
Run Code Online (Sandbox Code Playgroud)

调试器抛出错误运行时检查失败#0.我在Windows-C++工作

mat*_*t88 8

我相信你的问题的解决方案是'extern'C"{...'

请参阅http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.3


小智 6

通常你需要使用extern"C"来...

--- c_code.h ---

void func(int arg);
void (*func_ptr)(int arg);
Run Code Online (Sandbox Code Playgroud)

--- cpp_code.cpp ---

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

int main()
{
    func(20);
    *func_ptr(20);
}
Run Code Online (Sandbox Code Playgroud)