CRT startup code replacement for console programs, using MSVCRT and CRTDLL DLLs

Started by Antariy, May 03, 2014, 04:16:11 AM

Previous topic - Next topic

Antariy

The following code is the simple replacement for the startup code for console programs, that may be used with MSVC and any other compilers, and allowing to use MSVCRT.DLL or CRTDLL.DLL as a CRT library.


extern __declspec(dllimport) int __cdecl
#ifdef USE_MSVCRTDLL
__getmainargs
#define GETARGS __getmainargs
#else
__GetMainArgs
#define GETARGS __GetMainArgs
#endif
(long*,char***,char***, long, long*);

void _mainCRTStartup(){
long argc;
char** argv;
char** env;
long l;

GETARGS(&argc,&argv,&env,0,&l);

exit(main(argc,argv/*,env*/));
}


If the MSVCRT dll is required, then specify the USE_MSVCRTDLL macro, otherwise the CRTDLL will be used.
If the environment is required then uncomment env parameter passed to a main().
When linking use appropriate import library to the version which specified in the C code using a macro.
If the linker is not Microsoft's one then you should find manually how to exclude the standard libraries from the build and how to set up the entry point manually. For the Microsoft's one that are the commands: /NODEFAULTLIB and /ENTRY appropriately.

Some additional info about CRTDLL import library: http://masm32.com/board/index.php?topic=1418.msg32855#msg32855

Gunther

Alex,

very interesting idea.  :t I've to check the gcc linker options. Thank you.

Gunther
You have to know the facts before you can distort them.