HOOK - A HowTo for setting system wide hooks | CodeGuru

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
May 29, 2001
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.