crt0.c
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 7k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /***
  2. *crt0.c - C runtime initialization routine
  3. *
  4. *       Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       This the actual startup routine for console apps.  It calls the
  8. *       user's main routine main() after performing C Run-Time Library
  9. *       initialization.
  10. *
  11. *       (With ifdef's, this source file also provides the source code for
  12. *       wcrt0.c, the startup routine for console apps with wide characters,
  13. *       wincrt0.c, the startup routine for Windows apps, and wwincrt0.c,
  14. *       the startup routine for Windows apps with wide characters.)
  15. *
  16. *       #define _WINMAIN_ for Windows apps
  17. *       #define WPRFLAG for wide versions
  18. *
  19. *******************************************************************************/
  20. #ifdef _MBCS
  21. #undef _MBCS
  22. #endif
  23. #include <cruntime.h>
  24. #include <dos.h>
  25. #include <internal.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <rterr.h>
  29. #include <oscalls.h>
  30. #include <awint.h>
  31. #include <tchar.h>
  32. #include <excpt.h>
  33. extern CRITICAL_SECTION s_cs;
  34. // wWinMain is not yet defined in winbase.h. When it is, this should be
  35. // removed.
  36. int
  37. WINAPI
  38. wWinMain(
  39.     HINSTANCE hInstance,
  40.     HINSTANCE hPrevInstance,
  41.     LPWSTR lpCmdLine,
  42.     int nShowCmd
  43.     );
  44. int __cdecl _XcptFilter (
  45.         unsigned long xcptnum,
  46.         PEXCEPTION_POINTERS pxcptinfoptrs
  47.         );
  48. #define SPACECHAR   _T(' ')
  49. #define DQUOTECHAR  _T('"')
  50. // command line, environment, and a few other globals
  51. #ifdef WPRFLAG
  52. wchar_t *_wcmdln;           /* points to wide command line */
  53. #else
  54. char *_acmdln;              /* points to command line */
  55. #endif
  56. /***
  57. *BaseProcessStartup(PVOID Peb)
  58. *
  59. *Purpose:
  60. *       This routine does the C runtime initialization, calls main(), and
  61. *       then exits.  It never returns.
  62. *
  63. *Entry:
  64. *       PVOID Peb - pointer to Win32 Process Environment Block (not used)
  65. *
  66. *Exit:
  67. *       This function never returns.
  68. *
  69. *******************************************************************************/
  70. #ifdef _WINMAIN_
  71. #ifdef WPRFLAG
  72. void wWinMainCRTStartup(
  73. #else
  74. void WinMainCRTStartup(
  75. #endif
  76. #else // _WINMAIN_
  77. #ifdef WPRFLAG
  78. void wmainCRTStartup(
  79. #else
  80. void mainCRTStartup(
  81. #endif
  82. #endif // _WINMAIN_ 
  83.         void
  84.         )
  85. {
  86.         int mainret;
  87. #ifdef _WINMAIN_
  88.         _TUCHAR *lpszCommandLine;
  89.         STARTUPINFO StartupInfo;
  90. #endif
  91.         __error_mode = _OUT_TO_DEFAULT;
  92. #ifdef  _WINMAIN_
  93.         __app_type = _GUI_APP;
  94. #else
  95.         __app_type = _CONSOLE_APP;
  96. #endif
  97.         /*
  98.          * Get the full Win32 version
  99.          */
  100.         _osver = GetVersion();
  101.         _winminor = (_osver >> 8) & 0x00FF ;
  102.         _winmajor = _osver & 0x00FF ;
  103.         _winver = (_winmajor << 8) + _winminor;
  104.         _osver = (_osver >> 16) & 0x00FFFF ;
  105.         InitializeCriticalSection(&s_cs);
  106. #ifdef  _MT
  107.         if( !_mtinit() )                    /* initialize multi-thread */
  108.             _exit(_RT_THREAD);               /* write message and die */
  109. #endif
  110.         /*
  111.          * Guard the remainder of the initialization code and the call
  112.          * to user's main, or WinMain, function in a __try/__except
  113.          * statement.
  114.          */
  115.         __try {
  116. #if defined(_MBCS)
  117.             /*
  118.              * Initialize multibyte ctype table. Always done since it is
  119.              * needed for startup wildcard and argv processing.
  120.              */
  121.             __initmbctable();
  122. #endif
  123. #ifdef WPRFLAG
  124.             _wenvptr = NULL;   // points to wide environment block
  125.             
  126.             /* get wide cmd line info */
  127.             _wcmdln = (wchar_t *)GetCommandLineW();
  128.             /* get wide environ info */
  129.             _wenvptr = (wchar_t *)GetEnvironmentStringsW();
  130.             if ((_wcmdln == NULL) || (_wenvptr == NULL)) {
  131.                 exit(-1);
  132.             }
  133.             _wsetargv();
  134.             _wsetenvp();
  135. #else
  136.             _aenvptr = NULL;      // points to environment block
  137.             
  138.             /* get cmd line info */
  139.             _acmdln = (char *)GetCommandLineA();
  140.             /* get environ info */
  141.             _aenvptr = (char *)GetEnvironmentStringsA();
  142.             if ((_aenvptr == NULL) || (_acmdln == NULL)) {
  143.                 exit(-1);
  144.             }
  145.             _setargv();
  146.             _setenvp();
  147. #endif  // WPRFLAG
  148.             _cinit();                       /* do C data initialize */
  149. #ifdef _WINMAIN_
  150.             /*
  151.              * Skip past program name (first token in command line).
  152.              * Check for and handle quoted program name.
  153.              */
  154. #ifdef WPRFLAG
  155.             lpszCommandLine = (wchar_t *)_wcmdln;
  156. #else
  157.             lpszCommandLine = (unsigned char *)_acmdln;
  158. #endif
  159.             if ( *lpszCommandLine == DQUOTECHAR ) {
  160.                 /*
  161.                  * Scan, and skip over, subsequent characters until
  162.                  * another double-quote or a null is encountered.
  163.                  */
  164.                 while ( (*(++lpszCommandLine) != DQUOTECHAR)
  165.                     && (*lpszCommandLine != _T('')) ) {
  166. #ifdef _MBCS
  167.                         if (_ismbblead(*lpszCommandLine))
  168.                             lpszCommandLine++;
  169. #endif
  170.                 }
  171.                 /*
  172.                  * If we stopped on a double-quote (usual case), skip
  173.                  * over it.
  174.                  */
  175.                 if ( *lpszCommandLine == DQUOTECHAR )
  176.                     lpszCommandLine++;
  177.             }
  178.             else {
  179.                 while (*lpszCommandLine > SPACECHAR)
  180.                     lpszCommandLine++;
  181.             }
  182.             /*
  183.              * Skip past any white space preceeding the second token.
  184.              */
  185.             while (*lpszCommandLine && (*lpszCommandLine <= SPACECHAR)) {
  186.                 lpszCommandLine++;
  187.             }
  188.             StartupInfo.dwFlags = 0;
  189.             GetStartupInfo( &StartupInfo );
  190. #ifdef WPRFLAG
  191.             mainret = wWinMain( GetModuleHandle(NULL),
  192. #else
  193.             mainret = WinMain( GetModuleHandle(NULL),
  194. #endif
  195.                                NULL,
  196.                                lpszCommandLine,
  197.                                StartupInfo.dwFlags & STARTF_USESHOWWINDOW
  198.                                     ? StartupInfo.wShowWindow
  199.                                     : SW_SHOWDEFAULT
  200.                              );
  201. #else /* WIN_MAIN */
  202. #ifdef WPRFLAG
  203.             __winitenv = _wenviron;
  204.             mainret = wmain(__argc, __wargv, _wenviron);
  205. #else
  206.             __initenv = _environ;
  207.             mainret = main(__argc, __argv, _environ);
  208. #endif
  209. #endif /* WIN_MAIN */
  210.             exit(mainret);
  211.         }
  212.         __except ( _XcptFilter(GetExceptionCode(), GetExceptionInformation()) )
  213.         {
  214.             /*
  215.              * Should never reach here
  216.              */
  217.             _exit( GetExceptionCode() );
  218.         } /* end of try - except */
  219. }