2017-06-29 180 views
1

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

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

+0

一般来说,你应该检查错误代码(由'GetLastError'返回)*仅*如果一个函数失败。否则,你可能会得到误报。 –

+0

至于你的崩溃,你是否检查'GetProcAddress'没有失败? –

+0

最后,与您的问题完全无关,但没有以双下划线开头的自己的符号。这些由所有范围内的“实现”(编译器和标准库)保留。 [见这个老问题及其答案]以获取更多信息(http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier)。 –

回答

1

在64位系统上的地址是64个位宽。所述DWORD类型是“的32位无符号整数,”(来自this MSDN type reference引述)。

那意味着你truncate你从GetProcAddress收到的地址使其无效。

的解决方案是使用一个适当的指针类型,浇铸到该类型,而不是到DWORD。也许类似

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

(假设__MessageBoxA正确的指针。)