hi I've got the following problem, and I cannot figure out what is going on.
DLL code mylib.cpp (mylib.dll):
#include <Windows.h>
#include <tchar.h>
__declspec(dllexport) LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam) {
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserverd){
// Perform actions based on the reason for calling.
switch( fdwReason )
{
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
MessageBox(NULL,
_T("DLL Loaded"),
_T("DLL Loaded"),
NULL);
break;
case DLL_THREAD_ATTACH:
// Do thread-specific initialization.
MessageBox(NULL,
_T("DLL Unloaded"),
_T("DLL Unloaded"),
NULL);
break;
}
return TRUE;
}
Program code my_prog.cpp:
#include <Windows.h>
#include <tchar.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
FARPROC pHookProc;
static HINSTANCE hInstDLL;
hInstDLL = LoadLibrary(_T("mylib.dll"));
pHookProc = GetProcAddress(hInstDLL, "HookProc");
if (!pHookProc) {
MessageBox(NULL,
_T("GetProcAddress failed"),
_T("GetProc开发者_Go百科Address failed"),
NULL);
}
return 0;
}
Both files compile without any errors. Whenever I run my_prog.exe it would give a message "DLL Loaded", then right away It would give message "DLL unloaded" and, as a result, GetProcAddress() fails. Could someone shine some light on it for me please. Why does it unload the DLL instantaneously?
Thank you all in advance.
EDITED:
I've replaced DLL_THREAD_ATTACH by DLL_PROCESS_DETACH as c-smile suggested. I check and function exports as: long __stdcall HookProc(int,unsigned int,long) (1)(0x00001000). GetProcAddress() still fails. I get "DLL Loaded", GetProcAddress() failed, "DLL Unloaded"
- Replace
DLL_THREAD_ATTACH
byDLL_PROCESS_DETACH
- Make sure that your function is exported exactly as "HookProc".
- If no use .def file to define export name of the function.
Two things:
- Don't assume that
DLL_THREAD_ATTACH
means things are going wrong. It's something that should happen when something links to your DLL as c-smile said. Since this is a C++ compilation unit, your export will have the mangled name
?HookProc@@YGJHIJ@Z
- that's whyGetProcAddress(hInstDLL, "HookProc")
fails - it's not the right name.Use
extern "C" __declspec(dllexport) LRESULT CALLBACK HookProc(int nCode, WPARAM wParam, LPARAM lParam);
And you'll get a more manageable name of
_HookProc@12
, soGetProcAddress(hInstDLL, "_HookProc@12")
should work.
If you want an even nicer name, I think you'll need to use a DEF file, From http://msdn.microsoft.com/en-us/library/3y1sfaz2.aspx:
dllexport of a C++ function will expose the function with C++ name mangling. If C++ name mangling is not desired, either use a .def file (EXPORTS keyword) or declare the function as extern "C".
A .def file like the following should do the trick (note: the EXPORTS
keyword seems to be case sensitive):
EXPORTS
HookProc=_HookProc@12
Pass the .def file to the linker using the /def:whatever.def
option.
Instead of MessageBox
, can you try printf
? MessageBox
is modal and it is probably messing things up.
In the DLL code, when using DLL_PROCESS_DETACH:
"The lpReserved parameter indicates whether the DLL is being unloaded as a result of a FreeLibrary call, a failure to load, or process termination."
So I would check that parameter, it might help narrow down the problem.
I would also be checking the return value of LoadLibrary to make sure the actual Load succeeded. If the LoadLibrary is failing you can try using 'GetLastError()' API for more information.
Also you are not doing a 'FreeLibrary(hInst);' after your LoadLibrary.
精彩评论