2017-06-29 183 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正確的指針。)