A small algorithm which parses the commandline for a filename.
It can be used for example to open a txt file directly with your own editor, etc.
It checks the following possibilities:

; (1): AppName.exe
; (2): "AppName.exe" CommandLine
; (3): AppName.exe "CommandLine"
; (4): "AppName.exe" "CommandLine"

Are there some more ways and/or errors?





ProcessCommandLine PROC lpCmdLine:DWORD
pushad
mov edi, lpCmdLine
xor ecx, ecx
dec ecx
mov al, [edi]
inc edi
.IF al == 22h
repnz scasb
inc edi
mov al, [edi]
.IF al == 22h
inc edi
push edi
repnz scasb
dec edi
mov byte ptr [edi], 0
pop edi
.ENDIF
.ELSE
@@: inc edi
mov al, [edi]
.IF !al
popad
xor eax, eax
ret
.ENDIF
cmp al, 22h
jnz @B
inc edi
push edi
repnz scasb
dec edi
mov byte ptr [edi], 0
pop edi
.ENDIF
mov lpCmdLine, edi
popad
mov eax, lpCmdLine
ret
ProcessCommandLine ENDP



I knwo the "repnz" are not very fast but the commandline isn't two megabyte big :)

Have a nice day
Posted on 2002-08-21 03:07:53 by Rennsemmel
I wrote some code a year back to parse command line parameters into an array (much like argc, argv[] in c) and handle simple switches. Perhaps it would be of interest ..
Posted on 2002-08-23 15:49:12 by Jibz
This is the code I use:


.data
argc dd ?
argv dd 16 dup (?)
.code
ProcessCmdLine proc
push edi
push esi
push ebx
call GetCommandLine
mov esi, eax
mov edi, OFFSET argv
xor ecx, ecx
xor ebx, ebx
xor edx, edx
@@cmss: mov eax, esi
mov dl, 20h
cmp byte ptr [esi], 22h
sete cl
lea edx, [edx+ecx*2]
add eax, ecx
stosd
@@cm00: inc esi
cmp byte ptr [esi], 0
je @@cm01
cmp byte ptr [esi], dl
jne @@cm00
mov byte ptr [esi], 0
add esi, ecx
inc esi
cmp byte ptr [esi], 0
je @@cm01
inc [argc]
jmp @@cmss
@@cm01: pop ebx
pop esi
pop edi
inc [argc]
ret
ProcessCmdLine endp
Posted on 2002-08-23 17:53:09 by comrade