Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long '打开进程
Private Declare Function EnumProcessModules Lib "psapi.dll " (ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, cbNeeded As Long) As Long '枚举进程模块
Private Declare Function GetModuleFileNameExA Lib "psapi.dll " (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpFilename As String, ByVal nSize As Long) As Long '获取模块文件名
Public Function getPath(lPID As Long) As String
'通过PID获取进程文件路径
Dim lTmp As Long
Dim lBuf(1 To 250) As Long
Dim lRet As Long
Dim sPath As String
Dim lSize As Long
Dim lID As Long
On Error GoTo Err
lID = OpenProcess(&H400 Or &H10, 0, lPID)
If lID <> 0 Then
lRet = EnumProcessModules(lID, lBuf(1), 250, lTmp)
If lRet <> 0 Then
sPath = Space(260)
lSize = 500
lRet = GetModuleFileNameExA(lID, lBuf(1), sPath, lSize)
getPath = Left(sPath, lRet)
End If
End If
lRet = CloseHandle(lID)
'判断是否为SYSTEM权限程序
If getPath = "" Then getPath = "SYSTEM"
Exit Function
Err:
lRet = CloseHandle(lID)
End Function
Visual Basic 通过PID获取进程文件路径
最新推荐文章于 2022-04-10 19:09:29 发布