Here is an example demonstrating how to obtain easily the command line parameters. I used the
__getmainargs function from the msvcrt library.You know this function is used mostly by the C/C++
compilers.I don't pretend that this is the best method but still the use of this function appears to be
practical.




format PE CONSOLE 4.0

include 'include\macro\stdcall.inc'
include 'include\macro\import.inc'

section '.data' data readable writeable

argc dd ?
argv dd ?
env dd ?
msg db "Command=%s P1=%s P2=%s",0
errmsg db "I need only two parameters!",0
section '.code' code readable executable

entry start

start:
invoke __getmainargs,argc,argv,env,0
cmp [argc],3
jne error
mov esi,[argv]

invoke printf,msg,dword [esi],dword [esi+4],dword [esi+8]
add esp,16

finish:
invoke ExitProcess,0

error:
invoke printf,errmsg
add esp,4
jmp finish

section '.idata' import data readable writeable

library kernel,'KERNEL32.DLL',\
msvcrt,'msvcrt.dll'

kernel:
import ExitProcess,'ExitProcess'

msvcrt:
import __getmainargs,'__getmainargs',\
printf,'printf'

Posted on 2002-12-11 03:02:50 by Vortex
Very nice example on how to use the C library using assembly...

I didn't know about this function...
The best would be to rewrite this function (C lib source code comes with VC++ to do its assembly equivalent) so it can directly be integrated to the executable...
Posted on 2002-12-11 03:34:19 by JCP
Thanks Readiosys! I searched the __getmainargs function on the web,but the best information I have got:

http://sources.redhat.com/ml/cygwin/1998-07/msg00686.html

Yes,if somebody could help me how to get the full descriptions of the functions from msvcrt.dll (plus crtdll.dll)...
I would be very grateful.

Regards,

Vortex
Posted on 2002-12-11 03:49:56 by Vortex
I will try to find the source code when I will get back to home (in something like 11 hours :( )
Posted on 2002-12-11 04:05:27 by JCP
Merci Readiosys. Si je comprends bien,tu as beaucoup de travaux,non?
Posted on 2002-12-11 04:15:51 by Vortex
I would also like to get more information on those DLLs ;)
Regards
Posted on 2002-12-11 07:49:00 by POW
Yes, Vortex, you are right: I didn't even had the time to boot up my personal PC (doh).
I'm sorry but you will have to wait a bit longer for the sources...

Regards,
Posted on 2002-12-12 01:07:08 by JCP
Is it really safe to use C-library dll:s ? Are they included in all versions of windows?
Posted on 2002-12-12 03:36:23 by Delight

Is it really safe to use C-library dll:s ? Are they included in all versions of windows?


Why not Delight? Is there any alternative for the C/C++ compilers? MS VC++,Lcc,etc.. can't survive without
C-library dlls.Even,Win95 OSR2 contains the msvcrt.dll. They are safe to use if you take care of stack balancing.
For win32asm,they are sometimes really practical to use :)

Regards,

Vortex
Posted on 2002-12-12 03:58:40 by Vortex
I guess you're right, amigo. I'll start exploring those dll:s right away. Thanks
Posted on 2002-12-13 03:14:18 by Delight
Oops! A very small correction:



invoke __getmainargs,argc,argv,env,0
add esp,16 ; <---- This one should be added


Regards,

Vortex
Posted on 2002-12-14 04:37:29 by Vortex

Here is an example demonstrating how to obtain easily the command line parameters. I used the
__getmainargs function from the msvcrt library.You know this function is used mostly by the C/C++
compilers.I don't pretend that this is the best method but still the use of this function appears to be
practical.




format PE CONSOLE 4.0

include 'include\macro\stdcall.inc'
include 'include\macro\import.inc'

section '.data' data readable writeable

argc dd ?
argv dd ?
env dd ?
msg db "Command=%s P1=%s P2=%s",0
errmsg db "I need only two parameters!",0
section '.code' code readable executable

entry start

start:
invoke __getmainargs,argc,argv,env,0
cmp [argc],3
jne error
mov esi,[argv]

invoke printf,msg,dword [esi],dword [esi+4],dword [esi+8]
add esp,16

finish:
invoke ExitProcess,0

error:
invoke printf,errmsg
add esp,4
jmp finish

section '.idata' import data readable writeable

library kernel,'KERNEL32.DLL',\
msvcrt,'msvcrt.dll'

kernel:
import ExitProcess,'ExitProcess'

msvcrt:
import __getmainargs,'__getmainargs',\
printf,'printf'













I do not have these two files in my masm32 directory?
include 'include\macro\stdcall.inc'
include 'include\macro\import.inc'

How canI get these two files?

Thanks :)
Posted on 2003-06-25 14:51:17 by gorge
Gorge,

You cand find the include files stdcall.inc and import.inc in the Fasmw (Fasm Windows Version) package.
You need only to correct the paths pointing these two include files.

Visit Privalov's web site to get the latest releases of Fasm.

http://fasm.sourceforge.net

Regards,

Vortex
Posted on 2003-06-26 03:05:27 by Vortex
Hi Vortex,

How did you find those C functions parameters? For example,

__getmainargs

you passed,

invoke __getmainargs,argc,argv,env,0

i suppose the last zero is "envp" null terminator. But is there any other document for this?

Thanks
Posted on 2003-06-28 16:50:00 by cakmak
Cakmak,

Did you read my reply to Readiosys in this thread? :)

The documentation from:

http://sources.redhat.com/ml/cygwin/1998-07/msg00686.html
Posted on 2003-06-29 03:53:18 by Vortex
Ok thanks Vortex,

Before i saw your reply, i thought to debug a really C executable which i written. This is a very big helper(A very big thanks to OllyDbg also gives info about ST() stuff) but surelly not best way. Anyway i can not even think this kind of help for other languages :) , now i am cheating with debug codes.

greets
Posted on 2003-06-29 20:27:35 by cakmak
Btw, i haven't compiled your source(v1.47) fasm giving an error. i was tested these C functions in tasm. Anyway, at this lines,

kernel:
import ExitProcess,'ExitProcess'

error invalid expression.
description: if used 'ExitProcess'

and here,

macro import name,
{ common
name:
forward
if used label ;<<<< this line
local _label
label dd RVA _label
end if
common
if $ > name
name#.needed = FALSE
dd 0
else

instruction: if used 'ExitProcess'


any guess? I dont understand these macros.

Thanks
Posted on 2003-06-29 22:20:03 by cakmak
Cakmak,

Here is the __getmainargs demo reassembled with Fasm 1.47
Now,you need the include files from the Fasmw package.

format PE CONSOLE 4.0


include '%include%\win32a.inc'

section '.data' data readable writeable

argc dd ?
argv dd ?
env dd ?
msg db "Command=%s P1=%s P2=%s",0
errmsg db "I need only two parameters!",0
section '.code' code readable executable

entry start

start:
cinvoke __getmainargs,argc,argv,env,0
cmp [argc],3
jne error
mov esi,[argv]

cinvoke printf,msg,dword [esi],dword [esi+4],dword [esi+8]

finish:
invoke ExitProcess,0

error:
cinvoke printf,errmsg
jmp finish

section '.idata' import data readable writeable

library kernel,'KERNEL32.DLL',\
msvcrt,'msvcrt.dll'

import kernel,\
ExitProcess,'ExitProcess'


import msvcrt,\
__getmainargs,'__getmainargs',\
printf,'printf'


Build.bat:



set include=\fasmw\include
\fasm\fasm Cmdline.asm Cmdline.exe
Posted on 2003-06-30 05:35:24 by Vortex
Thank You Vortex,

only invoke -> cinvoke? What kind of error? Who may guess? :)

greets
Posted on 2003-06-30 20:49:43 by cakmak
...and i suppose, i found a restrict in scanner v3.0! Can't we add more dll names in scan.ini? Because i tried to adding msvcrt.dll but last char('l') is crippled. Btw, very good tool:alright:

Regards
Posted on 2003-06-30 21:12:52 by cakmak