|
-
May 23rd, 2010, 09:32 AM
#1
Thread Starter
Hyperactive Member
[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?
-
May 23rd, 2010, 10:16 AM
#2
Addicted Member
Re: How to get full path of running process?
Put this in a Form
vb Code:
Option Explicit Private Declare Function GetModuleFileName Lib "kernel32.dll" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long Private Const MAX_PATH As Long = 260 Private Sub Form_Load() Dim szBuff As String, lRet As Long szBuff = Space(MAX_PATH) lRet = GetModuleFileName(0, szBuff, MAX_PATH) szBuff = Left(szBuff, lRet) Call MsgBox("Process Path: " + szBuff, vbOKOnly Or vbInformation, "Process Path") Call Unload(Me) End Sub
-
May 23rd, 2010, 11:00 AM
#3
Thread Starter
Hyperactive Member
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.
-
May 23rd, 2010, 11:43 AM
#4
Addicted Member
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:
Option Explicit Private Declare Function OpenProcess Lib "KERNEL32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "KERNEL32.dll" (ByVal hObject As Long) As Long 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 Private Declare Function Process32First Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long Private Declare Function Process32Next Lib "KERNEL32.dll" (ByVal hSnapshot As Long, ByRef lppe As PROCESSENTRY32) As Long Private Declare Function CreateToolhelp32Snapshot Lib "KERNEL32.dll" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long Private Const TH32CS_SNAPPROCESS As Long = &H2 Private Const MAX_PATH As Long = 260 Private Const PROCESS_QUERY_INFORMATION As Long = (&H400) Private Const PROCESS_VM_READ As Long = (&H10) Private Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szExeFile As String * MAX_PATH End Type Private Sub Form_Load() Dim lRet As Long, tPE As PROCESSENTRY32, szBuff As String, hProcSnapShot As Long, hProc As Long, _ lCntChars As Long, bOK As Boolean hProcSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) If (hProcSnapShot) Then tPE.dwSize = Len(tPE) lRet = Process32First(hProcSnapShot, tPE) Do While (lRet) bOK = False hProc = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, tPE.th32ProcessID) If (hProc) Then szBuff = Space(MAX_PATH) lCntChars = GetModuleFileNameEx(hProc, 0, szBuff, MAX_PATH) If (lCntChars) Then szBuff = Left(szBuff, lCntChars) bOK = True End If Call CloseHandle(hProc) End If If (bOK) Then Debug.Print "PID: " & tPE.th32ProcessID & " (" + szBuff + ")" Else Debug.Print "Unable to get path for process with ID " & tPE.th32ProcessID End If lRet = Process32Next(hProcSnapShot, tPE) Loop End If Call CloseHandle(hProcSnapShot) Call Unload(Me) End Sub
Last edited by Xiphias3; May 23rd, 2010 at 12:02 PM.
-
May 23rd, 2010, 12:00 PM
#5
Addicted Member
Re: How to get full path of running process?
-
May 23rd, 2010, 12:02 PM
#6
Re: How to get full path of running process?
Good times.
vb Code:
Public Const PROCESS_QUERY_INFORMATION As Long = (&H400)
Public Const PROCESS_VM_READ As Long = (&H10)
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, ByVal cb As Long, ByRef cbNeeded As Long) As Long
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
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
Private Sub PrintProc(ProcessID As Long)
Dim hProc As Long, hMod As Long, cbNeeded As Long
Dim strBuff As String, lRet As Long
strBuff = Space(260)
hProc = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, ProcessID)
If hProc <> 0 Then
If EnumProcessModules(hProc, hMod, 4&, cbNeeded) Then
lRet = GetModuleFileNameEx(hProc, hMod, strBuff, 260)
strBuff = Left$(strBuff, lRet)
Debug.Print ProcessID & " " & strBuff
End If
CloseHandle hProc
End If
End Sub
Public Function GetProcess()
Dim aProcess(1023) As Long, cbNeeded As Long, cProcess As Long, I As Long
EnumProcesses ByVal VarPtr(aProcess(0)), 1024 * 4, cbNeeded
cProcess = cbNeeded / 4
For I = 0 To cProcess
PrintProc aProcess(I)
Next I
End Function
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
-
May 23rd, 2010, 12:08 PM
#7
Addicted Member
Re: How to get full path of running process?
Oh, you just added the CloseHandle().
-
May 23rd, 2010, 12:14 PM
#8
Re: How to get full path of running process?
 Originally Posted by Xiphias3
Oh, you just added the CloseHandle().
Seemed like a good idea.
Software I use and highly recommend: Opera, Miranda IM, Peerblock, Winamp, Unlocker Assistant, JoyToKey, Virtual CloneDrive, Secunia PSI, ExplorerXP, GOM Player, Real Alternative, Quicktime Alternative,Sumatra PDF, and non-freeware: Photoshop and VB6( ).
My codebank: AllRGB, Rounded Rectangle(math), Binary Server, Buddy Paint, LoadPictureGDI+, System GUID/Volume Serial, HexToAsc, List all processes and their paths, quasiString matching
Strings(search, extraction, retrieval etc): Retrieve BBCode Link from HTML, RemoveBetween ()'s, strFindBetween(str1,str2), Insert text in HTML, HTML - GetSpanByID
-
May 23rd, 2010, 02:23 PM
#9
Thread Starter
Hyperactive Member
Re: How to get full path of running process?
Thanks, mates!
Resolved!
-
Dec 26th, 2024, 05:37 AM
#10
Fanatic Member
Re: How to get full path of running process?
 Originally Posted by Lauriux1
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|