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

  1. /***
  2. *wperror.c - print system error message (wchar_t version)
  3. *
  4. *       Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *       defines _wperror() - print wide system error message
  8. *       System error message are indexed by errno.
  9. *
  10. *******************************************************************************/
  11.  
  12.  
  13. #include <cruntime.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <syserr.h>
  18. #include <mtdll.h>
  19. #include <io.h>
  20. #include <dbgint.h>
  21.  
  22. /***
  23. *void _wperror(wmessage) - print system error message
  24. *
  25. *Purpose:
  26. *       prints user's error message, then follows it with ": ", then the system
  27. *       error message, then a newline.  All output goes to stderr.  If user's
  28. *       message is NULL or a null string, only the system error message is
  29. *       printer.  If errno is weird, prints "Unknown error".
  30. *
  31. *Entry:
  32. *       const wchar_t *wmessage - users message to prefix system error message
  33. *
  34. *Exit:
  35. *       Prints message; no return value.
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. void __cdecl _wperror (
  42.         REG1 const wchar_t *wmessage
  43.         )
  44. {
  45.         REG2 int fh = 2;
  46.         int size;
  47.         char *amessage;
  48.  
  49.         /* convert WCS string into ASCII string */
  50.  
  51.         size = wcslen(wmessage) + 1;
  52.  
  53.         if (NULL == (amessage = (char *)_malloc_crt(size * sizeof(char))))
  54.             return;
  55.  
  56.         if (0 == (wcstombs(amessage, wmessage, size)))
  57.         {
  58.             _free_crt (amessage);
  59.             return;
  60.         }
  61.  
  62.         _lock_fh(fh);           /* acquire file handle lock */
  63.  
  64.         if (amessage && *amessage)
  65.         {
  66.                 _write_lk(fh,(char *)amessage,strlen(amessage));
  67.                 _write_lk(fh,": ",2);
  68.         }
  69.  
  70.         _free_crt(amessage);
  71.  
  72.         amessage = _sys_err_msg( errno );
  73.         _write_lk(fh,(char *)amessage,strlen(amessage));
  74.         _write_lk(fh,"\n",1);
  75.  
  76.         _unlock_fh(fh);         /* release file handle lock */
  77. }
  78.  
  79.