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.

161 lines
3.1 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1999
  3. Module Name:
  4. errorstr
  5. Abstract:
  6. This module contains an interesting collection of routines that are
  7. generally useful in the Calais context, but don't seem to fit anywhere else.
  8. Author:
  9. Doug Barlow (dbarlow) 11/14/1996
  10. Environment:
  11. Win32, C++ w/ Exceptions
  12. Notes:
  13. ?Notes?
  14. --*/
  15. #ifndef WIN32_LEAN_AND_MEAN
  16. #define WIN32_LEAN_AND_MEAN
  17. #endif
  18. #include <Windows.h>
  19. #include <stdio.h>
  20. #include <tchar.h>
  21. #include "cspUtils.h"
  22. /*++
  23. ErrorString:
  24. This routine does it's very best to translate a given error code into a
  25. text message. Any trailing non-printable characters are striped from the
  26. end of the text message, such as carriage returns and line feeds.
  27. Arguments:
  28. dwErrorCode supplies the error code to be translated.
  29. Return Value:
  30. The address of a freshly allocated text string. Use FreeErrorString to
  31. dispose of it.
  32. Throws:
  33. Errors are thrown as DWORD status codes.
  34. Remarks:
  35. Author:
  36. Doug Barlow (dbarlow) 8/27/1998
  37. --*/
  38. LPCTSTR
  39. ErrorString(
  40. DWORD dwErrorCode)
  41. {
  42. LPTSTR szErrorString = NULL;
  43. DWORD dwLen;
  44. LPTSTR szLast;
  45. dwLen = FormatMessage(
  46. FORMAT_MESSAGE_ALLOCATE_BUFFER
  47. | FORMAT_MESSAGE_FROM_SYSTEM,
  48. NULL,
  49. dwErrorCode,
  50. LANG_NEUTRAL,
  51. (LPTSTR)&szErrorString,
  52. 0,
  53. NULL);
  54. if (0 == dwLen)
  55. {
  56. ASSERT(NULL == szErrorString);
  57. dwLen = FormatMessage(
  58. FORMAT_MESSAGE_ALLOCATE_BUFFER
  59. | FORMAT_MESSAGE_FROM_HMODULE,
  60. GetModuleHandle(NULL),
  61. dwErrorCode,
  62. LANG_NEUTRAL,
  63. (LPTSTR)&szErrorString,
  64. 0,
  65. NULL);
  66. if (0 == dwLen)
  67. {
  68. ASSERT(NULL == szErrorString);
  69. szErrorString = (LPTSTR)LocalAlloc(
  70. LMEM_FIXED,
  71. 32 * sizeof(TCHAR));
  72. if (NULL == szErrorString)
  73. goto ErrorExit;
  74. _stprintf(szErrorString, TEXT("0x%08x"), dwErrorCode);
  75. }
  76. }
  77. ASSERT(NULL != szErrorString);
  78. for (szLast = szErrorString + lstrlen(szErrorString) - 1;
  79. szLast > szErrorString;
  80. szLast -= 1)
  81. {
  82. if (_istgraph(*szLast))
  83. break;
  84. *szLast = 0;
  85. }
  86. return szErrorString;
  87. ErrorExit:
  88. return TEXT("Unrecoverable error translating error code");
  89. }
  90. /*++
  91. FreeErrorString:
  92. This routine frees the Error String allocated by the ErrorString service.
  93. Arguments:
  94. szErrorString supplies the error string to be deallocated.
  95. Return Value:
  96. None
  97. Throws:
  98. None
  99. Remarks:
  100. Author:
  101. Doug Barlow (dbarlow) 8/27/1998
  102. --*/
  103. void
  104. FreeErrorString(
  105. LPCTSTR szErrorString)
  106. {
  107. if (NULL != szErrorString)
  108. LocalFree((LPVOID)szErrorString);
  109. }