Reputation:
If I try to use the LoadLibrary to load User32.dll the function returns error 14007 (ERROR_SXS_KEY_NOT_FOUND). This is the code I use:
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
So the hUserModule is not NULL, but also not valid. Why is this?
EDIT: GetModuleHandle also doesn't work
Upvotes: 1
Views: 1626
Reputation: 409356
On a 64-bit system addresses are 64 bits wide. The DWORD
type is "A 32-bit unsigned integer" (quoted from this MSDN type reference).
That means you truncate the address you receive from GetProcAddress
making it invalid.
The solution is to use a proper pointer type, casting to that type instead of to DWORD
. Perhaps something like
__MessageBoxA MsgBox = (__MessageBoxA) GetProcAddress(hUserModule, "MessageBoxA");
(Assuming __MessageBoxA
is a proper pointer.)
Upvotes: 1