home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / aw_com.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  2KB  |  106 lines

  1. /***
  2. *aw_com.c - W version of GetCommandLine.
  3. *
  4. *       Copyright (c) 1994-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Use GetCommandLineW if available, otherwise use A version.
  8. *
  9. *******************************************************************************/
  10.  
  11. #include <cruntime.h>
  12. #include <internal.h>
  13. #include <stdlib.h>
  14. #include <setlocal.h>
  15. #include <awint.h>
  16. #include <dbgint.h>
  17.  
  18. #define USE_W   1
  19. #define USE_A   2
  20.  
  21. /***
  22. *LPWSTR __cdecl __crtGetCommandLineW - Get wide command line.
  23. *
  24. *Purpose:
  25. *  Internal support function. Tries to use NLS API call
  26. *  GetCommandLineW if available and uses GetCommandLineA
  27. *  if it must. If neither are available it fails and returns 0.
  28. *
  29. *Entry:
  30. *  VOID
  31. *
  32. *Exit:
  33. *  LPWSTR - pointer to environment block
  34. *
  35. *Exceptions:
  36. *
  37. *******************************************************************************/
  38.  
  39. LPWSTR __cdecl __crtGetCommandLineW(
  40.     VOID
  41.     )
  42. {
  43.     static int f_use = 0;
  44.  
  45.     /*
  46.      * Look for unstubbed 'preferred' flavor. Otherwise use available flavor.
  47.      * Must actually call the function to ensure it's not a stub.
  48.      */
  49.  
  50.     if (0 == f_use)
  51.     {
  52.         if (NULL != GetCommandLineW())
  53.             f_use = USE_W;
  54.  
  55.         else if (NULL != GetCommandLineA())
  56.             f_use = USE_A;
  57.  
  58.         else
  59.             return 0;
  60.     }
  61.  
  62.     /* Use "W" version */
  63.  
  64.     if (USE_W == f_use)
  65.     {
  66.         return GetCommandLineW();
  67.     }
  68.  
  69.     /* Use "A" version */
  70.  
  71.     if (USE_A == f_use)
  72.     {
  73.         int buff_size;
  74.         wchar_t *wbuffer;
  75.         LPSTR lpenv;
  76.  
  77.         /*
  78.          * Convert strings and return the requested information.
  79.          */
  80.  
  81.         lpenv = GetCommandLineA();
  82.  
  83.         /* find out how big a buffer we need */
  84.         if (0 == (buff_size = MultiByteToWideChar(__lc_codepage,
  85.             MB_PRECOMPOSED, lpenv, -1, NULL, 0)))
  86.             return 0;
  87.  
  88.         /* allocate enough space for chars */
  89.         if (NULL == (wbuffer = (wchar_t *)
  90.             _malloc_crt(buff_size * sizeof(wchar_t))))
  91.             return 0;
  92.  
  93.         if (0 == MultiByteToWideChar(__lc_codepage,
  94.             MB_PRECOMPOSED, lpenv, -1, wbuffer, buff_size))
  95.             goto error_cleanup;
  96.  
  97.         return (LPWSTR)wbuffer;
  98.  
  99. error_cleanup:
  100.         _free_crt(wbuffer);
  101.         return 0;
  102.     }
  103.     else   /* f_use is neither USE_A nor USE_W */
  104.         return 0;
  105. }
  106.