Crestline
asked on
SetWindowsHookEx - Why not working
This DLL gets injected in to a simple form application using a 'starter app' which uses CreateProccessEXA from Madshi's library (www.madshi.net). For testing purposes I've set it up to hook the keyboard up and down arrows. Because of this, I'm not sure if I'm setting the hook correctly or not.
Can someone explain to me why this does not work? I thought I had read somewhere that maybe the hook has to be in a loop or something... I don't, the only example I find are very basic and they only seem to be in EXEs, not DLLs. Does that make a difference?
I know that the DLL is getting injected and started because I get the "Attaching" messagebox when my starter app is used and "Detaching" messagebox when I close my form. So the DLL is running in the simple form app Ultimately what I would like to do is hook messages sent to controls with in the form.
Anyways, here's the code. If more explanation is needed, just let me know.
/////////////// THE DLL ///////////////////
#include "stdafx.h"
#include "windows.h"
#include <stdlib.h>
#include "NSR_SM.h"
HHOOK hH;
LRESULT CALLBACK KeyboardProc(int hCode,WPARAM ww,LPARAM ll)
{
if (hCode < 0)
return CallNextHookEx(hH,hCode,ww ,ll);
else
{
switch(hCode)
{
case VK_UP:
// log here info , ww has its HWND
MessageBox(0,"Up", "Hook",0);
break;
case VK_DOWN:
// same
MessageBox(0,"Down", "Hook",0);
break;
}
return CallNextHookEx(hH,hCode,ww ,ll);
}
}
BOOL WINAPI DllMain( HANDLE hModule,
DWORD reason,
LPVOID lpReserved)
{
if (reason == DLL_PROCESS_ATTACH)
{
MessageBox(0,"Attaching", "Debug", 0);
HINSTANCE hDll = GetModuleHandle(NULL);
hH = SetWindowsHookEx(WH_KEYBOA RD,Keyboar dProc,hDll ,NULL);
}
else if (reason == DLL_PROCESS_DETACH)
{
MessageBox(0,"Detaching", "Debug", 0);
UnhookWindowsHookEx(hH);
}
return TRUE;
}
//////////// THE STARTER APP /////////////
#include "stdafx.h"
#include "windows.h"
#include "madCHook.h"
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
InitializeMadCHook();
CreateProcessExA((LPCSTR)" C:\\NSR_Fo rm.exe",
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi,
(LPCSTR)"C:\\NSR_SM.dll");
FinalizeMadCHook();
return 0;
}
Can someone explain to me why this does not work? I thought I had read somewhere that maybe the hook has to be in a loop or something... I don't, the only example I find are very basic and they only seem to be in EXEs, not DLLs. Does that make a difference?
I know that the DLL is getting injected and started because I get the "Attaching" messagebox when my starter app is used and "Detaching" messagebox when I close my form. So the DLL is running in the simple form app Ultimately what I would like to do is hook messages sent to controls with in the form.
Anyways, here's the code. If more explanation is needed, just let me know.
/////////////// THE DLL ///////////////////
#include "stdafx.h"
#include "windows.h"
#include <stdlib.h>
#include "NSR_SM.h"
HHOOK hH;
LRESULT CALLBACK KeyboardProc(int hCode,WPARAM ww,LPARAM ll)
{
if (hCode < 0)
return CallNextHookEx(hH,hCode,ww
else
{
switch(hCode)
{
case VK_UP:
// log here info , ww has its HWND
MessageBox(0,"Up", "Hook",0);
break;
case VK_DOWN:
// same
MessageBox(0,"Down", "Hook",0);
break;
}
return CallNextHookEx(hH,hCode,ww
}
}
BOOL WINAPI DllMain( HANDLE hModule,
DWORD reason,
LPVOID lpReserved)
{
if (reason == DLL_PROCESS_ATTACH)
{
MessageBox(0,"Attaching", "Debug", 0);
HINSTANCE hDll = GetModuleHandle(NULL);
hH = SetWindowsHookEx(WH_KEYBOA
}
else if (reason == DLL_PROCESS_DETACH)
{
MessageBox(0,"Detaching", "Debug", 0);
UnhookWindowsHookEx(hH);
}
return TRUE;
}
//////////// THE STARTER APP /////////////
#include "stdafx.h"
#include "windows.h"
#include "madCHook.h"
int _tmain(int argc, _TCHAR* argv[])
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
InitializeMadCHook();
CreateProcessExA((LPCSTR)"
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi,
(LPCSTR)"C:\\NSR_SM.dll");
FinalizeMadCHook();
return 0;
}
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
Actually, you should provide the correct signature for your 'DllMain()', which is
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
);
BOOL WINAPI DllMain(
HINSTANCE hinstDLL, // handle to DLL module
DWORD fdwReason, // reason for calling function
LPVOID lpvReserved // reserved
);
switch(hCode)
should be
switch(wParam)
should be
switch(wParam)
Or, actually
switch(ww)
*duck* :o)
switch(ww)
*duck* :o)
ASKER
hH = SetWindowsHookEx(WH_KEYBOA RD,Keyboar dProc,hins tDLL,NULL) ;
I only want this hook to occur in the form that this DLL is attached to, is this the case or do I need the dwThreadID as well? Or is the dwThreadID the thread of the DLL and not the Form?
Thanks.
I only want this hook to occur in the form that this DLL is attached to, is this the case or do I need the dwThreadID as well? Or is the dwThreadID the thread of the DLL and not the Form?
Thanks.
>>I only want this hook to occur in the form that this DLL is attached to
Oh, in this case, use
hH = SetWindowsHookEx(WH_KEYBOA RD,Keyboar dProc,NULL ,0);
Oh, in this case, use
hH = SetWindowsHookEx(WH_KEYBOA
ASKER
Not sure what I'm doing wrong but when I use the old version the hook works, but it starts up in other windows. Like when I bring another window to the forground and hit a key on the keyboard, my "Attaching" meesagebox comes up because my DLL is now in that process.
hH = SetWindowsHookEx(WH_KEYBOA RD,Keyboar dProc,hins tDLL,NULL) ;
When I use the new version, nothing seems to happen, nothing is triggered by the key strokes.
hH = SetWindowsHookEx(WH_KEYBOA RD,Keyboar dProc,NULL ,0);
I'm going to start a new Question as I didn't specifically state that I wanted this to only run in my Form's process and not system wide. As I said, in the end, I want to monitor messages sent to controls in this form, specifically messages sent to a listbox.
hH = SetWindowsHookEx(WH_KEYBOA
When I use the new version, nothing seems to happen, nothing is triggered by the key strokes.
hH = SetWindowsHookEx(WH_KEYBOA
I'm going to start a new Question as I didn't specifically state that I wanted this to only run in my Form's process and not system wide. As I said, in the end, I want to monitor messages sent to controls in this form, specifically messages sent to a listbox.
ASKER
New question can be found here:
https://www.experts-exchange.com/questions/21335362/SetWindowsHookEx-Hook-specific-process-not-system-wide.html
https://www.experts-exchange.com/questions/21335362/SetWindowsHookEx-Hook-specific-process-not-system-wide.html
ASKER
This is going to sound elementary, but hH = SetWindowsHookEx(WH_KEYBOA
Should simply casting as HINSTANCE work?
hH = SetWindowsHookEx(WH_KEYBOA