我在动态库中有一个函数,它看起来像:
namespace Dll {
int MyDll::getQ() {
srand(time(0));
int q = rand();
while (!isPrime(q) || q < 100) {
q = rand();
}
return q;
}
}.h文件中的函数getQ():
#ifdef _EXPORT
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
namespace Dll
{
class MyDll
{
public:
static DLL_EXPORT int __stdcall getQ();
}
}最后,来自另一个consoleApp的LoadLibrary代码的和平:
typedef int(__stdcall *CUSTOM_FUNCTION)();
int main()
{
HINSTANCE hinstance;
CUSTOM_FUNCTION proccAddress;
BOOL freeLibrarySuccess;
BOOL proccAddressSuccess = FALSE;
int result;
hinstance = LoadLibrary(TEXT("Dll.dll"));
if (hinstance != NULL)
{
proccAddress = (CUSTOM_FUNCTION)GetProcAddress(hinstance, "getQ");
if (proccAddress != NULL)
{
proccAddressSuccess = TRUE;
result = (*proccAddress)();
printf("function called\n");
printf("%d", result);
}
freeLibrarySuccess = FreeLibrary(hinstance);
}
if (!hinstance)
printf("Unable to call the dll\n");
if (!proccAddressSuccess)
printf("Unable to call the function\n");
}所以我试着修复这个问题几次,但我总是得到“无法调用函数”。代码连接到库,所以问题出在函数附近的某个地方。如果有人能指出我的错误,我将不胜感激。
发布于 2018-04-01 23:17:54
您缺少一个Extern "C"。
如果您不这样做,名称将被名称损坏,并且您无法仅使用getQ c++找到它们。此外,这样做是不可靠的,因为名称损坏可能会发生变化。
另一个主题是:_stdcall vs _cdecl
https://stackoverflow.com/questions/49599467
复制相似问题