Private Sub Command1_Click()
Dim WmiService As Object, Processes As Object, Process As Object
Set WmiService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set Processes = WmiService.ExecQuery("SELECT * FROM Win32_Process WHERE Name = ""qq.exe""")
For Each Process In Processes
Text1 = Process.ProcessId
Next
'Set WmiService = Nothing
'Set Processes = Nothing
'Set Process = Nothing
End Sub
'------------------------------------------------根据进程名取ID
'============================方法一============================
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Sub Command1_Click()
Dim PID As Long
GetWindowThreadProcessId Me.hwnd, PID
Text1.Text = PID
End Sub
'------------------------------------------------根据句柄名取ID
'============================方法二============================