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.

364 lines
7.5 KiB

  1. /******************************************************************
  2. * *
  3. * CPP file for common error handling functions. *
  4. * *
  5. ******************************************************************/
  6. #ifdef USE_STDAFX
  7. # include "stdafx.h"
  8. # include "rpc.h"
  9. #else
  10. # include <windows.h>
  11. #endif
  12. #include "HrMsg.h"
  13. #include <stdio.h>
  14. #include <stdarg.h>
  15. namespace HrMsg_cpp
  16. {
  17. void __stdcall AdmtThrowErrorImpl(const _com_error& ce, LPCTSTR pszDescription);
  18. }
  19. using namespace HrMsg_cpp;
  20. //---------------------------------------------------------------------------
  21. // GetError Helper Function
  22. //---------------------------------------------------------------------------
  23. _com_error GetError(HRESULT hr)
  24. {
  25. _com_error ce(hr);
  26. IErrorInfo* pErrorInfo = NULL;
  27. if (GetErrorInfo(0, &pErrorInfo) == S_OK)
  28. {
  29. ce = _com_error(FAILED(hr) ? hr : E_FAIL, pErrorInfo);
  30. }
  31. else
  32. {
  33. ce = _com_error(FAILED(hr) ? hr : S_OK);
  34. }
  35. return ce;
  36. }
  37. //-----------------------------------------------------------------------------
  38. // Return text for hresults
  39. //-----------------------------------------------------------------------------
  40. _bstr_t __stdcall HResultToText2(HRESULT hr)
  41. {
  42. _bstr_t bstrError;
  43. LPTSTR pszError = NULL;
  44. try
  45. {
  46. switch (HRESULT_FACILITY(hr))
  47. {
  48. // case FACILITY_NULL: // 0
  49. // case FACILITY_RPC: // 1
  50. // case FACILITY_DISPATCH: // 2
  51. // case FACILITY_STORAGE: // 3
  52. case FACILITY_ITF: // 4
  53. {
  54. HMODULE hModule = LoadLibrary(_T("MSDAERR.dll"));
  55. if (hModule)
  56. {
  57. FormatMessage(
  58. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_HMODULE,
  59. hModule,
  60. hr,
  61. 0,
  62. (LPTSTR)&pszError,
  63. 0,
  64. NULL
  65. );
  66. FreeLibrary(hModule);
  67. }
  68. break;
  69. }
  70. case FACILITY_WIN32: // 7
  71. {
  72. FormatMessage(
  73. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
  74. NULL,
  75. hr,
  76. 0,
  77. (LPTSTR)&pszError,
  78. 0,
  79. NULL
  80. );
  81. break;
  82. }
  83. // case FACILITY_WINDOWS: // 8
  84. // case FACILITY_SSPI: // 9
  85. // case FACILITY_SECURITY: // 9
  86. case FACILITY_CONTROL: // 10
  87. {
  88. HMODULE hModule = LoadLibrary(_T("MSADER15.dll"));
  89. if (hModule)
  90. {
  91. FormatMessage(
  92. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_HMODULE,
  93. hModule,
  94. hr,
  95. 0,
  96. (LPTSTR)&pszError,
  97. 0,
  98. NULL
  99. );
  100. FreeLibrary(hModule);
  101. }
  102. break;
  103. }
  104. // case FACILITY_CERT: // 11
  105. // case FACILITY_INTERNET: // 12
  106. // case FACILITY_MEDIASERVER: // 13
  107. case FACILITY_MSMQ: // 14
  108. {
  109. HMODULE hModule = LoadLibrary(_T("MQUTIL.dll"));
  110. if (hModule)
  111. {
  112. FormatMessage(
  113. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_HMODULE,
  114. hModule,
  115. hr,
  116. 0,
  117. (LPTSTR)&pszError,
  118. 0,
  119. NULL
  120. );
  121. FreeLibrary(hModule);
  122. }
  123. break;
  124. }
  125. // case FACILITY_SETUPAPI: // 15
  126. // case FACILITY_SCARD: // 16
  127. // case FACILITY_COMPLUS: // 17
  128. // case FACILITY_AAF: // 18
  129. // case FACILITY_URT: // 19
  130. // case FACILITY_ACS: // 20
  131. default:
  132. {
  133. FormatMessage(
  134. FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
  135. NULL,
  136. hr,
  137. 0,
  138. (LPTSTR)&pszError,
  139. 0,
  140. NULL
  141. );
  142. break;
  143. }
  144. }
  145. if (pszError)
  146. {
  147. size_t cch = _tcslen(pszError);
  148. if ((cch > 1) && (pszError[cch - 1] == _T('\n')))
  149. {
  150. pszError[cch - 1] = 0;
  151. if (pszError[cch - 2] == _T('\r'))
  152. {
  153. pszError[cch - 2] = 0;
  154. }
  155. }
  156. bstrError = pszError;
  157. }
  158. else
  159. {
  160. _TCHAR szError[32];
  161. _stprintf(szError, _T("Unknown error 0x%08lX."), hr);
  162. bstrError = szError;
  163. }
  164. }
  165. catch (...)
  166. {
  167. ;
  168. }
  169. if (pszError)
  170. {
  171. LocalFree((HLOCAL)pszError);
  172. }
  173. return bstrError;
  174. }
  175. _bstr_t __stdcall HResultToText(HRESULT hr)
  176. {
  177. return GetError(hr).Description();
  178. }
  179. //---------------------------------------------------------------------------
  180. // AdmtThrowError
  181. //
  182. // Generates formatted error description and generates exception.
  183. //
  184. // 2000-??-?? Mark Oluper - initial
  185. // 2001-02-13 Mark Oluper - moved to commonlib
  186. //---------------------------------------------------------------------------
  187. void __cdecl AdmtThrowError(_com_error ce, HINSTANCE hInstance, UINT uId, ...)
  188. {
  189. _TCHAR szFormat[512];
  190. _TCHAR szDescription[1024];
  191. if (LoadString(hInstance, uId, szFormat, 512))
  192. {
  193. va_list args;
  194. va_start(args, uId);
  195. _vsntprintf(szDescription, sizeof(szDescription) / sizeof(szDescription[0]), szFormat, args);
  196. szDescription[sizeof(szDescription) / sizeof(szDescription[0]) - 1] = _T('\0');
  197. va_end(args);
  198. }
  199. else
  200. {
  201. szDescription[0] = _T('\0');
  202. }
  203. AdmtThrowErrorImpl(ce, szDescription);
  204. }
  205. void __cdecl AdmtThrowError(_com_error ce, LPCTSTR pszFormat, ...)
  206. {
  207. _TCHAR szDescription[1024];
  208. if (pszFormat)
  209. {
  210. va_list args;
  211. va_start(args, pszFormat);
  212. _vsntprintf(szDescription, sizeof(szDescription) / sizeof(szDescription[0]), pszFormat, args);
  213. szDescription[sizeof(szDescription) / sizeof(szDescription[0]) - 1] = _T('\0');
  214. va_end(args);
  215. }
  216. else
  217. {
  218. szDescription[0] = _T('\0');
  219. }
  220. AdmtThrowErrorImpl(ce, szDescription);
  221. }
  222. namespace HrMsg_cpp
  223. {
  224. //---------------------------------------------------------------------------
  225. // AdmtThrowErrorImpl
  226. //
  227. // Concatenates rich error information and throws exception.
  228. //
  229. // 2000-??-?? Mark Oluper - initial
  230. // 2001-02-13 Mark Oluper - moved to commonlib
  231. //---------------------------------------------------------------------------
  232. void __stdcall AdmtThrowErrorImpl(const _com_error& ce, LPCTSTR pszDescription)
  233. {
  234. _bstr_t bstrNewDescription;
  235. try
  236. {
  237. bstrNewDescription = pszDescription;
  238. _bstr_t bstrSource = ce.Source();
  239. if (bstrSource.length() > 0)
  240. {
  241. if (bstrNewDescription.length() > 0)
  242. {
  243. bstrNewDescription += _T(" : ");
  244. }
  245. bstrNewDescription += bstrSource;
  246. }
  247. _bstr_t bstrOldDescription = ce.Description();
  248. if (bstrOldDescription.length() > 0)
  249. {
  250. if (bstrNewDescription.length() > 0)
  251. {
  252. if (bstrSource.length() > 0)
  253. {
  254. bstrNewDescription += _T(": ");
  255. }
  256. else
  257. {
  258. bstrNewDescription += _T(" ");
  259. }
  260. }
  261. bstrNewDescription += bstrOldDescription;
  262. }
  263. else
  264. {
  265. LPCTSTR pszErrorMessage = ce.ErrorMessage();
  266. if (pszErrorMessage)
  267. {
  268. if (bstrNewDescription.length() > 0)
  269. {
  270. bstrNewDescription += _T(" : ");
  271. }
  272. bstrNewDescription += pszErrorMessage;
  273. }
  274. }
  275. }
  276. catch (...)
  277. {
  278. ;
  279. }
  280. ICreateErrorInfoPtr spCreateErrorInfo;
  281. CreateErrorInfo(&spCreateErrorInfo);
  282. if (spCreateErrorInfo)
  283. {
  284. // LPOLESTR pszProgId;
  285. // if (ProgIDFromCLSID(clsid, &pszProgId) == S_OK)
  286. // {
  287. // spCreateErrorInfo->SetSource(pszProgId);
  288. // CoTaskMemFree(pszProgId);
  289. // }
  290. // else
  291. // {
  292. spCreateErrorInfo->SetSource(L"");
  293. // }
  294. // spCreateErrorInfo->SetGUID(iid);
  295. spCreateErrorInfo->SetGUID(GUID_NULL);
  296. spCreateErrorInfo->SetDescription(bstrNewDescription);
  297. spCreateErrorInfo->SetHelpFile(L"");
  298. spCreateErrorInfo->SetHelpContext(0);
  299. }
  300. _com_raise_error(ce.Error(), IErrorInfoPtr(spCreateErrorInfo).Detach());
  301. }
  302. } // namespace