Reputation: 342
I'm writing an assembly program (MASM compatible) for win64 and want to use the C function mainCRTStartup() if possible to set up the environment and pass the command line arguments. Can I do that somehow?
When you run a (console) C program, mainCRTStartup() is the first function to be called and it calls the program's main function. Where does the mainCRTStartup() function come from? Is it added by the compiler or the linker, or is it contained in the MSVCRT.DLL? I'm not using the Microsoft linker, I use JWasm and JWlink.
I know perfectly well how to call ordinary C functions from assembly, but mainCRTStartup() is very special and I'm not even sure it's a C function at all, maybe a windows OS function.
If I can call it somehow I need to pass my own main() along for callback.
Upvotes: 0
Views: 1618
Reputation: 33794
int mainCRTStartup()
have no arguments. so you can not (or not need) direct pass arguments for it. he take command line or by call GetCommandLine
or by calling __[w]getmainargs
to MSVCRT.DLL
or statically linked CRT
code. (very depended from CRT
version)
mainCRTStartup
is EXE
entry point. first code which executed from EXE
but of course not first code which executed in process (all statically dll dependences already loaded and initialized (including MSVCRT.DLL
if application use it )
mainCRTStartup
come from LIB
:libcmt.lib
or msvcrt.lib
or msvcurt.lib
. when you link exe - at first you use some LIBs
and OBJs
as linker input and for EXE
you must set /ENTRY:function
option. you can set this function explicitly, or if you not do this - default will be used:
By default, the starting address is a function name from the C run-time library. The linker selects it according to the attributes of the program, as shown in the following table.
in your case this will be mainCRTStartup
- linker will be search it in all LIBs
and OBJs
which you pass to him as input. if he not found it - you got error unresolved external symbol. if linker found it - he use it code as part of exe. so linker take code of mainCRTStartup
from lib (for example even msvcrt.lib
containing mainCRTStartup
as code, but not as import thunk) and link it to exe.
mainCRTStartup
is not a windows OS function. it just EXE entry point. not more.
I not exactly understand why you try todo. if you want simply use CRT
in own assembly code - you need do next:
_main
or _wmain
(for x86) or main
or wmain
(for
x64)msvcrt.lib
as linker input lib/ENTRY:[w]mainCRTStartup
in project settings or in MASM
END [w]mainCRTStartup
if you want own custom function, which will be executed before mainCRTStartup
- let name it start
- you need change point 3:
/ENTRY:start
in project settings or in MASM
END start
and code of start
start PROC
; do something here
call [w]mainCRTStartup ; no args, can even jump instead call
start ENDP
and [w]mainCRTStartup
alredy call your [w]main
callback. you not need to pass own main
because [w]mainCRTStartup
called [w]main
not by address but by name - so all what you need have [w]main implemented in your code. linker already bind call from [w]mainCRTStartup
to your [w]main
or say about unresolved symbol
Upvotes: 3