自问自答………………
Option Explicit
Private Const PROCESS_VM_READ As Long = (&H10)
Private Declare Function FreeLibrary Lib "kernel32.dll" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32.dll" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32.dll" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Any) As Long
Public Function GetRemoteCmdLine(ByVal PID As Long) As String
'返回指定进程的命令行
'PID - 目标进程PID
'返回值:
'成功返回命令行,失败返回空字符串
Dim hDll As Long, hProcess As Long, APIPtr As Long
Dim CmdLinePtr As Long, lRet As Long, lRet2 As Long
Dim CmdLineStr As String, CmdLineByte(511) As Byte
hDll = LoadLibrary("kernel32")
'取得GetCommandLineA地址 + 1
'kernel32.dll中的反汇编代码(Win2003版):
'mov eax,dword ptr [7C88B5D4]
'机器码:
'A1D4B5887C
'+1跳过mov指令,后面4个字节就是指向命令行的指针
'这个地址在每个进程里都是一样的,可以直接使用
APIPtr = GetProcAddress(hDll, "GetCommandLineA") + 1
Call FreeLibrary(hDll)
hProcess = OpenProcess(PROCESS_VM_READ, 0, PID) '打开进程
If hProcess = 0 Then Exit Function
If ReadProcessMemory(hProcess, APIPtr, CmdLinePtr, 4, lRet2) <> 1 Then Exit Function '得到7C88B5D4
If ReadProcessMemory(hProcess, CmdLinePtr, CmdLinePtr, 4, lRet2) <> 1 Then Exit Function '再取个指针(竟是两个指针-_-!)
If ReadProcessMemory(hProcess, CmdLinePtr, CmdLineByte(0), 512, lRet2) <> 1 Then Exit Function '拉一块内存过来
CmdLineStr = StrConv(CmdLineByte, vbUnicode) '处理一下,可以输出了
CmdLineStr = Left$(CmdLineStr, InStr(1, CmdLineStr, vbNullChar) - 1)
GetRemoteCmdLine = CmdLineStr
End Function
Option Explicit
Private Const PROCESS_VM_READ As Long = (&H10)
Private Declare Function FreeLibrary Lib "kernel32.dll" (ByVal hLibModule As Long) As Long
Private Declare Function LoadLibrary Lib "kernel32.dll" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32.dll" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function ReadProcessMemory Lib "kernel32.dll" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, ByRef lpBuffer As Any, ByVal nSize As Long, ByRef lpNumberOfBytesWritten As Any) As Long
Public Function GetRemoteCmdLine(ByVal PID As Long) As String
'返回指定进程的命令行
'PID - 目标进程PID
'返回值:
'成功返回命令行,失败返回空字符串
Dim hDll As Long, hProcess As Long, APIPtr As Long
Dim CmdLinePtr As Long, lRet As Long, lRet2 As Long
Dim CmdLineStr As String, CmdLineByte(511) As Byte
hDll = LoadLibrary("kernel32")
'取得GetCommandLineA地址 + 1
'kernel32.dll中的反汇编代码(Win2003版):
'mov eax,dword ptr [7C88B5D4]
'机器码:
'A1D4B5887C
'+1跳过mov指令,后面4个字节就是指向命令行的指针
'这个地址在每个进程里都是一样的,可以直接使用
APIPtr = GetProcAddress(hDll, "GetCommandLineA") + 1
Call FreeLibrary(hDll)
hProcess = OpenProcess(PROCESS_VM_READ, 0, PID) '打开进程
If hProcess = 0 Then Exit Function
If ReadProcessMemory(hProcess, APIPtr, CmdLinePtr, 4, lRet2) <> 1 Then Exit Function '得到7C88B5D4
If ReadProcessMemory(hProcess, CmdLinePtr, CmdLinePtr, 4, lRet2) <> 1 Then Exit Function '再取个指针(竟是两个指针-_-!)
If ReadProcessMemory(hProcess, CmdLinePtr, CmdLineByte(0), 512, lRet2) <> 1 Then Exit Function '拉一块内存过来
CmdLineStr = StrConv(CmdLineByte, vbUnicode) '处理一下,可以输出了
CmdLineStr = Left$(CmdLineStr, InStr(1, CmdLineStr, vbNullChar) - 1)
GetRemoteCmdLine = CmdLineStr
End Function