加载库('user32.dll') returns 14007

LoadLibrary('user32.dll') returns 14007

如果我尝试使用 LoadLibrary 加载 User32.dll 函数 returns 错误 14007 (ERROR_SXS_KEY_NOT_FOUND)。这是我使用的代码:

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 也不起作用

在 64 位系统上,地址是 64 位宽。 DWORD 类型是 "A 32-bit unsigned integer"(引用自 this MSDN type reference)。

这意味着您截断您从GetProcAddress收到的地址使其无效。

解决方案是使用正确的指针类型,转换为该类型而不是 DWORD。也许像

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

(假设 __MessageBoxA 一个正确的指针。)