为什么 GetProcAddress 在实现时会抛出异常?

Min*_*rve -3 c++ winapi

这是我的头文件包含的内容:

extern HMODULE Ws_32;
extern HMODULE User32;
extern HMODULE Kernel32;
extern HMODULE Advapi32;

typedef int (WINAPI *fnMessageBox)(
    _In_opt_ HWND    hWnd,
    _In_opt_ LPCTSTR lpText,
    _In_opt_ LPCTSTR lpCaption,
    _In_     UINT    uType
);

struct API
{
    HMODULE User32;
    fnMessageBox _MessageBox;
};
Run Code Online (Sandbox Code Playgroud)

这就是我的 CPP 文件包含的内容:

API Api;

VOID Tools::LoadApis(API Api)
 {
    if ((Api.User32 = GetModuleHandleW(L"USER32.DLL")) == ERROR)
    {
        Api.User32 = LoadLibraryW(L"USER32.DLL");
    }

    if (Api.User32)
    {
        Api._MessageBox = fnMessageBox(GetProcAddress(Api.User32, "MessageBox"));
    }
}

int main()
{
    Tools::LoadApis(Api); // not a part of the example
    Api._MessageBox(0, 0, 0, 0);
}
Run Code Online (Sandbox Code Playgroud)

在线,我打电话的地方

Api._MessageBox(0, 0, 0, 0);
Run Code Online (Sandbox Code Playgroud)

定义后

Api._MessageBox = fnMessageBox(GetProcAddress(Api.User32, "MessageBox"));
Run Code Online (Sandbox Code Playgroud)

我得到一个内存异常,如下:

在rat.exe 中的0x00000000 处抛出异常:0xC0000005:访问冲突执行位置0x00000000。

use*_*670 5

GetProcAddress从不抛出 /C++/ 异常。真正的问题是你永远不会检查存储在Api._MessageBox.

另请注意,MessageBox在 WinAPI 中是一个宏扩展到user32MessageBoxWMessageBoxA从 user32 导出。所以GetProcAddress(Api.User32, "MessageBox")返回null是有意义的。

在第二个注释中,您的代码中有一个错误:您初始化了一个Api函数参数,但随后使用了Api全局变量(仍未初始化)。您应该摆脱全局变量并通过API引用传递:

VOID Tools::LoadApis(API & Api)
{
   //...
}

int main()
{
    API Api{};
    Tools::LoadApis(Api); // not a part of the example
    if(Api._MessageBox)
    {
        Api._MessageBox(0, 0, 0, 0);
    }
}
Run Code Online (Sandbox Code Playgroud)