Reputation: 430
All I'm trying to do is get my DLL injected into some other programs (at process creation time) and get it to execute the DllMain function. Unfortunately, no matter what code I am trying, it never works.
For example, I have this code: http://pastebin.com/P4NzLb3X Basically it is just using SetWindowsHookEx to install a keyboard hook. Checking with Process Hacker however shows me that the DLL never actually gets injected into any process. :(
I searched already for the entire day for a solution. How can I solve this?
Upvotes: 0
Views: 3823
Reputation: 430
I solved the problem with the help of these two links:
http://www.gamedev.net/topic/568401-problem-with-loading-dll--setwindowshookex/
Global Keyhook on 64-Bit Windows
3 things had to be fixed:
extern "C" __declspec(dllexport) int
- this fixes DLL loads on 32 bit processesCALLBACK
attribute to function (extern "C" int CALLBACK meconnect(...)
) - this fixes crash that happens on 32 bit processes after the fix above.Add a message loop into the host process (the process that is calling the SetWindowsHookEx
function) like so:
MSG msg;
while(1) {
// Keep pumping...
PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE);
TranslateMessage(&msg);
DispatchMessage(&msg);
Sleep(10);
SHORT v = GetKeyState(VK_RETURN);
bool pressed = (v & 0x8000) != 0;
if(pressed)
break;
}
this fixes the freeze in 64 bit processes.
Upvotes: 2