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

  1. /***
  2. *mbtowenv.c - convert multibyte environment block to wide
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines __mbtow_environ(). Create a wide character equivalent of
  8. *       an existing multibyte environment block.
  9. *
  10. *******************************************************************************/
  11.  
  12.  
  13. #include <windows.h>
  14. #include <cruntime.h>
  15. #include <internal.h>
  16. #include <stdlib.h>
  17. #include <dbgint.h>
  18.  
  19. /***
  20. *__mbtow_environ - copy multibyte environment block to wide environment block
  21. *
  22. *Purpose:
  23. *       Create a wide character equivalent of an existing multibyte
  24. *       environment block.
  25. *
  26. *Entry:
  27. *       Assume _environ (global pointer) points to existing multibyte
  28. *       environment block.
  29. *
  30. *Exit:
  31. *       If success, every multibyte environment variable has been added to
  32. *       the wide environment block and returns 0.
  33. *       If failure, returns -1.
  34. *
  35. *Exceptions:
  36. *       If space cannot be allocated, returns -1.
  37. *
  38. *******************************************************************************/
  39.  
  40. int __cdecl __mbtow_environ (
  41.         void
  42.         )
  43. {
  44.         int size;
  45.         wchar_t *wenvp;
  46.         char **envp = _environ;
  47.  
  48.         /*
  49.          * For every environment variable in the multibyte environment,
  50.          * convert it and add it to the wide environment.
  51.          */
  52.  
  53.         while (*envp)
  54.         {
  55.             /* find out how much space is needed */
  56.             if ((size = MultiByteToWideChar(CP_OEMCP, 0, *envp, -1, NULL, 0)) == 0)
  57.                 return -1;
  58.  
  59.             /* allocate space for variable */
  60.             if ((wenvp = (wchar_t *) _malloc_crt(size * sizeof(wchar_t))) == NULL)
  61.                 return -1;
  62.  
  63.             /* convert it */
  64.             if ((size = MultiByteToWideChar(CP_OEMCP, 0, *envp, -1, wenvp, size)) == 0)
  65.                 return -1;
  66.  
  67.             /* set it - this is not primary call, so set primary == 0 */
  68.             __crtwsetenv(wenvp, 0);
  69.  
  70.             envp++;
  71.         }
  72.  
  73.         return 0;
  74. }
  75.  
  76.