Hey
I am trying to use SetWindowHookEx() API to inject my code into another process, but I am kinda lost with the hook procedure that should be placed in the .dll file
is there an explaination about how to build a hook procedure?
thanks in advance!
Printable View
Hey
I am trying to use SetWindowHookEx() API to inject my code into another process, but I am kinda lost with the hook procedure that should be placed in the .dll file
is there an explaination about how to build a hook procedure?
thanks in advance!
There are several good examples on SetWindowsHookEx on both CodeGuru and CodeProject (and in MSDN as well)... search
Can you be more specific about the hook procedure problem you're having?
Also, read about "systemwide hooks" in MSDN: http://msdn.microsoft.com/library/de...dn_hooks32.asp
this is my callback function
Code:LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode<0)
return CallNextHookEx(0,nCode,wParam,lParam);
if (wParam == WM_RBUTTONDOWN)
{
MessageBox(GetForegroundWindow(), "Yes", "Works", MB_OK);
}
return CallNextHookEx(0,nCode,wParam,lParam);
}
and in onbutton function I set:
as you can see I am trying to do a mouse hook, this try is to get any right click, but later when I get it to work, I want it to be for a defined application that when it gets right click the the popup pops upCode:HOOKPROC wlm;
HHOOK check;
HINSTANCE WLMDLL;
WLMDLL = LoadLibrary((LPCTSTR) "MyHook.dll");
wlm = (HOOKPROC)GetProcAddress(WLMDLL, "MouseProc");
check = SetWindowsHookEx(WH_MOUSE,wlm,WLMDLL,0);
can you see what I am doing wrong in my code?
thanks in advance!
If you really want a global hook (specifying 0 as last arg to SetWindowHookEx), the hook proc must reside in a DLL.
I'm not sure how you have setup everything... Have you exported the MouseProc function? (ie does GetProcAddress work?) Check every return values when installing the hook.
I wrote a simple test in C with VC2003... see attachment.
thanks alot, I am taking a look at your code..
Hi All,
If we want a global hook (specifying the thread id of the process whose keyboard and mouse inout we want to block as last arg to SetWindowHookEx), then where the hookproc must reside, will it be a part of dll or will it be a part of exe .
please reply,
Thanks
Shailesh
It depends on the hook type. Some hooks can actually reside in an EXE, but most of them require a DLL.
You can also have a thread specific hook in your own app, ie the thread id must belong to the process that called SetWindowHookEx.
By the way, never could understand those plays with LoadLibrary and GetProcAddress, though this point is most common in the samples I saw, here and there. Personally me typically place the SetWindowsHookEx call into dll and link to the latter by means of import library with no problem. :)