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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
"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.
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?
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
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...

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Mike TomlinsonHigh School Computer Science, Computer Applications, and Mathematics TeachersCommented:
Well there ya go...

Thanx for posting one bchoor!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.