Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

This time i'm not mistaking jmarler ;-)

Status
Not open for further replies.

LuckyLuke

Programmer
Mar 28, 2001
188
NL
Hi all,

I finally found a way to do system wide hooking inside visual basic, it doesn't even have to be inside a dll! :) It is done by hooking into the WH_JOURNALRECORD hook. Together with a WH_GETMESSAGE hook it is possible to set up a system wide keyboard/mouse hook. I'm still tuning my code a bit and I'll post it later. Meanwhile you can try it out yourself a bit already ;-)

LuCkY
 
Hey Lucky,
I'd love to see your code for this . . . in the meantime, check out this thread (thread222-118564). StrongM figured out a way to implement a systemwide hook in pure VB (with very easy code) by using a new message that was added after NT4 SP3 adnd WIN 2k. However, this won't work in Win9x since Win9x does not use the WH_KEYBOARD_LL message. - Jeff Marler B-)
 
Here it is:

[tt]
Option Explicit

Public Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cBytes As Long)

Public Const WH_JOURNALRECORD = 0
Public Const WH_GETMESSAGE = 3

Private Const WM_LBUTTONDOWN = &H201
Private Const WM_CANCELJOURNAL = &H4B

Private Type POINTAPI
x As Long
y As Long
End Type

Private Type MSG
hWnd As Long
Message As Long
wParam As Long
lParam As Long
Time As Long
pt As POINTAPI
End Type

Private Type EVENTMSG
Message As Long
lParamLow As Long
lParamHigh As Long
Time As Long
hWnd As Long
End Type

Public m_hJournalRecord As Long, m_hGetMsg As Long

Public Function JournalRecordProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If nCode < 0 Then
JournalRecordProc = CallNextHookEx(m_hJournalRecord, nCode, wParam, lParam)
Else
Call ProcessMessage(lParam)
Call CallNextHookEx(m_hJournalRecord, nCode, wParam, lParam)
End If
End Function

Public Function GetMsgProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If nCode < 0 Then
GetMsgProc = CallNextHookEx(m_hGetMsg, nCode, wParam, lParam)
Else
Dim Message As MSG

Call CopyMemory(Message, ByVal lParam, Len(Message))
Select Case Message.Message
Case WM_CANCELJOURNAL
If wParam = 1 Then Call ProcessMessage(WM_CANCELJOURNAL)
End Select

Call CallNextHookEx(m_hGetMsg, nCode, wParam, ByVal lParam)
End If
End Function

Private Sub ProcessMessage(ByVal lParam As Long)
Dim Message As EVENTMSG

If lParam = WM_CANCELJOURNAL Then
m_hJournalRecord = SetWindowsHookEx(WH_JOURNALRECORD, AddressOf JournalRecordProc, App.hInstance, 0)
Exit Sub
End If

Call CopyMemory(Message, ByVal lParam, Len(Message))
Select Case Message.Message
Case WM_LBUTTONDOWN
'etc
End Select
End Sub
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top