1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
如何取得指定进程的命令行参数(VB6.0,马大哈原创) [ 2007-06-19
| 作者:马大哈 | 来自:本站原创 ]
之前在CSDN里遇到这种需求的
我本来想的是,在别的进程里调用GetCommandLine函数,并取返回值...
结果一GOOGLE,发现了一个更简单的办法,详见:
http://www.vckbase.com/document/viewdoc/?id=1685
于是就参照里面的思路,改成了VB的代码.
中间遇到的阻力,就是汇编代码里的那两个指针问题...
2003下GetCommandLineA的代码,反出来是这样:
mov eax,dword ptr [7C88B5D4]
ret
而这7C88B5D4所指的内存,竟又是一个指针...
由于是第一次弄ASM,很是费了点事
好在后来知道了,就搞定也....
以下是函数代码:
Public Function GetRemoteCmdLine(ByVal hPId As Long) As String
'返回指定进程的命令行
'hPId - 目标进程PID
'返回值:
' 成功返回命令行,失败返回空字符串
Dim hDll As Long, hProcess As Long, APIPtr As Long, CmdLinePtr As Long, lRet As Long, lRet2 As Long
Dim CmdLineStr As String, CmdLineByte(511) As Byte
GetRemoteCmdLine = ""
hDll = LoadLibrary(StrPtr(kernel32)): Debug.Assert hDll
APIPtr = GetProcAddress(hDll, "GetCommandLineA") + 1 '取得GetCommandLineA地址 + 1
'kernel32.dll中的反汇编代码(Win2003版):
'mov eax,dword ptr [7C88B5D4]
'机器码:
'A1D4B5887C
'+1跳过mov指令,后面4个字节就是指向命令行的指针
'这个地址在每个进程里都是一样的,可以直接使用
Call FreeLibrary(hDll)
hProcess = OpenProcess(PROCESS_VM_READ, 0, hPId) '打开进程
If hProcess = 0 Then Exit Function
lRet = ReadProcessMemory(hProcess, APIPtr, CmdLinePtr, 4, lRet2) '得到7C88B5D4
If lRet <> 1 Then Exit Function
lRet = ReadProcessMemory(hProcess, CmdLinePtr, CmdLinePtr, 4, lRet2) '再取个指针(竟是两个指针-_-!)
If lRet <> 1 Then Exit Function
lRet = ReadProcessMemory(hProcess, CmdLinePtr, CmdLineByte(0), 512, lRet2) '拉一块内存过来
If lRet <> 1 Then Exit Function
CmdLineStr = StrConv(CmdLineByte, vbUnicode) '处理一下,可以输出了
CmdLineStr = Mid(CmdLineStr, 1, InStr(1, CmdLineStr, Chr(0), vbTextCompare) - 1)
Debug.Print CmdLineStr
GetRemoteCmdLine = CmdLineStr
End Function
下载地址:
点此打下载 [本日志由 马大哈 于 2009-06-03 01:51 AM 编辑]
马大哈 于 2011-05-13 11:23 AM 发表评论:
引用内容
最初由 tbage 发表于 2011-04-17 01:54 PM:
老马写的..可惜没的附件了..谁有发个我啊,谢谢tbage@163.com
已经发送了附件到你的邮箱中
tbage 于 2011-04-17 01:54 PM 发表评论:
老马写的..可惜没的附件了..
谁有发个我啊,谢谢
tbage@163.com
马大哈 于 2008-06-12 05:52 PM 发表评论:
sutuo 于 2008-06-11 03:33 PM 发表评论:
win4sm 于 2007-06-19 10:14 PM 发表评论:
不错不错