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

  1. /***
  2. *wtombenv.c - convert wide environment block to multibyte
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines __wtomb_environ(). Create a multibyte equivalent of
  8. *       an existing wide character 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. *__wtomb_environ - copy wide environment block to multibyte environment block
  21. *
  22. *Purpose:
  23. *       Create a multibyte equivalent of an existing wide character
  24. *       environment block.
  25. *
  26. *Entry:
  27. *       Assume _wenviron (global pointer) points to existing wide
  28. *       environment block.
  29. *
  30. *Exit:
  31. *       If success, every wide environment variable has been added to
  32. *       the multibyte 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 __wtomb_environ (
  41.         void
  42.         )
  43. {
  44.         char *envp;
  45.         wchar_t **wenvp = _wenviron;
  46.  
  47.         /*
  48.          * For every environment variable in the multibyte environment,
  49.          * convert it and add it to the wide environment.
  50.          */
  51.  
  52.         while (*wenvp)
  53.         {
  54.             int size;
  55.  
  56.             /* find out how much space is needed */
  57.             if ((size = WideCharToMultiByte(CP_OEMCP, 0, *wenvp, -1, NULL, 0, NULL, NULL)) == 0)
  58.                 return -1;
  59.  
  60.             /* allocate space for variable */
  61.             if ((envp = (char *) _malloc_crt(size * sizeof(char))) == NULL)
  62.                 return -1;
  63.  
  64.             /* convert it */
  65.             if (WideCharToMultiByte(CP_OEMCP, 0, *wenvp, -1, envp, size, NULL, NULL) == 0)
  66.                 return -1;
  67.  
  68.             /* set it - this is not primary call, so set primary == 0 */
  69.             __crtsetenv(envp, 0);
  70.  
  71.             wenvp++;
  72.         }
  73.  
  74.         return 0;
  75. }
  76.  
  77.