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.

306 lines
6.9 KiB

  1. /*++
  2. Copyright (c) 1993-2002 Microsoft Corporation
  3. Module Name:
  4. error.cpp
  5. Abstract:
  6. This file implements the error handeling functions for the
  7. entire DRWTSN32 application. This includes error popups,
  8. debug prints, and assertions.
  9. Author:
  10. Wesley Witt (wesw) 1-May-1993
  11. Environment:
  12. User Mode
  13. --*/
  14. #include "pch.cpp"
  15. void
  16. __cdecl
  17. FatalError(
  18. HRESULT Error,
  19. _TCHAR * pszFormat,
  20. ...
  21. )
  22. /*++
  23. Routine Description:
  24. This function is called when there is nothing else to do, hence
  25. the name FatalError. It puts up a popup and then terminates.
  26. Arguments:
  27. Same as printf.
  28. Return Value:
  29. None.
  30. --*/
  31. {
  32. PTSTR pszErrMsg = NULL;
  33. PTSTR pszInternalMsgFormat = NULL;
  34. _TCHAR szArgumentsBuffer[1024 * 2] = {0};
  35. _TCHAR szMsg[1024 * 8] = {0};
  36. DWORD dwCount;
  37. va_list arg_ptr;
  38. va_start(arg_ptr, pszFormat);
  39. _vsntprintf(szArgumentsBuffer, sizeof(szArgumentsBuffer) / sizeof(_TCHAR),
  40. pszFormat, arg_ptr);
  41. szArgumentsBuffer[sizeof(szArgumentsBuffer) / sizeof(_TCHAR) - 1] = 0;
  42. va_end(arg_ptr);
  43. dwCount = FormatMessage(
  44. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  45. FORMAT_MESSAGE_IGNORE_INSERTS,
  46. NULL,
  47. Error,
  48. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  49. (PTSTR) &pszErrMsg,
  50. 0,
  51. NULL
  52. );
  53. _sntprintf(szMsg, sizeof(szMsg) / sizeof(_TCHAR),
  54. LoadRcString(IDS_ERROR_FORMAT_STRING),
  55. szArgumentsBuffer, Error);
  56. szMsg[sizeof(szMsg) / sizeof(_TCHAR) - 1] = 0;
  57. if (dwCount) {
  58. if ( (_tcslen(szMsg) + _tcslen(pszErrMsg) +1) * sizeof(_TCHAR) < sizeof(szMsg)) {
  59. _tcscat(szMsg, pszErrMsg);
  60. }
  61. }
  62. MessageBox(NULL, szMsg, LoadRcString(IDS_FATAL_ERROR), MB_TASKMODAL | MB_SETFOREGROUND | MB_OK);
  63. if (pszErrMsg) {
  64. LocalFree(pszErrMsg);
  65. }
  66. ExitProcess(0);
  67. }
  68. void
  69. __cdecl
  70. NonFatalError(
  71. PTSTR pszFormat,
  72. ...
  73. )
  74. /*++
  75. Routine Description:
  76. This function is used to generate a popup with some kind of
  77. warning message inside.
  78. Arguments:
  79. Same as printf.
  80. Return Value:
  81. None.
  82. --*/
  83. {
  84. PTSTR pszErrMsg = NULL;
  85. PTSTR pszInternalMsgFormat = NULL;
  86. _TCHAR szArgumentsBuffer[1024 * 2] = {0};
  87. _TCHAR szMsg[1024 * 8] = {0};
  88. DWORD dwCount;
  89. va_list arg_ptr;
  90. DWORD dwError;
  91. dwError = GetLastError();
  92. va_start(arg_ptr, pszFormat);
  93. _vsntprintf(szArgumentsBuffer, sizeof(szArgumentsBuffer) / sizeof(_TCHAR),
  94. pszFormat, arg_ptr);
  95. szArgumentsBuffer[sizeof(szArgumentsBuffer) / sizeof(_TCHAR) - 1] = 0;
  96. va_end(arg_ptr);
  97. if (ERROR_SUCCESS == dwError) {
  98. // Don't bother getting an error message
  99. _tcscpy(szMsg, szArgumentsBuffer);
  100. } else {
  101. // We have a real error
  102. dwCount = FormatMessage(
  103. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  104. FORMAT_MESSAGE_IGNORE_INSERTS,
  105. NULL,
  106. dwError,
  107. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  108. (PTSTR) &pszErrMsg,
  109. 0,
  110. NULL
  111. );
  112. _sntprintf(szMsg, sizeof(szMsg) / sizeof(_TCHAR),
  113. LoadRcString(IDS_ERROR_FORMAT_STRING),
  114. szArgumentsBuffer, dwError);
  115. szMsg[sizeof(szMsg) / sizeof(_TCHAR) - 1] = 0;
  116. if (dwCount) {
  117. if ( (_tcslen(szMsg) + _tcslen(pszErrMsg) +1) * sizeof(_TCHAR) < sizeof(szMsg)) {
  118. _tcscat(szMsg, pszErrMsg);
  119. }
  120. }
  121. }
  122. MessageBox(NULL, szMsg, LoadRcString(IDS_NONFATAL_ERROR),
  123. MB_TASKMODAL | MB_SETFOREGROUND | MB_OK);
  124. if (pszErrMsg) {
  125. LocalFree(pszErrMsg);
  126. }
  127. }
  128. void
  129. __cdecl
  130. dprintf(
  131. _TCHAR *format,
  132. ...
  133. )
  134. /*++
  135. Routine Description:
  136. This function is a var-args version of OutputDebugString.
  137. Arguments:
  138. Same as printf.
  139. Return Value:
  140. None.
  141. --*/
  142. {
  143. _TCHAR buf[1024];
  144. va_list arg_ptr;
  145. va_start(arg_ptr, format);
  146. _vsntprintf(buf, sizeof(buf) / sizeof(_TCHAR), format, arg_ptr);
  147. buf[sizeof(buf) / sizeof(_TCHAR) - 1] = 0;
  148. va_end(arg_ptr);
  149. OutputDebugString( buf );
  150. return;
  151. }
  152. void
  153. AssertError(
  154. PTSTR pszExpression,
  155. PTSTR pszFile,
  156. DWORD dwLineNumber
  157. )
  158. /*++
  159. Routine Description:
  160. Display an assertion failure message box which gives the user a choice
  161. as to whether the process should be aborted, the assertion ignored or
  162. a break exception generated.
  163. Arguments:
  164. Expression - Supplies a string representation of the failed assertion.
  165. File - Supplies a pointer to the file name where the assertion
  166. failed.
  167. LineNumber - Supplies the line number in the file where the assertion
  168. failed.
  169. Return Value:
  170. None.
  171. --*/
  172. {
  173. int nResponse;
  174. _TCHAR szModuleBuffer[ MAX_PATH ];
  175. DWORD dwLength;
  176. _TCHAR szBuffer[ 4096 ];
  177. DWORD dwError;
  178. LPTSTR lpszMsgBuf = NULL;
  179. dwError = GetLastError();
  180. //
  181. // Get the last error string
  182. //
  183. FormatMessage(
  184. FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
  185. FORMAT_MESSAGE_IGNORE_INSERTS,
  186. NULL,
  187. dwError,
  188. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  189. (LPTSTR) &lpszMsgBuf,
  190. 0,
  191. NULL);
  192. //
  193. // Get the asserting module's file name.
  194. //
  195. dwLength = GetModuleFileName( NULL, szModuleBuffer, sizeof(szModuleBuffer) / sizeof(_TCHAR));
  196. _sntprintf(szBuffer, sizeof(szBuffer) / sizeof(_TCHAR),
  197. _T("Assertion Failed : <%s> in file %s at line %u\n\n")
  198. _T("Module Name: %s\nLast system error: %u\n%s"),
  199. pszExpression, pszFile, dwLineNumber, szModuleBuffer,
  200. dwError, lpszMsgBuf);
  201. szBuffer[sizeof(szBuffer) / sizeof(_TCHAR) - 1] = 0;
  202. LocalFree( lpszMsgBuf );
  203. nResponse = MessageBox(NULL, szBuffer, _T("DrWatson Assertion"),
  204. MB_TASKMODAL | MB_ABORTRETRYIGNORE | MB_ICONERROR | MB_TASKMODAL);
  205. switch( nResponse ) {
  206. case IDABORT:
  207. //
  208. // Terminate the process.
  209. //
  210. ExitProcess( (UINT) -1 );
  211. break;
  212. case IDIGNORE:
  213. //
  214. // Ignore the failed assertion.
  215. //
  216. break;
  217. case IDRETRY:
  218. //
  219. // Break into a debugger.
  220. //
  221. DebugBreak();
  222. break;
  223. default:
  224. //
  225. // Break into a debugger because of a catastrophic failure.
  226. //
  227. DebugBreak( );
  228. break;
  229. }
  230. }