Article ID: 168795 - Last Review: November 23, 2006 - Revision: 3.3 How To Hook Into a Window's Messages Using AddressOfThis article was previously published under Q168795 On This PageSUMMARY
Hooking into a Window (sometimes called sub-classing) is a technique that
enables interception of the messages that are being sent to that Window.
Microsoft Visual Basic allows sub-classing through the use of the
AddressOf operator. This article's purpose is to demonstrate a hooked message stream.
NOTE: It is beyond the scope of this article to describe any particular application of this technique. MORE INFORMATION
Microsoft Windows controls applications by sending messages to the windows
that are created by each application. These messages alert the targeted
window when it's time to redraw, when a mouse button is pressed, and all of
the other information a window needs to know in order to act appropriately.
Thus, a minimal Windows application consists of a function for which these
messages are processed (called WindowProc). This function is registered
into the system when the window is created so the system knows where to
send messages.
The following application consists of a simple form with two command buttons. The code is designed to intercept Windows messages being sent to the form and to print the values of those messages in the Immediate window. CAUTION: ANY USE OF THE SAMPLE CODE PROVIDED IN THIS ARTICLE IS AT YOUR OWN RISK. Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or Microsoft Advisory Services. For more information, visit these Microsoft Web sites: Microsoft Certified Partners - https://partner.microsoft.com/global/30000104 (https://partner.microsoft.com/global/30000104) Microsoft Advisory Services - http://support.microsoft.com/gp/advisoryservice (http://support.microsoft.com/gp/advisoryservice) For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:http://support.microsoft.com/default.aspx?scid=fh;EN-US;CNTACTMS (http://support.microsoft.com/default.aspx?scid=fh;en-us;cntactms) WARNING: Failure to unhook a window before its imminent destruction will result in application errors, Invalid Page Faults, and data loss. This is due the fact that the new WindowProc function being pointed to no longer exists, but the window has not been notified of the change. Always unhook the sub-classed window upon unloading the sub-classed form or exiting the application. This is especially important while debugging an application that uses this technique within the Microsoft Visual Basic Development Environment. Pressing the End button or selecting End from the Run menu without unhooking will cause an Invalid Page Fault and close Microsoft Visual Basic. Step-by-Step Example
The second procedure (UnHook) simply reverses what you have done and puts the address of the original window procedure back. Here WindowProc is the function that you are routing the window messages to when you "Hook" the form's WindowProc function. Note that you make use of the CallWindowProc function; using the lpPrevWndProc variable to send any unprocessed messages to the original handler. Hence, you are allowing a chain of window procedures to process all messages. REFERENCES
Microsoft Visual Basic Books Online
(http://msdn.microsoft.com/vbasic/)
"Dan Appleman's Visual Basic Programmer's Guide to the Win32 API" by Dan Appleman Platform Software Development Kit (SDK) Online Help (http://www.microsoft.com/msdownload/platformsdk/instmsi.htm) Win32 Programmer Reference: CallWindowProc; SetWindowLong APPLIES TO
| Article Translations
|
Back to the top
