Results 1 to 3 of 3

Thread: How to find exe name from process id

  1. #1

    Thread Starter
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    89

    Unhappy How to find exe name from process id

    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:

    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

  2. #2
    Frenzied Member
    Join Date
    Mar 2008
    Posts
    1,210

    Re: How to find exe name from process id

    Something like this perhaps;

    Code:
    Option Explicit
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetModuleFileNameEx Lib "psapi" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long
    Private Declare Sub CloseHandle Lib "kernel32" (ByVal hObject As Long)
    Private Const TH32CS_SNAPPROCESS = 2&
    Private Const PROCESS_QUERY_INFORMATION = &H400&
    Private Const PROCESS_VM_READ = &H10&
    Private Const MAX_PATH = 260&
    
    'returns the full path of a process identified by Pid.
    Private Function GetPathFromPid(ByVal Pid As Long) As String
        Dim hProcess As Long, s As String * MAX_PATH
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, Pid)
        GetModuleFileNameEx hProcess, 0, s, MAX_PATH    'this call crashes under Win 95, 98 and probably NT4 and ME
        CloseHandle hProcess
        GetPathFromPid = Left$(s, InStr(s, vbNullChar) - 1)
    End Function

  3. #3

    Thread Starter
    Lively Member Mahdi Jazini's Avatar
    Join Date
    Feb 2014
    Location
    Iran / Tehran
    Posts
    89

    Re: How to find exe name from process id

    Thank you very much for your answer

    Using the second part of This Answer and Your Answer i have completed the answer as follow:

    Code:
    Option Explicit
    Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    Private Declare Function GetModuleFileNameEx Lib "psapi" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long
    Private Declare Sub CloseHandle Lib "kernel32" (ByVal hObject As Long)
    Private Const TH32CS_SNAPPROCESS = 2&
    Private Const PROCESS_QUERY_INFORMATION = &H400&
    Private Const PROCESS_VM_READ = &H10&
    Private Const MAX_PATH = 260&
    
    Private Function GetExeFromPid(ByVal Pid As Long) As String
        Dim hProcess As Long, s As String * MAX_PATH
        Dim lPos As Long
        Dim sFilePath As String
        hProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, Pid)
        GetModuleFileNameEx hProcess, 0, s, MAX_PATH    'this call crashes under Win 95, 98 and probably NT4 and ME
        CloseHandle hProcess
        sFilePath = Left$(s, InStr(s, vbNullChar) - 1)
        lPos = InStrRev(sFilePath, "\", Compare:=vbTextCompare)
        If lPos > 0 Then GetExeFromPid = LCase$(Mid$(sFilePath, lPos + 1))
    End Function

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width