An Interview with Stephen Teilhet
Author of Subclassing & Hooking with Visual Basic
by Glen Gillmore06/04/2001
Windows is a message-based system. This means that every user action or system action creates one or more messages. Even the simplest action can produce an astonishing number of messages. So what does this have to do with Subclassing & Hooking with Visual Basic, O'Reilly's upcoming book? Subclassing and hooking operate on the messages created by an action, allowing Visual Basic developers to manipulate and thereby change the way in which a system behaves. When used correctly, subclassing and hooking provide developers with two very powerful application-development techniques.
Glen Gilmore caught up with the author of Subclassing & Hooking, Stephen Teilhet, and asked him to elaborate on the importance of subclassing and hooking, the challenges developers face when using them, and how VB.NET has made adding subclassing code to your application much easier.
Gillmore: What is subclassing and hooking? What is it used for, and who needs it?
Teilhet: Simply put, subclassing and hooking involve the interception of messages. Messages in the Windows operating system can originate from several different locations, such as the mouse, the keyboard, the system, your own application, and even other applications. Each of these messages is directed towards one or more windows. Using subclassing or hooking, we can intercept any message before--and even after--it is received by the destination window (or windows).
Being able to examine and manipulate messages in this manner allows developers to have more control over their applications as well as the operating system. For example, when a window is resized or moved, several different types of messages are sent to the window that is being resized. With subclassing, the developer can control how and if the user can resize or move the window by intercepting and modifying a few of these messages. To illustrate the point further, the Windows operating system has hundreds of messages that it can send to a window. Imagine being able to control or manipulate any of these messages in your own unique way. This is the type of power that subclassing and hooking open up to the developer.
Hooking is somewhat different than subclassing. Hooking allows the developer to intercept a specific group of messages. There are 15 different types of hooks that the Windows operating system exposes and each one of them intercepts a specific grouping of messages. Hooking is used when you want more control over a specific system. For example, the WH_MOUSE hook only intercepts messages that are used by the mouse, and the WH_KEYBOARD hook only intercepts messages that are used by the keyboard. Some hooks, such as the WH_CBT hook, are used for specific types of applications. This particular hook assists developers in creating computer-based training (CBT) applications.
Just about any developer will run into problems that are easily solved by using subclassing or hooks. Many developers not familiar with subclassing or hooking may give up or try to create overly complex solutions for problems.
A little creativity mixed with some subclassing and hooking code empowers a developer to create almost any application or add almost any type of functionality to an application.
|
Related Reading
Subclassing and Hooking with Visual Basic |
Gillmore: What skills does someone need prior to tackling the information you present?
Teilhet: I tried to write this book so that developers of all levels could understand and use these techniques in the real world. The first few chapters describe how the Windows messaging system operates in detail as well as how subclassing and hooking operate at a high level and how they fit into the Windows messaging system. More advanced developers can either scan these chapters or skip them completely. It is imperative that this material is thoroughly understood before moving on to the more technical parts of the book. The rest of the book devotes a chapter to each of the types of subclassing techniques and hooks available to the developer.
Gillmore: What does your book provide that MSDN doesn't?
Teilhet: There are several things that my book provides that are not available on MSDN (Microsoft Developer's Network). To start with, I go into details about problems that I have run into while writing subclassing and hooking code. Any one of these problems can be encountered while developing real-world applications. I also consolidate information that is scattered throughout the MSDN into simple, easy-to-read lists. This information includes such things as the pros and cons of using subclassing or hooking, when to use hooks instead of subclassing, cases where it is appropriate to use each specific type of subclassing or hook, rules to follow when using subclassing or hooks, and much more. There is information in my book that can only be gleaned from using subclassing and each type of hook. I have also taken some of the more advanced and less-documented concepts discussed in the MSDN and gone into greater detail about them.
Gillmore: Are there particular challenges VB programmers face when trying to implement subclassing and hooking?
Teilhet: Yes, although, you might be surprised to hear that the challenges lie not in writing the code to install and remove the subclassing or the hook, but in the handling of the messages. The challenges are actually threefold. First, the developer must determine which messages are pertinent to capture and modify. Second, the callback function must be written so that it does not interfere with the other events of the application and the operating system that you do not wish to modify. Third, when there is a bug in the code, the developer must use different techniques and tools from those they normally might use to analyze and debug the application. My book goes into depth on each of these topics.
One of the problems that I feel turns off developers (especially VB developers) to subclassing and hooking is the fact that using these techniques makes it much easier to cause a general protection fault (GPF). These problems are usually much harder to find and fix. Another big problem with subclassing and hooking is that of performance. If you use either of these techniques, the performance of the application or even of the system may be degraded to unacceptable levels. (Unfortunately, this is quite easy to do, especially with a type of hook called a system-wide hook.)
As I stated earlier, both subclassing and hooking allow the developer to intercept messages and, possibly, act on them. In doing so the message must go through an additional layer of code. For simplicity's sake, I will not describe this layer of code in detail. Instead I will say that this code is what a developer would write to handle the messages. This additional layer of code can cause undue performance degradation if written improperly. Be aware that it is highly likely that large numbers of messages could be sent to your application every second; your subclassing or hooking code will need to handle all or a subset of these messages.
Two examples of situations where performance would be negatively affected are executing a bubble sort on a long list of names or performing a database commit inside of the subclassing or hooking code. However, I discuss the performance pitfalls of writing subclassing and hooking code in my book and I show the developer how to avoid or work around performance problems that can be introduced into their code.
Subclassing & Hooking also provides a lot of in-depth detail about where to check for many different problems, how to avoid writing code that is problematic, and the methods and tools used to find the source of general protection faults, performance bottlenecks, and other types of problems you can encounter.
O'Reilly & Associates has published C# Essentials, which introduces the Microsoft C# programming language; and .NET Framework Essentials, which provides a technical overview of the Microsoft .NET Framework.
Gillmore: What are some of the ways VB.NET might impact subclassing and hooking code written with VB5 and VB6?
Teilhet: Actually, many of the rules outlined in my book for writing good subclassing and hooking code carry over to VB.NET from VB5 and VB6. VB.NET makes adding subclassing code to your application much easier. To a degree, subclassing is built into the classes that are used to create windows and controls. In fact, there are three different ways to subclass a window or control in VB.NET. Since these three methods of subclassing are built into the Base Class Library (BCL) classes, they are implemented in a similar manner regardless of the .NET language a developer chooses to use.
However, neither VB.NET nor any other .NET language makes using hooks any easier. The good news, though, is that they are not much harder to use. However, you must become proficient with delegates (similar to type-safe function pointers) in order to use hooks in any .NET language.
My book covers implementing subclassing and hooks in VB.NET. I also cover what a WinForm is and how to use it; what delegates are and how to use them; and as an added bonus, I explain nine different methods buried in the class hierarchy that allow for easier capturing of keystrokes from a window or control. Porting this code to C# or another .NET language should be fairly simple.
For more information about .NET, check out O'Reilly's new .NET Resource Center.
Gillmore: For newbies who want to harness the power of Windows by intercepting its messages yet don't want to subject their clients to a blizzard of GPFs, what words of comfort can you offer?
Teilhet: First, start out by understanding the Windows messaging system. This will help immensely with debugging your code. Second, understand the message or messages that you want to intercept. Finally, understand how subclassing or the specific type of hook you are using operates. Unfortunately, there is no one-step method to writing good subclassing or hooking code. In my book, I try to guide all levels of developers, from newbies to senior-level developers, through these three steps in a way that is easy to understand. In addition, my book guides developers along the path to writing good, efficient, and bug-free subclassing and hooking code, based on what I learned from hard-won experience.
Stephen Teilhet earned a degree in electrical engineering but soon got into writing software. He has worked for several consulting companies on a wide range of projects, specializing in Visual Basic, C++ MTS, COM+, MSMQ, and SQL Server. His interest in understanding the underlying mechanisms of software, rather than just using the software, is what led him to write this book. He has also written for the Visual Basic Programmers Journal. Stephen currently works for Compuware NuMega Labs in Nashua, New Hampshire, where he is immersed in the Microsoft .NET technologies.
O'Reilly & Associates will soon release (June 2001) Subclassing & Hooking with Visual Basic.
Sample Chapter 1, Introduction, is available free online.
You can also look at the Table of Contents, the Index, and the Full Description of the book.
For more information, or to order the book, click here.

