What Is Subclassing?

The idea behind subclassing is simple; implementing it is not so simple. Subclassing is, in simple terms, the creation of a new window procedure, which is inserted into the message stream just before the default window procedure that every window initially starts out with. I use the term message stream to denote the path a message takes from its source to its destination.

This is not to say that subclassing is not dangerous when implemented incorrectly or without regard to other processes running in the operating system. But by understanding the messaging system and following its rules, we can safely use subclassing.

The dangers of subclassing are what stop many programmers from learning and using subclassing in their projects. The foremost danger with subclassing is causing a General Protection Fault (GPF). GPFs are critical errors that will either cause your application/system to stop functioning or cause the application/system to shut down. If you are running an application in the VB IDE and it causes a GPF, not only is the application shut down, but also the IDE. This is because both are running in the same process, and a GPF will cause that process to terminate.

Several things cause GPFs. They include:

  • Using a pointer variable that points to an invalid location in memory

  • Trying to read from memory that is not accessible from the application (e.g., in a separate process memory space)

  • Trying to write to memory that is not accessible from the application ...

Get Subclassing and Hooking with Visual Basic now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.