Ask about the check program path, use the HOOK error, use the buttons to normal

Ask you, I want to check the program path running qq.exe program, use the following procedure. But I use the keyboard HOOK to perform error, but in the main window with buttons can perform normal. Why, there is a solution, please master pointing.
For Each ename In GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_ 'Circulation process
If UCase(ename.Name) = UCase("qq.exe") Then MsgBox ename.executablepath 'The process path search named QQ.exe
Next

----------------------------
form1.frm
Private Sub Form_Load()
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, AddressOf MyKBHook, App.hInstance, 0)
If hHook = 0 Then End 'If the hook registration failure will return 0, otherwise it returns a hook handle registration

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_ 'Circulation process
If UCase(ename.Name) = UCase("qq.exe") Then MsgBox ename.executablepath 'The process path search named QQ.exe
Next
End Sub

---------------------------------
Module1.bas

Public hHook As Long 'The handle is used to store the hook

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
'These parameters have different meanings in different hook, where ncode is the type code
If ncode = 0 Then
If wParam = WM_KEYDOWN Then 'Here wParam keyboard events, specific key information stored in the lParam pointer to the memory area

'Copy the memory lParam to which the pointer data to mymsg the custom type
CopyMemory mymsg, ByVal lParam, Len(mymsg)

If wParam = WM_KEYDOWN Then
CopyMemory mymsg, ByVal lParam, Len(mymsg)
Select Case mymsg.vKey
Case vbKeyTab 'Press the TAB key
MsgBox "tab"
For Each ename In GetObject("winmgmts:\\.\root\cimv2:win32_process").instances_ 'Circulation process
If UCase(ename.Name) = UCase("qq.exe") Then MsgBox ename.executablepath 'The process path search named QQ.exe
Next
End Select
End If
' Form1.Text1.Text = mymsg.sKey
End If

End If

'The message to the next hook, if you want to lock the keyboard, only need to put this sentence into a MyKBHook =-1, that eat the message, so the keyboard input can: -)
MyKBHook = CallNextHookEx(hHook, ncode, wParam, lParam)

End Function

Started by Harry at December 08, 2016 - 7:05 AM

Change in defining your button events
Public Sub Command1_Click()

And then in the call processing the events you have a look, such as
Case vbKeyTab 'Press the TAB key
Form1.Command1_Click
End Select

Posted by Antonio at December 16, 2016 - 7:41 AM

According to the upstairs to try, or as a problem. That is the automation error.
1 click on the button operation, normal.

2 press the Tab button, pop-up automation error. The Command1_Click function.

Posted by Harry at December 22, 2016 - 7:45 AM

You don't have WMI, change API to enumeration should be no problem, WMI's low efficiency, but also relates to the com/com+ and internal service communication, so another method may be better

Posted by Antonio at December 23, 2016 - 7:59 AM