HOOK - A HowTo for setting system wide hooks
Click this link for a Screen Shot.
Environment: MSVC++6.0 SP3, Win98/2000/NT4(SP5)
Code Description
Purpose:
This article features a HowTo to write, set and make use of system wide hooks.
Implementation:
To set system wide hooks, you need to reference a DLL in the SetWindowsHookEx()-command. The demonstrational workspace constists of two separate projects. One builds the DLL with its exported member functions SetHook() and RemoveHook().
Note, that we make use of a memory mapped file to save the instance-, hook- and window-handles which is wrapped in the struct SCGLOBALDATA.
The HookProc()-callback function is called from the system, when keycodes are recieved. We then pass the values in lParam, wParam (virtual keycode and keystroke-message information) via a WM_COPYDATA-message to a controlling (separate) executable that determines by its return value if the hook is to be forwarded through CallNextHookEx().
For additional information, the controlling procedure displays various information on the keystroke it has currently processed. Due to the system wide character of the hook, it even logs keys that are pressed in other applications or a DOS-Prompt.
Member functions:
The DLL exports BOOL SetHook(HWND hWnd) and BOOL RemoveHook() which are pretty self-explaining. You need to pass a valid HWND to SetHook which limits the functionality to window-applications. To bypass this, you can create a dialogbox-based application with an initially hidden window.
Example:
Download the precompiled executable to get an idea of what hooks are all about. The complete VC++6.0 workspace contains all necessary sourcecode to build the demo application for you to play around with.
Final word:
I hope, you find the supplied code useful and instructive.
Please feel free to send comments to VolkerBartheld@reetcom.de
Downloads
HookDemoExe.zip is a compiled executable of the demo application.
HookDemo.zip is a MSVC++ V6 workspace of the demo application.
Download demo project executable HookDemoExe.zip - 19.990 kBytes
Download demo project workspace HookDemo.zip - 18.120 kBytes

Comments
More concessions with herveleger, more curb napping!
Posted by tradecomejug on 04/29/2013 09:17pmtittle of san quentin quailalter egobeatitudekeepunconditionalvend down the river
Replypsapi is part of the platform SDK??
Posted by Legacy on 09/19/2003 12:00amOriginally posted by: rich
Hi!
I'm sorry to say this~ you said "psapi is part of the platform SDK", but It's not easy to find.
please, tell me where It is.
Replywhat is system wide hook?
Posted by Legacy on 08/17/2003 12:00amOriginally posted by: ashiq
what is the purpose of using system wide hook?
ReplyIt's GREAT! Thanks a lot!
Posted by Legacy on 05/03/2003 12:00amOriginally posted by: Jasion Wa
It's GREAT! Thanks a lot!
ReplyHook message ....
Posted by Legacy on 04/06/2003 12:00amOriginally posted by: anhThuan
ReplyHelp ! How can i hook up the printer
Posted by Legacy on 11/02/2002 12:00amOriginally posted by: Ashok
Hai i need a help
ReplyBoth WIndows 9x/ME and NT /2000
I need to hook the printer port i.e. if any application tries to print any job i should catch it and with validation job should enter for printing
Kindly help me
Thanks
Ashok
Help! How can I hook Menu Clicks?
Posted by Legacy on 09/04/2002 12:00amOriginally posted by: Guru Prasad
Replylist api
Posted by Legacy on 03/08/2002 12:00amOriginally posted by: prabhu
hi,
how can i list the details of the api called by an application. the details including parameters and return values.
Replyhow can I monitor if a app was started without VXD ?
Posted by Legacy on 02/06/2002 12:00amOriginally posted by: Pascut Cristian
I need to log to a file (eventually to terminate it) every application that is started on a WIN9x, WINME system. CAn you help me here ? It can be done with a hook on WH_SHELL ? If yes, how ?
Replyncode of Call Back doesn't receive values
Posted by Legacy on 01/17/2002 12:00amOriginally posted by: Aravindhan
ReplyLoading, Please Wait ...