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.

183 lines
5.1 KiB

  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1996-2002 Microsoft Corporation
  4. //
  5. // Module Name:
  6. // FormatErrorMessage.cpp
  7. //
  8. // Description:
  9. // Error message formatting routines.
  10. //
  11. // Maintained By:
  12. // David Potter (davidp) 31-MAR-2000
  13. //
  14. // Revision History:
  15. //
  16. // Notes:
  17. //
  18. /////////////////////////////////////////////////////////////////////////////
  19. #include "Pch.h"
  20. #include <wchar.h>
  21. /////////////////////////////////////////////////////////////////////////////
  22. //++
  23. //
  24. // HRESULT
  25. // WINAPI
  26. // HrFormatErrorMessage(
  27. // LPWSTR pszErrorOut,
  28. // UINT nMxErrorIn,
  29. // DWORD scIn
  30. // )
  31. //
  32. // Routine Description:
  33. // Format the error message represented by the status code. Works for
  34. // HRESULTS as well.
  35. //
  36. // Arguments:
  37. // pszErrorOut -- Unicode string in which to return the error message.
  38. // nMxErrorIn -- Maximum length of the output string.
  39. // scIn -- Status code.
  40. //
  41. // Return Value:
  42. // S_OK Status code formatted successfully.
  43. // Other HRESULTs from FormatMessageW().
  44. //
  45. //--
  46. /////////////////////////////////////////////////////////////////////////////
  47. HRESULT
  48. WINAPI
  49. HrFormatErrorMessage(
  50. LPWSTR pszErrorOut,
  51. UINT nMxErrorIn,
  52. DWORD scIn
  53. )
  54. {
  55. HRESULT hr = S_OK;
  56. DWORD cch;
  57. TraceFunc( "" );
  58. // Format the NT status code from the system.
  59. cch = FormatMessageW(
  60. FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  61. NULL,
  62. scIn,
  63. MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ),
  64. pszErrorOut,
  65. nMxErrorIn,
  66. 0
  67. );
  68. if ( cch == 0 )
  69. {
  70. hr = GetLastError();
  71. hr = THR( HRESULT_FROM_WIN32( hr ) );
  72. //Trace( g_tagError, _T("Error %d getting message from system for error code %d"), GetLastError(), sc );
  73. // Format the NT status code from NTDLL since this hasn't been
  74. // integrated into the system yet.
  75. cch = FormatMessageW(
  76. FORMAT_MESSAGE_FROM_HMODULE | FORMAT_MESSAGE_IGNORE_INSERTS,
  77. GetModuleHandleW( L"NTDLL.DLL" ),
  78. scIn,
  79. MAKELANGID( LANG_NEUTRAL, SUBLANG_NEUTRAL ),
  80. pszErrorOut,
  81. nMxErrorIn,
  82. 0
  83. );
  84. if ( cch == 0 )
  85. {
  86. hr = GetLastError();
  87. hr = THR( HRESULT_FROM_WIN32( hr ) );
  88. #ifdef _DEBUG
  89. //DWORD _sc = GetLastError();
  90. //Trace( g_tagError, _T("Error %d getting message from NTDLL.DLL for error code %d"), _sc, sc );
  91. #endif
  92. pszErrorOut[ 0 ] = L'\0';
  93. } // if: error formatting status code from NTDLL
  94. else
  95. {
  96. hr = S_OK;
  97. } // else: successfully formatted the status code
  98. } // if: error formatting status code from system
  99. HRETURN( hr );
  100. } //*** HrFormatErrorMessage()
  101. /////////////////////////////////////////////////////////////////////////////
  102. //++
  103. //
  104. // HRESULT
  105. // __cdecl
  106. // HrFormatErrorMessageBoxText(
  107. // LPWSTR pszMessageOut,
  108. // UINT nMxMessageIn,
  109. // HRESULT hrIn,
  110. // LPCWSTR pszOperationIn,
  111. // ...
  112. // )
  113. //
  114. // Routine Description:
  115. // Format the error message represented by the status code. Works for
  116. // HRESULTS as well.
  117. //
  118. // Arguments:
  119. // pszMessageOut -- Unicode string in which to return the message.
  120. // nMxMessageIn -- Size of the output buffer.
  121. // hrIn -- Status code.
  122. // pszOperationIn -- Operational format message
  123. // ... -- Arguments for the operational format string.
  124. //
  125. // Return Value:
  126. // S_OK Text formatted successfully.
  127. // Other HRESULTs from FormatErrorMessage().
  128. //
  129. //--
  130. /////////////////////////////////////////////////////////////////////////////
  131. HRESULT
  132. __cdecl
  133. HrFormatErrorMessageBoxText(
  134. LPWSTR pszMessageOut,
  135. UINT nMxMessageIn,
  136. HRESULT hrIn,
  137. LPCWSTR pszOperationIn,
  138. ...
  139. )
  140. {
  141. HRESULT hr = S_OK;
  142. va_list valMarker;
  143. TraceFunc( "" );
  144. WCHAR szErrorMsg[ 1024 ];
  145. WCHAR szOperation[ 2048 ];
  146. hr = HrFormatErrorMessage( szErrorMsg, ARRAYSIZE( szErrorMsg ), hrIn );
  147. va_start( valMarker, pszOperationIn ); // Initialize variable arguments.
  148. THR( StringCchVPrintfW(
  149. szOperation
  150. , ARRAYSIZE( szOperation )
  151. , pszOperationIn
  152. , valMarker
  153. ) );
  154. THR( StringCchPrintfW(
  155. pszMessageOut
  156. , nMxMessageIn
  157. , L"%ls:\r\n\r\n%ls\r\nError ID %d (%#x)" // BUGBUG needs to be a string resource
  158. , szOperation
  159. , szErrorMsg
  160. , hrIn
  161. , hrIn
  162. ) );
  163. HRETURN( hr );
  164. } //*** HrFormatErrorMessageBoxText()