2

我有一个库(C++),它有一些 API 函数。其中之一被声明为 __cdecl,但从 __stdcall 获得一个函数指针。就像是:

typedef  int (__stdcall *Func)(unsigned char* buffer);
//...
int ApiFunc(Func funcPtr); //This is __cdecl since it is an 'extern "C"' library and the calling convention is not specified

然后 - 我有一个使用此库的 C++ 可执行项目,但不调用上述 API 或使用该Func类型。

Func更改to的调用约定后__stdcall,出现以下编译错误:

错误 C2995: 'std::pointer_to_unary_function<_Arg,_Result,_Result(__cdecl *)(_Arg)> std::ptr_fun(_Result (__cdecl *)(_Arg))' : 函数模板已定义 c:\program files\微软视觉工作室 8\vc\include\functional

知道会是什么吗?

提前致谢!!

4

2 回答 2

2

它们是兼​​容的,至少在 Windows 中(并且在 Linux 中根本没有 __stdcall ......)问题是错误地,库重新定义了 __stdcall 以与 Linux 兼容,如下所示:

#ifndef __MYLIB_WIN32
//Just an empty define for Linux compilation
#define __stdcall
#endif

exe项目包含这个定义,__MYLIB_WIN32并没有在其中定义,而只是在库中。将上述定义更改为:

#ifndef WIN32
//Just an empty define for Linux compilation
#define __stdcall
#endif

一切正常。

谢谢你们。

于 2010-06-30T08:38:41.443 回答
2

呃..他们不兼容。您必须在调用双方指定相同的调用约定。否则尝试调用将炸毁机器堆栈。

于 2010-06-29T13:50:42.690 回答