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.

263 lines
7.2 KiB

  1. /*++
  2. Microsoft Windows
  3. Copyright (C) Microsoft Corporation, 1981 - 1998
  4. Module Name:
  5. error.cxx
  6. Abstract:
  7. Contains error reporting routines. All functions are fairly straightforward
  8. Author:
  9. Rahul Thombre (RahulTh) 4/12/1998
  10. Revision History:
  11. 4/12/1998 RahulTh Created this module.
  12. 10/1/1998 RahulTh Massive changes to the error reporting mechanism
  13. now better and more convenient
  14. --*/
  15. #include "precomp.hxx"
  16. //+--------------------------------------------------------------------------
  17. //
  18. // Member: CError::ConstructMessage
  19. //
  20. // Synopsis: this is an internal helper function that constructs a message
  21. // from the available error codes it is called by both ShowMessage
  22. // and ShowConsoleMessage
  23. //
  24. // Arguments: [in] argList : the va_list of arguments
  25. // [out] szErrMsg : the formatted error message
  26. //
  27. // Returns: nothing
  28. //
  29. // History: 10/2/1998 RahulTh created
  30. //
  31. // Notes:
  32. //
  33. //---------------------------------------------------------------------------
  34. void CError::ConstructMessage (va_list argList, CString& szErrMsg)
  35. {
  36. AFX_MANAGE_STATE (AfxGetStaticModuleState());
  37. TCHAR lpszMessage[2048];
  38. szErrMsg.LoadString (m_msgID);
  39. HRESULT hr;
  40. hr = StringCchVPrintf (lpszMessage, sizeof(lpszMessage)/sizeof(lpszMessage[0]), (LPCTSTR) szErrMsg, argList);
  41. if (FAILED(hr))
  42. {
  43. szErrMsg = L"";
  44. return;
  45. }
  46. szErrMsg = lpszMessage;
  47. if (ERROR_SUCCESS != m_winErr)
  48. {
  49. LPVOID lpMsgBuf;
  50. DWORD dwRet;
  51. dwRet = ::FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |
  52. FORMAT_MESSAGE_FROM_SYSTEM |
  53. FORMAT_MESSAGE_IGNORE_INSERTS,
  54. NULL,
  55. m_winErr,
  56. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  57. (LPTSTR) &lpMsgBuf,
  58. 0,
  59. NULL
  60. );
  61. if (dwRet)
  62. {
  63. szErrMsg += TEXT("\n\n");
  64. szErrMsg += (LPCTSTR) lpMsgBuf;
  65. LocalFree (lpMsgBuf);
  66. }
  67. }
  68. }
  69. //+--------------------------------------------------------------------------
  70. //
  71. // Member: CError::SetTitle
  72. //
  73. // Synopsis: sets the value of the member that determines the title of the
  74. // message box.
  75. //
  76. // Arguments: titleID : the resource id of the title.
  77. //
  78. // Returns: nothing
  79. //
  80. // History: 4/12/1999 RahulTh created
  81. //
  82. // Notes:
  83. //
  84. //---------------------------------------------------------------------------
  85. void CError::SetTitle (UINT titleID)
  86. {
  87. m_titleID = titleID;
  88. }
  89. //+--------------------------------------------------------------------------
  90. //
  91. // Member: CError::SetStyle
  92. //
  93. // Synopsis: sets the value of the member that determines the message
  94. // box style.
  95. //
  96. // Arguments: nStyle : the message box style.
  97. //
  98. // Returns: nothing
  99. //
  100. // History: 4/12/1999 RahulTh created
  101. //
  102. // Notes:
  103. //
  104. //---------------------------------------------------------------------------
  105. void CError::SetStyle (UINT nStyle)
  106. {
  107. m_nStyle = nStyle;
  108. }
  109. //+--------------------------------------------------------------------------
  110. //
  111. // Member: CError::SetError
  112. //
  113. // Synopsis: sets the value of the member that stores the windows error
  114. // encountered if any.
  115. //
  116. // Arguments: dwWinError : the value of the windows error encountered
  117. //
  118. // Returns: nothing
  119. //
  120. // History: 12/11/1998 RahulTh created
  121. //
  122. // Notes:
  123. //
  124. //---------------------------------------------------------------------------
  125. void CError::SetError (DWORD dwWinError)
  126. {
  127. m_winErr = dwWinError;
  128. }
  129. //+--------------------------------------------------------------------------
  130. //
  131. // Member: CError::ShowMessage
  132. //
  133. // Synopsis: displays an error message in a message box based on the
  134. // members of the object
  135. //
  136. // Arguments: message id for the error + more
  137. //
  138. // Returns: the return value of the message box
  139. //
  140. // History: 10/1/1998 RahulTh created
  141. //
  142. // Notes: if the resultant message is longer than 2048 characters
  143. // then result is unpredictable and may also cause AVs.
  144. // but this is a limitation of wvsprintf. However, this is not
  145. // so bad since we can make sure that we do not have any error
  146. // message that exceed this self imposed limit
  147. //
  148. //---------------------------------------------------------------------------
  149. int CError::ShowMessage (UINT errID, ...)
  150. {
  151. AFX_MANAGE_STATE (AfxGetStaticModuleState());
  152. va_list argList;
  153. CString szErrMsg;
  154. CString szTitle;
  155. m_msgID = errID; //update the message ID with the new one
  156. szTitle.LoadString (m_titleID);
  157. va_start (argList, errID);
  158. ConstructMessage (argList, szErrMsg);
  159. va_end (argList);
  160. return ::MessageBox (m_hWndParent, (LPCTSTR)szErrMsg,
  161. (LPCTSTR) szTitle, m_nStyle);
  162. }
  163. //+--------------------------------------------------------------------------
  164. //
  165. // Member: ShowConsoleMessage
  166. //
  167. // Synopsis: displays a message using MMC's IConsole interface
  168. //
  169. // Arguments: [in] pConsole : pointer to IConsole interface
  170. // [in] errID : error resource ID
  171. // + other codes
  172. //
  173. // Returns: return value of the message box
  174. //
  175. // History: 10/2/1998 RahulTh created
  176. //
  177. // Notes:
  178. //
  179. //---------------------------------------------------------------------------
  180. int CError::ShowConsoleMessage (LPCONSOLE pConsole, UINT errID, ...)
  181. {
  182. AFX_MANAGE_STATE (AfxGetStaticModuleState());
  183. va_list argList;
  184. CString szErrMsg;
  185. CString szTitle;
  186. int iRet;
  187. m_msgID = errID;
  188. szTitle.LoadString (m_titleID);
  189. va_start(argList, errID);
  190. ConstructMessage (argList, szErrMsg);
  191. va_end (argList);
  192. pConsole->MessageBox ((LPCTSTR)szErrMsg, (LPCTSTR) szTitle, m_nStyle,
  193. &iRet);
  194. return iRet;
  195. }
  196. //+--------------------------------------------------------------------------
  197. //
  198. // Function: _DbgMsg
  199. //
  200. // Synopsis: function that sends messages to the debugger
  201. //
  202. // Arguments: format string + more
  203. //
  204. // Returns: nothing
  205. //
  206. // History: 10/1/1998 RahulTh created
  207. //
  208. // Notes: Do not try to print debug messages longer than 2048 characters.
  209. //
  210. //---------------------------------------------------------------------------
  211. void _DbgMsg (LPCTSTR szFormat ...)
  212. {
  213. CString cszFormat;
  214. va_list argList;
  215. //do not try to print debug messages longer than 2048 characters.
  216. TCHAR lpszMessage[2048];
  217. CTime theTime = CTime::GetCurrentTime();
  218. va_start (argList, szFormat);
  219. (void) StringCchVPrintf (lpszMessage, sizeof(lpszMessage)/sizeof(lpszMessage[0]), szFormat, argList);
  220. va_end(argList);
  221. cszFormat = ((TEXT("FDE.DLL@") + theTime.Format(TEXT("[%x, %X]>> "))) + lpszMessage) + '\n';
  222. OutputDebugString ((LPCTSTR)cszFormat);
  223. return;
  224. }