如何从进程 ID 中查找 exe 名称
How to find exe name from process id
我有一个函数可以结束活动进程 window。它获得 window 句柄,然后找到它的进程 ID 并结束它的进程。但问题是 (explorer.exe)
如果进程名称等于 (explorer.exe)
,我想设置一个忽略结束进程的条件
但我不知道如何
这是我的代码:
Private Sub ENDS_WINDOW_PROCESS(Window_Handle As Long)
Dim target_process_id As Long
Dim target_process_handle As Long
If Window_Handle = 0 Then
'MsgBox "Error finding target window handle"
Exit Sub
End If
GetWindowThreadProcessId Window_Handle, target_process_id
If target_process_id = 0 Then
'MsgBox "Error finding target process ID"
Exit Sub
End If
target_process_handle = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, ByVal 0&, target_process_id)
If target_process_handle = 0 Then
'MsgBox "Error finding target process handle"
Exit Sub
End If
If TerminateProcess(target_process_handle, 0&) = 0 Then
'MsgBox "Error terminating process"
Else
'MsgBox "Process terminated"
End If
CloseHandle target_process_handle
End Sub
试试这个函数(从 http://www.vbforums.com/showthread.php?763427-RESOLVED-Get-exe-name-from-Process-ID-fails-Help 修改而来)
Public Function GetExeName(pid As Long) As String
Dim Process As Object
Dim sFilePath As String
Dim lPos As Long
On Error GoTo GetFileErr:
'Scan process and find pid then return the path and exe name
For Each Process In GetObject("winmgmts:").ExecQuery("Select * from Win32_Process")
If (pid = CLng(Process.ProcessID)) Then
'Return exe path
sFilePath = Process.ExecutablePath
lPos = InStrRev(sFilePath, "\", Compare:=vbTextCompare)
If lPos > 0 Then GetExeName = Mid$(sFilePath, lPos + 1)
Exit Function
End If
Next Process
Exit Function
GetFileErr:
GetExeName = vbNullString
End Function
我有一个函数可以结束活动进程 window。它获得 window 句柄,然后找到它的进程 ID 并结束它的进程。但问题是 (explorer.exe)
如果进程名称等于 (explorer.exe)
,我想设置一个忽略结束进程的条件但我不知道如何
这是我的代码:
Private Sub ENDS_WINDOW_PROCESS(Window_Handle As Long)
Dim target_process_id As Long
Dim target_process_handle As Long
If Window_Handle = 0 Then
'MsgBox "Error finding target window handle"
Exit Sub
End If
GetWindowThreadProcessId Window_Handle, target_process_id
If target_process_id = 0 Then
'MsgBox "Error finding target process ID"
Exit Sub
End If
target_process_handle = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, ByVal 0&, target_process_id)
If target_process_handle = 0 Then
'MsgBox "Error finding target process handle"
Exit Sub
End If
If TerminateProcess(target_process_handle, 0&) = 0 Then
'MsgBox "Error terminating process"
Else
'MsgBox "Process terminated"
End If
CloseHandle target_process_handle
End Sub
试试这个函数(从 http://www.vbforums.com/showthread.php?763427-RESOLVED-Get-exe-name-from-Process-ID-fails-Help 修改而来)
Public Function GetExeName(pid As Long) As String
Dim Process As Object
Dim sFilePath As String
Dim lPos As Long
On Error GoTo GetFileErr:
'Scan process and find pid then return the path and exe name
For Each Process In GetObject("winmgmts:").ExecQuery("Select * from Win32_Process")
If (pid = CLng(Process.ProcessID)) Then
'Return exe path
sFilePath = Process.ExecutablePath
lPos = InStrRev(sFilePath, "\", Compare:=vbTextCompare)
If lPos > 0 Then GetExeName = Mid$(sFilePath, lPos + 1)
Exit Function
End If
Next Process
Exit Function
GetFileErr:
GetExeName = vbNullString
End Function