?
Solved

VB .NET Hooking

Posted on 2006-04-16
7
Medium Priority
?
523 Views
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
        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
0
Comment
Question by:inxil
  • 3
5 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16468784
"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
 
LVL 7

Author Comment

by:inxil
ID: 16469185
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1000 total points
ID: 16469249
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
 
LVL 10

Assisted Solution

by:bchoor
bchoor earned 1000 total points
ID: 16474769
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16475028
Well there ya go...

Thanx for posting one bchoor!
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

840 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