|
发表于 2004-9-10 07:37:00
|
显示全部楼层
还是贴一下吧,其实每个程序都是调用的下面的这个函数
#ifdef _WINMAIN_
#ifdef WPRFLAG
int wWinMainCRTStartup(
#else /* WPRFLAG */
int WinMainCRTStartup(
#endif /* WPRFLAG */
#else /* _WINMAIN_ */
#ifdef WPRFLAG
int wmainCRTStartup(
#else /* WPRFLAG */
int mainCRTStartup(
#endif /* WPRFLAG */
#endif /* _WINMAIN_ */
void
)
{
int argc; /* three standard arguments to main */
_TSCHAR **argv;
_TSCHAR **envp;
int argret;
int mainret;
int managedapp;
#ifdef _WINMAIN_
_TUCHAR *lpszCommandLine;
STARTUPINFO StartupInfo;
BOOL inDoubleQuote=FALSE;
#endif /* _WINMAIN_ */
_startupinfo startinfo;
/*
* Determine if this is a managed application
*/
managedapp = check_managed_app();
/*
* Guard the initialization code and the call to user's main, or
* WinMain, function in a __try/__except statement.
*/
__try {
/*
* Set __app_type properly
*/
#ifdef _WINMAIN_
__set_app_type(_GUI_APP);
#else /* _WINMAIN_ */
__set_app_type(_CONSOLE_APP);
#endif /* _WINMAIN_ */
/*
* Mark this module as an EXE file so that atexit/_onexit
* will do the right thing when called, including for C++
* d-tors.
*/
__onexitbegin = __onexitend = (_PVFV *)(-1);
/*
* Propogate the _fmode and _commode variables to the DLL
*/
*_IMP___FMODE = _fmode;
*_IMP___COMMODE = _commode;
#ifdef _M_IX86
/*
* Set the local copy of the Pentium FDIV adjustment flag
*/
_adjust_fdiv = * _imp___adjust_fdiv;
#endif /* _M_IX86 */
/*
* Run the RTC initialization code for this DLL
*/
#ifdef _RTC
_RTC_Initialize();
#endif /* _RTC */
/*
* Call _setargv(), which will trigger a call to __setargv() if
* SETARGV.OBJ is linked with the EXE. If SETARGV.OBJ is not
* linked with the EXE, a dummy _setargv() will be called.
*/
#ifdef WPRFLAG
_wsetargv();
#else /* WPRFLAG */
_setargv();
#endif /* WPRFLAG */
/*
* If the user has supplied a _matherr routine then set
* __pusermatherr to point to it.
*/
if ( !__defaultmatherr )
__setusermatherr(_matherr);
#ifdef _M_IX86
_setdefaultprecision();
#endif /* _M_IX86 */
/*
* Do runtime startup initializers.
*
* Note: the only possible entry we'll be executing here is for
* __lconv_init, pulled in from charmax.obj only if the EXE was
* compiled with -J. All other .CRT$XI* initializers are only
* run as part of the CRT itself, and so for the CRT DLL model
* are not found in the EXE. For that reason, we call _initterm,
* not _initterm_e, because __lconv_init will never return failure,
* and _initterm_e is not exported from the CRT DLL.
*
* Note further that, when using the CRT DLL, executing the
* .CRT$XI* initializers is only done for an EXE, not for a DLL
* using the CRT DLL. That is to make sure the -J setting for
* the EXE is not overriden by that of any DLL.
*/
_initterm( __xi_a, __xi_z );
#ifdef _RTC
atexit(_RTC_Terminate);
#endif /* _RTC */
/*
* Get the arguments for the call to main. Note this must be
* done explicitly, rather than as part of the dll's
* initialization, to implement optional expansion of wild
* card chars in filename args
*/
#ifdef WPRFLAG
startinfo.newmode = _newmode;
argret = __wgetmainargs(&argc, &argv, &envp,
_dowildcard, &startinfo);
#else /* WPRFLAG */
startinfo.newmode = _newmode;
argret = __getmainargs(&argc, &argv, &envp,
_dowildcard, &startinfo);
#endif /* WPRFLAG */
if (argret < 0)
_amsg_exit(_RT_SPACEARG);
/*
* do C++ constructors (initializers) specific to this EXE
*/
_initterm( __xc_a, __xc_z );
#ifdef _WINMAIN_
/*
* Skip past program name (first token in command line).
* Check for and handle quoted program name.
*/
#ifdef WPRFLAG
/* OS may not support "W" flavors */
if (_wcmdln == NULL)
return 255;
lpszCommandLine = (wchar_t *)_wcmdln;
#else /* WPRFLAG */
lpszCommandLine = (unsigned char *)_acmdln;
#endif /* WPRFLAG */
while (*lpszCommandLine > SPACECHAR ||
(*lpszCommandLine&&inDoubleQuote)) {
/*
* Flip the count from 1 to 0 or 0 to 1 if current character
* is DOUBLEQUOTE
*/
if (*lpszCommandLine==DQUOTECHAR) inDoubleQuote=!inDoubleQuote;
#ifdef _MBCS
if (_ismbblead(*lpszCommandLine)) {
if (lpszCommandLine) {
lpszCommandLine++;
}
}
#endif /* _MBCS */
++lpszCommandLine;
}
/*
* Skip past any white space preceeding the second token.
*/
while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR)) {
lpszCommandLine++;
}
StartupInfo.dwFlags = 0;
GetStartupInfo( &StartupInfo );
#ifdef WPRFLAG
mainret = wWinMain(
#else /* WPRFLAG */
mainret = WinMain(
#endif /* WPRFLAG */
GetModuleHandleA(NULL),
NULL,
lpszCommandLine,
StartupInfo.dwFlags & STARTF_USESHOWWINDOW
? StartupInfo.wShowWindow
: SW_SHOWDEFAULT
);
#else /* _WINMAIN_ */
#ifdef WPRFLAG
__winitenv = envp;
mainret = wmain(argc, argv, envp);
#else /* WPRFLAG */
__initenv = envp;
mainret = main(argc, argv, envp);
#endif /* WPRFLAG */
#endif /* _WINMAIN_ */
if ( !managedapp )
exit(mainret);
_cexit();
}
__except ( _XcptFilter(GetExceptionCode(), GetExceptionInformation()) )
{
/*
* Should never reach here
*/
mainret = GetExceptionCode();
if ( !managedapp )
_exit(mainret);
_c_exit();
} /* end of try - except */
return mainret;
}
|
|