Hooks and VB.NET
Unlike subclassing and superclassing, VB.NET does not readily support the use of the standard Windows hooks within the language.
Tip
None of the .NET languages make any special concessions to easily implement Windows hooks. To implement hooks within other .NET languages, such as C#, follow the same steps as are outlined in this chapter.
Actually, more code is involved with implementing hooks in VB.NET than in the previous versions of Visual Basic (VB). This is because we must use delegates to implement the filter function callback mechanism that hooks rely on. I discuss delegates in more detail in the next section.
The example applications that we will create in this chapter will install thread-specific hooks. Unfortunately, as with VB 6 dynamic link libraries (DLLs), you cannot use VB.NET DLLs to install system-wide hooks. To install a system-wide hook, you must place the code to install and remove the hook, as well as the filter function itself, within a Win32 DLL. You can write this DLL using Visual C++ 6 or Visual C++.NET. Note that if you are using Visual C++.NET, you must write the DLL as unmanaged code.
As a note, the following functions will be removed after Beta 1:
| Microsoft.Win32.Interop.Windows.SetWindowsHookEx |
| Microsoft.Win32.Interop.Windows.UnhookWindowsHookEx |
| Microsoft.Win32.Interop.Windows.CallNextHookEx |
Instead of using these functions, you should use the normal method of declaring application programming interface (API) functions that you
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access