获取进程命令行之三

引用内容 引用内容

Option Explicit


Private Type CLIENT_ID
    UniqueProcess
As Long
    
UniqueThread  As Long
End
Type


Private Const SYNCHRONIZE As Long = &H100000
Private Const STANDARD_RIGHTS_REQUIRED As Long = &HF0000
Private Const PROCESS_VM_READ = &H10

Private Const PROCESS_QUERY_INFORMATION As Long = (&H400)
Private Declare Function NtQueryInformationProcess Lib "ntdll.dll" (ByVal ProcessHandle As Long, _
                                
ByVal ProcessInformationClass As PROCESSINFOCLASS, _
                                
ByVal ProcessInformation As Long, _
                                
ByVal ProcessInformationLength As Long, _
                                
ByRef ReturnLength As Long) As Long

Private Enum
PROCESSINFOCLASS
    ProcessBasicInformation =
0
    
ProcessQuotaLimits
    ProcessIoCounters
    ProcessVmCounters
    ProcessTimes
    ProcessBasePriority
    ProcessRaisePriority
    ProcessDebugPort
    ProcessExceptionPort
    ProcessAccessToken
    ProcessLdtInformation
    ProcessLdtSize
    ProcessDefaultHardErrorMode
    ProcessIoPortHandlers
    ProcessPooledUsageAndLimits
    ProcessWorkingSetWatch
    ProcessUserModeIOPL
    ProcessEnableAlignmentFaultFixup
    ProcessPriorityClass
    ProcessWx86Information
    ProcessHandleCount
    ProcessAffinityMask
    ProcessPriorityBoost
    ProcessDeviceMap
    ProcessSessionInformation
    ProcessForegroundInformation
    ProcessWow64Information
    ProcessImageFileName
    ProcessLUIDDeviceMapsEnabled
    ProcessBreakOnTermination
    ProcessDebugObjectHandle
    ProcessDebugFlags
    ProcessHandleTracing
    ProcessIoPriority
    ProcessExecuteFlags
    ProcessResourceManagement
    ProcessCookie
    ProcessImageInformation
    MaxProcessInfoClass
End Enum

Private
Type PROCESS_BASIC_INFORMATION
    ExitStatus
As Long
'NTSTATUS
    
PebBaseAddress As Long 'PPEB
    
AffinityMask As Long 'ULONG_PTR
    
BasePriority As Long 'KPRIORITY
    
UniqueProcessId As Long 'ULONG_PTR
    
InheritedFromUniqueProcessId As Long 'ULONG_PTR
End Type

Private Declare Function NtOpenProcess Lib "ntdll.dll" (ByRef ProcessHandle As Long, _
                                
ByVal AccessMask As Long, _
                                
ByRef ObjectAttributes As OBJECT_ATTRIBUTES, _
                                
ByRef ClientID As CLIENT_ID) As Long

Private
Type OBJECT_ATTRIBUTES
    Length
As Long
    
RootDirectory As Long
    
ObjectName As Long
    
Attributes As Long
    
SecurityDescriptor As Long
    
SecurityQualityOfService As Long
End
Type


Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Public Declare Function
GetCurrentProcessId Lib "kernel32" () As Long
Public Declare Function
GetCurrentThreadId Lib "kernel32" () As Long

Private Declare Sub
CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (ByRef Destination As Any, _
                                      
ByRef Source As Any, _
                                      
ByVal Length As Long)
                                      

Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function
GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function
FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function
CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Declare Function
ReadProcessMemory Lib "kernel32" (ByVal hProcess As Long, lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long

'Public gblnIsChange As Boolean

'判断Nt系列函数是否调用成功
Private Function NT_SUCCESS(ByVal nStatus As Long) As Boolean
    
NT_SUCCESS = (nStatus >= 0)
End Function


Public Function
GetProcessCommandLine(ByVal dwProcessId As Long) As String
    Dim
ntStatus As Long
    Dim
objBasic As PROCESS_BASIC_INFORMATION
    
Dim objFlink As Long
    Dim
objPEB As Long, objLdr As Long
    Dim
objBaseAddress As Long
    Dim
bytName(260 * 2 - 1) As Byte
    Dim
strModuleName As String, objName As Long
    Dim
objCid As CLIENT_ID
    
Dim objOa As OBJECT_ATTRIBUTES

    
Dim hProcess As Long
    
objOa.Length = Len(objOa)
    objCid.UniqueProcess = dwProcessId
    ntStatus = NtOpenProcess(hProcess, PROCESS_QUERY_INFORMATION
Or PROCESS_VM_READ, objOa, objCid)
    
If hProcess = 0 Then
        
GetProcessPath = ""
        
Exit Function
    End If
    Dim
lngRet As Long, lngReturn As Long
    
ntStatus = NtQueryInformationProcess(hProcess, ProcessBasicInformation, VarPtr(objBasic), Len(objBasic), ByVal 0&)
    
If (NT_SUCCESS(ntStatus)) Then
        
objPEB = objBasic.PebBaseAddress
        lngRet = ReadProcessMemory(hProcess,
ByVal objPEB + &HC, objLdr, 4, ByVal 0&)
        lngRet = ReadProcessMemory(hProcess,
ByVal objLdr + &H14, objFlink, 4, ByVal 0&)
        lngRet = ReadProcessMemory(hProcess,
ByVal objFlink + &H18, objBaseAddress, 4, ByVal 0&)
        
If objBaseAddress > 0 Then
            
lngRet = ReadProcessMemory(hProcess, ByVal objFlink + &H28, objName, 4, ByVal 0&)
            lngRet = ReadProcessMemory(hProcess,
ByVal objName, bytName(0), 260 * 2, ByVal 0&)
            strModuleName = bytName
            strModuleName = Mid(strModuleName, InStr(strModuleName,
""""), Len(strModuleName) - InStr(strModuleName, """"))
            strModuleName = Left(strModuleName & Chr(
0), InStr(strModuleName & Chr(0), Chr(0)) - 1)
            GetProcessPath = strModuleName
        
End If
    End If
    
CloseHandle hProcess
End Function



[本日志由 JiaJia 于 2008-03-29 05:11 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: VB 进程
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.