Parsing commandline with __getmainargs from msvcrt

Started by bozo, April 06, 2005, 12:27:33 PM

Previous topic - Next topic

bozo


comment ~

This is just for those of you who haven't yet
used __getmainargs from MSVCRT.DLL

i just thought it'd be worth a look for some of you.

It'll save you the hassle of using GetCommandLineA
or GetCommandLineW being parsed with CommandLineToArgvW
which is unicode only..

There is __getmainargsw for unicode.

The INCLUDELIB directive is in each *.INC file.
MACROS.ASM is also placed in the INCLUDE directory.

Use latest version of MASM.

ml /Cp /coff /c /IC:\MASM32\INCLUDE showargs.asm
link /SUBSYSTEM:CONSOLE /LIBPATH:C:\MASM32\LIB showargs.obj

Example:

C:\showargs this is "a test for" __getmainargs

Argv[0]: showargs
Argv[1]: this
Argv[2]: is
Argv[3]: a test for
Argv[4]: __getmainargs

The fourth arguement of __getmainargs is an option for wildcard..either TRUE or FALSE
Say you wanted to include all the files in the current directory as parameters,
then you would set the fourth __getmainargs arguement to TRUE and supply "*" in commandline.

Or you could have only files of a particular extension, like *.asm

If it can't find any asm files in the current directory, it'll display
the arguement as supplied, as in that example would be "*.asm"

Anyone writing console apps with 32-bit assembly would probably prefer to use
__getmainargs than other parsers.

It definitely makes the task a whole lot easier without prewritten code.
~
.586
.model flat,stdcall

include <windows.inc>
include <kernel32.inc>
include <msvcrt.inc>
include <macros.asm>

main proto :dword, :dword

.data

bWildCard dd 0

pEnv dd 0 ; environment variables
pArgv dd 0 ; arguement vectors
nArgc dd 0 ; arguement count

stinfo STARTUPINFO <?>

.code

start:
mov bWildCard, FALSE
invoke __getmainargs,addr nArgc,addr pArgv,addr pEnv,bWildCard,addr stinfo

invoke main,nArgc,pArgv

exit

main proc argc:dword, argv:dword
mov ebx, argv
mov edi, argc
xor esi, esi

.while (edi >= 1)
invoke printf,
chr$(10,'Argv[%d]: %s'),
esi,
dword ptr [ebx + 4 * esi]
dec edi
inc esi
.endw
ret
main endp

end start


The code tags seem to have problems displaying TABs?..doesn't matter now.

Vortex


bozo

Yeah, I know i only post it now, but i did use it since around march/april last year.

Vortex


Vortex

A quick example with CommandLineToArgvW :

.386
.model flat,stdcall
option casemap : none

include     UnicodeCmdLine.inc

.data
format1     db 'Command line parameter = %s',13,10,0

.code

start:

    call    main
    invoke  ExitProcess,0

main PROC uses esi edi ebx

LOCAL argc:DWORD
LOCAL argv:DWORD
LOCAL buffer[128]
LOCAL buffer2[160]

    invoke  GetCommandLineW
    lea     edx,argc
    invoke  CommandLineToArgvW,eax,edx
    mov     ebx,argc
    mov     argv,eax

ProcessParam:

    mov     eax,argv
    mov     esi,DWORD PTR [eax]
    lea     edi,buffer
    xor     ecx,ecx
    sub     ecx,1
@@:
    add     ecx,1
    mov     al,BYTE PTR [esi+2*ecx]
    mov     BYTE PTR [edi+ecx],al
    test    al,al
    jnz     @b
    invoke  wsprintf,ADDR buffer2,ADDR format1,edi
    invoke  StdOut,ADDR buffer2
    add     argv,4
    sub     ebx,1
    jnz     ProcessParam
    ret   

main ENDP       

END start

[attachment deleted by admin]