请教关于查程序路径,使用HOOK调用出错,使用按钮正常
请教大家,我想查运行的qq.exe程序的程序路径,使用以下的程序。但我用键盘HOOK来执行就出错,而在主窗口中用按钮就可以正常执行。为什么,有办法解决吗,请高手指点。For Each ename In GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_ '循环进程
If UCase(ename.Name) = UCase("qq.exe") Then MsgBox ename.executablepath '查找名为QQ.exe的进程路径
Next
----------------------------
form1.frm
Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf MyKBHook, App.hInstance, 0)
If hHook = 0 Then End '如果钩子注册失败会返回0,否则返回注册的钩子句柄
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnhookWindowsHookEx hHook
End Sub
Private Sub Command1_Click()
Dim ename As Object
For Each ename In GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_ '循环进程
If UCase(ename.Name) = UCase("qq.exe") Then MsgBox ename.executablepath '查找名为QQ.exe的进程路径
Next
End Sub
---------------------------------
Module1.bas
对象 --------------------编程问答-------------------- 在你的按钮事件定义时改一下
Public hHook As Long '用来存放钩子的句柄
Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
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 CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public a As Long
Public Type EVENTMSG
vKey As Long
sKey As Long
flag As Long
time As Long
End Type
Public mymsg As EVENTMSG
Public Const WH_KEYBOARD_LL = 13
Public Const WM_KEYDOWN = &H100
Public Function MyKBHook(ByVal ncode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim ename As Object
'这些参数在不同钩子中具有不同含义,在这里ncode 是类型代码
If ncode = 0 Then
If wParam = WM_KEYDOWN Then '在这里wParam 表示键盘事件,具体的按键信息保存在lParam 指针所指向的内存区域中
'把内存中lParam 指针所指向的数据复制到mymsg这个自定义类型
CopyMemory mymsg, ByVal lParam, Len(mymsg)
If wParam = WM_KEYDOWN Then
CopyMemory mymsg, ByVal lParam, Len(mymsg)
Select Case mymsg.vKey
Case vbKeyTab '按下TAB键
MsgBox "tab"
For Each ename In GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_ '循环进程
If UCase(ename.Name) = UCase("qq.exe") Then MsgBox ename.executablepath '查找名为QQ.exe的进程路径
Next
End Select
End If
' Form1.Text1.Text = mymsg.sKey
End If
End If
'将消息传给下一个钩子,如果你想锁定键盘,只需要把这句改成MyKBHook =-1,表示吃掉这个消息,这样键盘就输入不了了:-)
MyKBHook = CallNextHookEx(hHook, ncode, wParam, lParam)
End Function
Public Sub Command1_Click()
然后在你的处理过程调用该事件看看,如
Case vbKeyTab '按下TAB键
Form1.Command1_Click
End Select --------------------编程问答-------------------- 按楼上的试了一下,还是一样出问题。说是自动化错误。
1.点击按钮操作,正常。
2.按Tab键,弹出自动化错误。是 Command1_Click函数出错。
--------------------编程问答-------------------- 你不用wmi,换成api来枚举应该没问题,wmi的效率低,而且涉及com/com+与内部服务通讯,所以换种方法可能会比较好
补充:VB , 基础类