Results 1 to 10 of 10

Thread: [RESOLVED] How to get full path of running process?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Resolved [RESOLVED] How to get full path of running process?

    Hello!
    I need some hlep, I want to get full path of running process. I have heard that it is possible to do that using GetModuleFileNameEx, maybe someone could show me an example how to do that?

  2. #2
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: How to get full path of running process?

    Put this in a Form
    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function GetModuleFileName Lib "kernel32.dll" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
    4. Private Const MAX_PATH As Long = 260
    5.  
    6. Private Sub Form_Load()
    7.     Dim szBuff As String, lRet As Long
    8.    
    9.     szBuff = Space(MAX_PATH)
    10.     lRet = GetModuleFileName(0, szBuff, MAX_PATH)
    11.     szBuff = Left(szBuff, lRet)
    12.     Call MsgBox("Process Path: " + szBuff, vbOKOnly Or vbInformation, "Process Path")
    13.     Call Unload(Me)
    14. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to get full path of running process?

    Thanks, Xiphias3, but I was talking about a external process that's why I have mentioned GetModuleFileNameEx.

  4. #4
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: How to get full path of running process?

    Oh, my bad. Lemme type it up.

    EDIT:
    Just so you know, you can also use the szExeFile member of the PROCESSENTRY32 structure.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Declare Function OpenProcess Lib "KERNEL32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    4. Private Declare Function CloseHandle Lib "KERNEL32.dll" (ByVal hObject As Long) As Long
    5. Private Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long
    6. Private Declare Function Process32First Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
    7. Private Declare Function Process32Next Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long
    8. Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
    9.  
    10. Private Const TH32CS_SNAPPROCESS As Long = &H2
    11. Private Const MAX_PATH As Long = 260
    12. Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)
    13. Private Const PROCESS_VM_READ As Long = (&H10)
    14.  
    15. Private Type PROCESSENTRY32
    16.     dwSize As Long
    17.     cntUsage As Long
    18.     th32ProcessID As Long
    19.     th32DefaultHeapID As Long
    20.     th32ModuleID As Long
    21.     cntThreads As Long
    22.     th32ParentProcessID As Long
    23.     pcPriClassBase As Long
    24.     dwFlags As Long
    25.     szExeFile As String * MAX_PATH
    26. End Type
    27.  
    28. Private Sub Form_Load()
    29.     Dim lRet As Long, tPE As PROCESSENTRY32, szBuff As String, hProcSnapShot As Long, hProc As Long, _
    30.         lCntChars As Long, bOK As Boolean
    31.    
    32.     hProcSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    33.     If (hProcSnapShot) Then
    34.         tPE.dwSize = Len(tPE)
    35.         lRet = Process32First(hProcSnapShot, tPE)
    36.         Do While (lRet)
    37.             bOK = False
    38.             hProc = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, tPE.th32ProcessID)
    39.             If (hProc) Then
    40.                 szBuff = Space(MAX_PATH)
    41.                 lCntChars = GetModuleFileNameEx(hProc, 0, szBuff, MAX_PATH)
    42.                 If (lCntChars) Then
    43.                     szBuff = Left(szBuff, lCntChars)
    44.                     bOK = True
    45.                 End If
    46.                 Call CloseHandle(hProc)
    47.             End If
    48.            
    49.             If (bOK) Then
    50.                 Debug.Print "PID: " & tPE.th32ProcessID & " (" + szBuff + ")"
    51.             Else
    52.                 Debug.Print "Unable to get path for process with ID " & tPE.th32ProcessID
    53.             End If
    54.            
    55.             lRet = Process32Next(hProcSnapShot, tPE)
    56.         Loop
    57.     End If
    58.     Call CloseHandle(hProcSnapShot)
    59.     Call Unload(Me)
    60. End Sub
    Last edited by Xiphias3; May 23rd, 2010 at 12:02 PM.

  5. #5
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: How to get full path of running process?

    *added some code*

  6. #6
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: How to get full path of running process?

    Good times.

    vb Code:
    1. Public Const PROCESS_QUERY_INFORMATION As Long = (&H400)
    2. Public Const PROCESS_VM_READ As Long = (&H10)
    3.  
    4. Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    5. Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    6. Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
    7. Public Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, ByRef lphModule As Long, ByVal cb As Long, ByRef lpcbNeeded As Long) As Long
    8. Public Declare Function GetModuleFileNameEx Lib "psapi.dll" Alias "GetModuleFileNameExA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long
    9.  
    10. Private Sub PrintProc(ProcessID As Long)
    11. Dim hProc As Long, hMod As Long, cbNeeded As Long
    12. Dim strBuff As String, lRet As Long
    13. strBuff = Space(260)
    14.  
    15. hProc = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcessID)
    16. If hProc <> 0 Then
    17.   If EnumProcessModules(hProc, hMod, 4&, cbNeeded) Then
    18.     lRet = GetModuleFileNameEx(hProc, hMod, strBuff, 260)
    19.     strBuff = Left$(strBuff, lRet)
    20.     Debug.Print ProcessID & " " & strBuff
    21.   End If
    22.   CloseHandle hProc
    23. End If
    24. End Sub
    25.  
    26. Public Function GetProcess()
    27. Dim aProcess(1023) As Long, cbNeeded As Long, cProcess As Long, I As Long
    28. EnumProcesses ByVal VarPtr(aProcess(0)), 1024 * 4, cbNeeded
    29. cProcess = cbNeeded / 4
    30. For I = 0 To cProcess
    31.   PrintProc aProcess(I)
    32. Next I
    33. End Function

  7. #7
    Addicted Member Xiphias3's Avatar
    Join Date
    Jan 2009
    Location
    Clarendon, Jamaica
    Posts
    188

    Re: How to get full path of running process?

    Oh, you just added the CloseHandle().

  8. #8
    Fanatic Member FireXtol's Avatar
    Join Date
    Apr 2010
    Posts
    874

    Re: How to get full path of running process?

    Quote Originally Posted by Xiphias3 View Post
    Oh, you just added the CloseHandle().
    Seemed like a good idea.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: How to get full path of running process?

    Thanks, mates!

    Resolved!

  10. #10
    Fanatic Member HackerVlad's Avatar
    Join Date
    Nov 2023
    Posts
    681

    Re: How to get full path of running process?

    Quote Originally Posted by Lauriux1 View Post
    Thanks, Xiphias3, but I was talking about a external process that's why I have mentioned GetModuleFileNameEx.
    GetModuleFileNameEx is a good function that returns a truthful result, the correct full path to the process, but it only works for 32-bit processes. Therefore, it is best to use other functions as well. Here, in detail, on how to get the full path to processes, I have written a whole module for this.: https://www.vbforums.com/showthread....ons-of-Windows

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