News:

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

Command Line Arguments

Started by MusicalMike, November 24, 2005, 07:59:53 AM

Previous topic - Next topic

MusicalMike

I am going to be writing an app soon that requires access to the command line arguments passed to it so naturally I created some hello world code to get familior with reading command line arguments. It took me for ever to get the code to work. No matter what I did, I kept getting weird problems. Finally, fed up with fighting with the api, I took this bruit force approach to getting the command line arguments.


.486                                     
.model flat, stdcall                     
option casemap :none                     

include shell32.inc
include windows.inc
include kernel32.inc
include user32.inc
include masm32.inc
include msvcrt.inc
   
includelib shell32.lib
includelib kernel32.lib
includelib user32.lib
includelib masm32.lib
includelib msvcrt.lib

.data
path db 1000 dup(00h)
buf db 1000 dup(00h)
leng dd 00h
args dd 00h

.code


Start:
push offset path
push 1000
call GetCurrentDirectory
push offset path
call crt_strlen
mov leng, eax
add leng, 12 ; the number 12 assumes the name of the exe is 8 chars long
call GetCommandLine
push eax
push offset buf
call crt_strcpy
mov eax, leng
push leng
push 0FFh
push offset buf
call crt__strnset
push 0FFh
push offset buf
call crt_strrchr
mov args, eax
add args, 1
push args
call StdOut
push 5000
call Sleep
push 00h
call ExitProcess

End Start


Though the code above does indeed work, I can't help it feel that there should be a better alternative to this brute force method. I would appreciate any assistance.

MusicalMike


Grincheux

Try this :

PathGetArgs Function 

--------------------------------------------------------------------------------

Finds the command line arguments within a given path.

Syntax

LPTSTR PathGetArgs(          LPCTSTR pszPath
);
Parameters

pszPath
[in] Pointer to a null-terminated string of maximum length MAX_PATH that contains the path to be searched.
Return Value

Returns a pointer to a null-terminated string containing the arguments portion of the path if successful; if there are no arguments the function returns a pointer to the end of the input string. If the function is given a NULL argument it returns NULL.

Example


Hide Example

#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"

void main( void )
{
// Path_1 to search for file arguments (2-Args):
char buffer_1[ ] = "test.exe temp.txt sample.doc";
char *lpStr1;
lpStr1 = buffer_1;

// Path_2 to search for file arguments (3-Args):
char buffer_2[ ] = "test.exe 1 2 3";
char *lpStr2;
lpStr2 = buffer_2;

// Path_3 to search for file arguments (3-Args):
char buffer_3[ ] = "test.exe sample All 15";
char *lpStr3;
lpStr3 = buffer_3;

// Path_4 to search for file arguments (zero args):
char buffer_4[ ] = "test.exe";
char *lpStr4;
lpStr4 = buffer_4;

cout << "The path passed to the function was : " << lpStr1 <<
        "\nThe arg(s)found in path 1 were      : " << PathGetArgs(lpStr1) << endl;

cout << "\nThe path passed to the function was : " << lpStr2 <<
        "\nThe arg(s)found in path 2 were      : " << PathGetArgs(lpStr2) << endl;

cout << "\nThe path passed to the function was : " << lpStr3 <<
        "\nThe arg(s)found in path 3 were      : " << PathGetArgs(lpStr3) << endl;

cout << "\nThe path passed to the function was : " << lpStr4 <<
        "\nThe arg(s)found in path 4 were      : " << PathGetArgs(lpStr4) << endl;
}

OUTPUT:
===========
The path passed to the function was : test.exe temp.txt sample.doc
The arg(s)found in path 1 were      : temp.txt sample.doc

The path passed to the function was : test.exe 1 2 3
The arg(s)found in path 2 were      : 1 2 3

The path passed to the function was : test.exe sample All 15
The arg(s)found in path 3 were      : sample All 15

The path passed to the function was : test.exe
The arg(s)found in path 4 were      :
===========

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/shlwapi/path/pathfileexists.asp
Kenavo

Grincheux
_____________________________________________________
http://www.phrio.biz

MusicalMike

Thanks, but there are some stock functions that come with masm that greately simplify the task. I stumbled accross them after I opened this thread hense my sudden "never mind I figured it out" post. Thanks again though.

Vortex

Hi Mike,

You can use __getmainargs from msvcrt.dll to parse commadline parameters :

http://www.masmforum.com/simple/index.php?topic=1286.0