Visual Basic 通过PID获取进程文件路径

来源:互联网 发布:网络信息收集步骤 编辑:程序博客网 时间:2024/05/29 11:16
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 LongOn 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 FunctionErr:    lRet = CloseHandle(lID)End Function

0 0