Reputation: 801
I have a function that ends process of active window. it gets window handle then finds its process id and ends its process. but the problem is (explorer.exe)
I want to put a condition to ignore end process if the process name is equal to (explorer.exe)
But i don't know how
This is my code:
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
Upvotes: 1
Views: 574
Reputation: 720
Try this function (modification from 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
Upvotes: 3