VB .NET Hooking

Posted on 2006-04-16
Last Modified: 2008-01-09
Okay, let me preface this with the fact that I'm very rusty in VB in general, and that the last version I used was VB 6.  I did some Windows API programming back then, but not a whole bunch...

Okay, so here's what I'm trying to do:

I want to set up a hook that logs the lParam values of a specific WM_USER message sent to an element within an external application.  I'm easily able to find the element with the following code:

' Up with declarations at top of code
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Integer, ByVal hWnd2 As Integer, ByVal lpsz1 As String, ByVal lpsz2 As String) As Integer

' Within a subroutine
Dim E As Integer
E = FindWindowEx(ParentWindow, 0, "ElementType", vbNullString)
E = FindWindowEx(ParentWindow, E, "ElementType", vbNullString)

The problem is that I can't seem to figure out what to do next.  Most of the hooking tutorials out there are about how to hook keyboard or mouse messages, which I'm not interested in.  Here's what I've tried:

' Up with the declarations at top of code
Public Delegate Function CallBack(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
    Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Integer, ByVal lpfn As CallBack, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
    Public Declare Function UnhookWindowsHookEx Lib "user32" Alias "UnhookWindowsHookExA" (ByVal idHook As Integer) As Boolean
    Public Declare Function CallNextHookEx Lib "user32" Alias "CallNextHookExA" (ByVal idHook As Integer, ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer

Public TheHook As Integer
Private HookProc As CallBack

' Within the same subroutine as above
HookProc = AddressOf TheHookProc
TheHook = SetWindowsHookEx(WM_USER + 1004, HookProc, IntPtr.Zero, E) ' E is the same E as from code above

Private Function TheHookProc(ByVal nCode As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer
        Return CallNextHookEx(TheHook, nCode, wParam, lParam)
End Function

I know this is a bit jumbled.  Let me know if anything needs clarification...


Question by:inxil
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    "Most of the hooking tutorials out there are about how to hook keyboard or mouse messages..."

    That's because in general that is the only type of hooking allowed by VB.

    Most other hooks require a language such as C/C++ to implement the hook for you in a DLL.
    LVL 7

    Author Comment

    Are there any C++ libraries out there (pre-written) that could do what I'm looking for, or would I have to write it?
    LVL 85

    Accepted Solution

    I'm sure there are, but I've never used one I'm a very untrusting fellow when it comes to using other peoples compiled code.  My computer works the way I want it to and I want to keep it that way.  =)

    Google for C++ Hook Library and/or DLL...
    LVL 10

    Assisted Solution


    pretty good resource - I converted to VB.NET implementation of the ActiveX control - let me know if you need some help with the conversion

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Well there ya go...

    Thanx for posting one bchoor!

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Join & Write a Comment

    Suggested Solutions

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now