关于GetCommandLine获得命令行缓冲区数据的问题
关于GetCommandLine获得命令行缓冲区数据的问题
关于Api函数GetCommandLine获得进程指向命令行缓冲区的指针数据的问题,GetCommandLine函数用了这么久了,却没有注意到在获得的包含路径的命令行的结尾还有一个 空格字符,
今天用OD调试一程序才发现。在调试器中取得的命令行长度比直接运行的多一个字符。
测试代码:
.386
.model flat,stdcall
option casemap:none
include windows.inc
include kernel32.inc
include user32.inc
include masm32.inc
includelib kernel32.lib
includelib user32.lib
includelib masm32.lib
.data?
buffer db 50 dup(?)
szBuffer db 500 dup(?)
.code
szFormat db "(%s)%x",0
sHexToTxt proc uses ebx ecx edx esi edi addr1:DWORD,addr2:DWORD
local @buf1[10]:BYTE
local @buf2[10]:BYTE
invoke lstrlen,addr1
mov ecx,eax
mov esi,addr1
mov edi,addr2
.while ecx>0
push ecx
push esi
push edi
mov @buf1[0],0
mov ebx,0
mov bl,byte ptr [esi]
mov @buf2[0],bl
mov @buf2[1],0
invoke wsprintf,addr @buf1,addr szFormat,addr @buf2,ebx
invoke lstrlen,addr @buf1
mov ecx,eax
pop edi
lea esi,@buf1
rep movsb
mov byte ptr [edi],"_"
inc edi
pop esi
pop ecx
inc esi
dec ecx
.endw
ret
sHexToTxt endp
start:
invoke GetCommandLine ;获得命令行字符串指针
push eax
invoke sHexToTxt,eax,addr szBuffer
invoke MessageBox,0,addr szBuffer,0,0
pop eax
mov esi,eax
mov edi,eax ;保存到edi
mov ecx, -1 ;计数器
mov al, 0 ;查找0(结束符)
push edi
repnz scasb ;查找结束符
not ecx ;返回整个字符串长度
pop edi
mov al, 20h ;查找20h(空格)
repnz scasb ;查找
sub ebx,ecx
dec ecx ;获得命令行字符串指针
test ecx,ecx ;是否有效
add esi,ebx
lea edi,buffer
rep movsb
invoke MessageBox,0,addr buffer,0,0
invoke ExitProcess,0
end start