GetProcAddress fails

GetProcAddress fails

本文关键字:fails GetProcAddress      更新时间:2023-10-16

我在动态库中有一个fucntion,看起来像:

namespace Dll {
    int MyDll::getQ() {
        srand(time(0));
        int q = rand();
        while (!isPrime(q) || q < 100) {
            q = rand();
        }
        return q;
    }
}

函数getq((.h文件:

#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的代码和平:

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 calledn");
            printf("%d", result);
        }
        freeLibrarySuccess = FreeLibrary(hinstance);
    }
    if (!hinstance)
        printf("Unable to call the dlln");
    if (!proccAddressSuccess)
        printf("Unable to call the functionn");
}

所以我尝试了几次修复,但是我总是"无法调用该功能"。代码连接到库,因此问题在功能附近。如果有人将我指向我的错误,我会很感激。

您缺少外部" C"。

如果您不这样做,则名称将被c 弄糊,而您只能使用getQ名称找到它们。此外,这样做并不可靠,因为名称杂交可能会改变。

另一个主题是:_stdcall vs _cdecl