gpt4 book ai didi

c++ WINAPI通过GetProcAddress调用导出的函数

转载 作者:搜寻专家 更新时间:2023-10-31 01:34:32 25 4
gpt4 key购买 nike

我试图通过 GetProcAddress 获取 DLL 的函数指针来调用 DLL 的导出函数,但在调用该函数时应用程序崩溃了。

我使用 dependencywalker 查看导出函数的名称是否正确。 GetProcAddress 返回的地址不为空。我几乎可以肯定它与调用约定有关,我同时使用了 __cdecl__stdcall 但没有成功。但是我确实想使用 GetProcAdress 而不是 __declspec(dllimport)

DLL #1(调用方)

  • 将 DLL#2.lib 链接到此 DLL

    typedef void(__stdcall *ptr_init)(DWORD size);

    ctx.hModule = LoadLibraryA("someDLL.dll");
    ptr_init init = (ptr_init)GetProcAddress(ctx.hModule, "init");

    if (init == NULL) {
    out = out + " | init function is null";
    } else {
    out = out + " | init function found!";//It is found
    }

    DWORD test = 10;
    (*init)(test);//<-- makes application crash

DLL #2(包含导出函数的 DLL)

//header.h
extern "C" __declspec(dllexport) void init(DWORD size);

//source.cpp
extern "C" __declspec(dllexport) void init(DWORD size) {
//code
}

最佳答案

你应该保持一致。如果您检索指针作为指向 stdcall 函数的指针 - 它必须在实现中声明为 stdcall:

//header.h
extern "C" __declspec(dllexport) void __stdcall init(DWORD size);

关于c++ WINAPI通过GetProcAddress调用导出的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39330898/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com