VB .NET Hooking

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
        MsgBox(lParam.ToString)
        Return CallNextHookEx(TheHook, nCode, wParam, lParam)
End Function


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

Thanks,

Chris
LVL 7
inxilAsked:
Who is Participating?
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I'm sure there are, but I've never used one myself...as 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...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
"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.
0
 
inxilAuthor Commented:
Are there any C++ libraries out there (pre-written) that could do what I'm looking for, or would I have to write it?
0
 
bchoorCommented:
http://www.vbforums.com/showthread.php?t=322261

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

HTH
~BC
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Well there ya go...

Thanx for posting one bchoor!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.