代码之家  ›  专栏  ›  技术社区  ›  user7877793

LoadLibrary('user32.dll')返回14007

  •  1
  • user7877793  · 技术社区  · 7 年前

    SetLastError(0); //To make sure there are no previous errors.
    HINSTANCE hUserModule = LoadLibrary(L"User32.dll");
    if (hUserModule == NULL) { //Checking if hUserModule is NULL
        MessageBoxA(NULL, "Fatal error", "", 16);
        ExitProcess(0);
    } //hUserModule is not NULL
    printf("%d\n", GetLastError()); //14007
    DWORD paMessageBoxA = (DWORD)GetProcAddress(hUserModule, "MessageBoxA");
    __MessageBoxA MsgBox = (__MessageBoxA)paMessageBoxA; //typedef int(__stdcall *__MessageBoxA)(HWND, LPCSTR, LPCSTR, UINT);
    MsgBox(NULL, "", "", 64); //Application crahses
    

    因此,hUserModule不是NULL,但也是无效的。为什么会这样?

    编辑:GetModuleHandle也不工作

    1 回复  |  直到 7 年前
        1
  •  1
  •   Some programmer dude    7 年前

    DWORD 类型为“32位无符号整数”(引用自 this MSDN type reference

    这意味着你 截断 GetProcAddress 使其无效。

    解决方案是使用适当的指针类型,将其转换为该类型,而不是转换为 .也许是这样

    __MessageBoxA MsgBox = (__MessageBoxA) GetProcAddress(hUserModule, "MessageBoxA");
    

    (假设 __MessageBoxA 正确的指针。)