News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Command line arguments

Started by loki_dre, May 22, 2009, 07:07:22 PM

Previous topic - Next topic

loki_dre

How do i process command line arguments?
ie. int main( int argc[ , char *argv[ ] [, char *envp[ ] ] ] );


Vortex


PBrennick

You can do something like the following:

    invoke  GetCommandLine
    mov     CommandLine, eax
    invoke  PathUnquoteSpaces, CommandLine  ; Parse the command line, remove chaff
    invoke  PathGetArgs, CommandLine    ; Get the command line arguments
    mov     CommandLine, eax
    invoke  PathUnquoteSpaces, CommandLine  ; Parse the Argument, remove chaff
    invoke  CopyCat, addr szArgument, CommandLine, NULL
    .if(szArgument[0] == 0)
      jmp     failed
    .endif
    invoke  CopyCat, addr szArgument, NULL, NULL


PathUnquoteSpaces needs to be used twice in case there are any spaces in the path portion of the command line. I am using that code in ProjectBuilder with no problems.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

loki_dre

What is the declaration for "szArgument"?  It's not a DWORD is it?

PBrennick


szArgument      db  MAX_PATH*50 dup(?)  ; Command Line Argument Buffer

Paul
The GeneSys Project is available from:
The Repository or My crappy website

BlackVortex

Anything that begins with "sz" is usually a pointer to a zero-terminated string.

PBrennick

You got it.  :thumbu

There is a prefix conventions list if anyone doesn't already have it, I can post it. Conventions, in my opinion, are very important. In this case, Black Vortex new exactly what the declaration should be because of the prefix in the label.

Another popular one would be the 'h' in hWnd which means it is a handle so it is scoped DWORD.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

mitchi

LP means Long Pointer. It's used a lot.

LPWSTR -> Pointer to UNICODE string
LPCSTR -> Pointer to Multi-byte string (ASCII)

LPDWORD -> Pointer to a DWORD memory location. Usually used for OUT parameters of functions.

jj2007

Quote from: loki_dre on May 22, 2009, 07:07:22 PM
How do i process command line arguments?
ie. int main( int argc[ , char *argv[ ] [, char *envp[ ] ] ] );

The Masm32 library has a handy routine, too:

include \masm32\include\masm32rt.inc

.data
ComLine1   db "This is the command line: "
ComLine2   db 128 dup(0)

.code
start:
invoke GetCL, 1, offset ComLine2 ; 1=first argument
.if eax==1 ; 1=valid argument
MsgBox 0, offset ComLine1, "Congrats:", MB_OK
.else
MsgBox 0, str$(eax), "No command line? Error:", MB_OK
.endif
exit

end start

PBrennick

JJ,

GetCL is nice but sure is a lot more work than just the few lines I employ using APIs. It is better to avoid APIs whenever possible though so I cannot say which (if either) is better. The thing I like about GetCL is that it parses for multiple arguments. Have you a method of your own? Just curious, I always planned to write my own for GeneSys but never got around to it. Might be a nice project. You could write a algo and donate it to GeneSys.  :8)

I am going to need a multiple argument parser because the rewrite of the GeneSys editor is going to lose the builder options and will invoke the ProjectBuilder externally. This means I will need to add command line options to pattern the usage of the builder.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

sinsi

I like using CommandLineToArgvW

include \masm32\include\masm32rt.inc

.data?
stdout  dd ?
argc    dd ?
argv    dd ?
temp    dd ?

.const
crlf dw 13,10,0,0

.code

uniprintstring:
        push ebx
        mov ebx,eax
        invoke lstrlenW,eax
        invoke WriteConsoleW,stdout,ebx,eax,OFFSET temp,0
        pop ebx
        ret
       
start:  invoke GetStdHandle,STD_OUTPUT_HANDLE
        mov stdout,eax
        invoke GetCommandLineW
        invoke CommandLineToArgvW,eax,OFFSET argc
        mov argv,eax

        mov edi,eax
        mov ebx,argc

@@:     test ebx,ebx
        jz @f
        mov eax,[edi]
        call uniprintstring
        mov eax,OFFSET crlf
        call uniprintstring
        dec ebx
        add edi,4
        jmp @b

@@:     
        inkey
        exit

end start
Light travels faster than sound, that's why some people seem bright until you hear them.

PBrennick

Thank you Sinsi,

That looks like it will solve my problem quite nicely. Clean and simple looking, also, considering what it accomplishes.

Paul
The GeneSys Project is available from:
The Repository or My crappy website

sinsi

Yeah, the only thing to remember is that it's all unicode - the print macro didn't work too well  :bdg
Light travels faster than sound, that's why some people seem bright until you hear them.

PBrennick

The print macro is just to show what happens. I am not concerned about it nor do I need that part. Thanx.

Paul
The GeneSys Project is available from:
The Repository or My crappy website