Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.4 KiB

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