orm_Unload(Cancel As Integer)
Call UnHookKBD
End Sub
*****************************************************************************
七、自订型态的传递
因这只要用ByRef的方式来做就没有什麽大的问题,故不做说明。
八、综合应用
我们再以一个实例来说明Win API在VB5中呼叫的技巧。有一个函式叫CopyMemory
的宣告如下:
-----------------------------------------------------------------------------
Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" ( _
lpvDest As Any, lpvSource As Any, ByVal cbCopy as Long)
-----------------------------------------------------------------------------
这个函式可以将 lpvDest的momory copy 到lpvSource上去,cbCopy则代表要copy
多少个byte。有了这个函式,我们可以知道一个Double值存在Memory中的各个byte到底
是多少。
-----------------------------------------------------------------------------
Dim dbl as Double
Dim bte(0 to 7) as Byte
Dbl = 168.256
CopyMemory dbl, byt(0), 8
-----------------------------------------------------------------------------
如此检视bte阵列便可以知道这Double值的各个byte是多少。再以另一个
JournalRecord Hook为例来说明:
范例六
*****************************************************************************
'' 以下在Hook.bas
Const WM_MOUSELAST = &H209
Const WM_MOUSEFIRST = &H200
Public Const WM_KEYLAST = &H108
Public Const WM_KEYFIRST = &H100
Public Const WH_JOURNALRECORD = 0
Type EVENTMSG
message As Long
paramL As Long
paramH As Long
time As Long
hwnd As Long
End Type
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
Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, _
ByVal nCode As Long, ByVal wParam As Long, lParam As Any) As Long
Declare Sub CopyMemory Lib "KERNEL32" Alias "RtlMoveMemory" _
(lpvDest As Any, ByVal lpvSource As Long, ByVal cbCopy As Long)
Public hNxtHook As Long '' handle of Hook Procedure
Public msg As EVENTMSG
Sub EnableHook()
hNxtHook = SetWindowsHookEx(0, AddressOf HookProc, App.hInstance, 0)
End Sub
Sub FreeHook()
Dim ret As Long
ret = UnhookWindowsHookEx(hNxtHook)
End Sub
Function HookProc(ByVal code As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
CopyMemory msg, lParam, Lenb(msg)
If (msg.message >= WM_KEYFIRST _
And msg.message <= WM_KEYLAST) Then
Debug.Print msg.message, msg.paramH
End If
HookProc = CallNextHookEx(hNxtHook, code, wParam, lParam)
End Function
''以下程式於Form1
Private Sub Form_Load()
Call EnableHook
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call FreeHook
End Sub
*****************************************************************************
详细的流程不多做说明,我们只把重点放在HookProc这个Hook Procedure,如果我
们查JournalRecord Hook的Hook Procedure可得定义如下:
-----------------------------------------------------------------------------
LRESULT CALLBACK JournalRecordProc(
int code, // hook code
WPARAM wPar