Saturday, April 2, 2011

[Visual Basic] Get File Path Using PID

This Function is for finding the Current File Path using PID ( Process ID )

Alternate way to find the process ID.

You can Find the current application file path using this easily ;)


Public Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId 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
Public Const STANDARD_RIGHTS_REQUIRED As Long = & HF0000
Public Const SYNCHRONIZE As Long = & H100000
Public Const PROCESS_ALL_ACCESS As Long = (STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or & HFFF)

Public Function EnumFileName (pid As Long) As String
Dim hProc As Long, a$
Dim NFile As Long
Dim filename As String * 64 'If thr path is longest the data this hold should also be long
hProc = OpenProcess (PROCESS_ALL_ACCESS, False, pid)
NFile = GetModuleFileNameEx (hProc, 0, filename, 64)
eName = filename & vbNullString 'file path

End Function

0 comments for "[Visual Basic] Get File Path Using PID"

Post a Comment

background