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:17pm

    tittle of san quentin quailalter egobeatitudekeepunconditionalvend down the river

    Reply
  • psapi is part of the platform SDK??

    Posted by Legacy on 09/19/2003 12:00am

    Originally 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.

    Reply
  • what is system wide hook?

    Posted by Legacy on 08/17/2003 12:00am

    Originally posted by: ashiq

    what is the purpose of using system wide hook?

    Reply
  • It's GREAT! Thanks a lot!

    Posted by Legacy on 05/03/2003 12:00am

    Originally posted by: Jasion Wa

    It's GREAT! Thanks a lot!

    Reply
  • Hook message ....

    Posted by Legacy on 04/06/2003 12:00am

    Originally posted by: anhThuan

    I want to know how to hook message from other computer....
    
    For example :
    somebody use "net.exe" to send me a short message, so i want to hook this message ....
    goto run command : net send * text here
    teach me...
    thanks

    Reply
  • Help ! How can i hook up the printer

    Posted by Legacy on 11/02/2002 12:00am

    Originally posted by: Ashok

    Hai i need a help
    Both 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

    Reply
  • Help! How can I hook Menu Clicks?

    Posted by Legacy on 09/04/2002 12:00am

    Originally posted by: Guru Prasad

    Hi, 
    
    I want to hook a Menu Click when the user clicks on a particular Menu Item. The problem with a WM_MENUSELECT hook is that it traps all events when the user "browses" over a particular menu item (the moment when the text of the Status Bar in a standard Windows application changes to reflect the menu item in focus. Like, you take the cursor over File > New and the Status bar says "Opens a new Document").
    I want to trap the events when the user clicks or otherwises "chooses" a menu item (mapped using ON_COMMAND). In the hook I want the option to discard the Menu Item Click if I want to.
    I tried using the WM_CALLWNDPROC hook which traps the WM_COMMAND message but the documentation in MSDN (Win32 Hooks by Kyle Marsh) says that we shouldn't (and cannot) modify the messages using this hook. Plus this hook is also a drain on the system as it traps most Windows messages.
    As for the details - I am using this hook in an ActiveX control and this hook will be used only on a particular thread (So I don't need a system hook) - if that helps.
    Can someone help me tackle this Menu Click beast?
    Any help will be appreciated.
    Best Regards and thanx in advance,
    Guru.

    Reply
  • list api

    Posted by Legacy on 03/08/2002 12:00am

    Originally posted by: prabhu

    hi,

    how can i list the details of the api called by an application. the details including parameters and return values.

    Reply
  • how can I monitor if a app was started without VXD ?

    Posted by Legacy on 02/06/2002 12:00am

    Originally 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 ?

    Reply
  • ncode of Call Back doesn't receive values

    Posted by Legacy on 01/17/2002 12:00am

    Originally posted by: Aravindhan

    I am using C++ Builder to create system wide Keyboard hooks
    
    using the SetWindowsHookEx() command but the ncode parameter of the CALLBACK function does not receive any values.It's value is always equal to 0.

    Please help me.

    Aravindhan

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Savvy enterprises are discovering that the cloud holds the power to transform IT processes and support business objectives. IT departments can use the cloud to redefine the continuum of development and operations—a process that is becoming known as DevOps. Download the Executive Brief DevOps: Why IT Operations Managers Should Care About the Cloud—prepared by Frost & Sullivan and sponsored by IBM—to learn how IBM SmartCloud Application services provide a robust platform that streamlines …

  • Cisco and Intel have harnessed flash memory technology and truly innovative system software to blast through the boundaries of today's I/O-bound server/storage architectures. See how they are bringing real-time responsiveness to data-intensive applications—for unmatched business advantage. Sponsored by Cisco and Intel® Partnering in Innovation

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds