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.

427 lines
11 KiB

  1. #include "StdAfx.h"
  2. #include "Error.h"
  3. using namespace _com_util;
  4. #define FORMAT_BUFFER_SIZE 1024
  5. #define DESCRIPTION_BUFFER_SIZE 2048
  6. #define COUNT_OF(a) (sizeof(a) / sizeof(a[0]))
  7. #define LENGTH_OF(a) (sizeof(a) / sizeof(a[0]) - sizeof(a[0]))
  8. namespace
  9. {
  10. // AdmtCreateErrorInfo Method
  11. IErrorInfoPtr __stdcall AdmtCreateErrorInfo(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription)
  12. {
  13. ICreateErrorInfoPtr spCreateErrorInfo;
  14. CreateErrorInfo(&spCreateErrorInfo);
  15. if (spCreateErrorInfo)
  16. {
  17. IErrorInfoPtr spErrorInfo = ce.ErrorInfo();
  18. // if (spErrorInfo == NULL)
  19. // {
  20. // GetErrorInfo(0, &spErrorInfo);
  21. // }
  22. // source
  23. if (IsEqualCLSID(clsid, GUID_NULL) == FALSE)
  24. {
  25. LPOLESTR pszProgId;
  26. if (ProgIDFromCLSID(clsid, &pszProgId) == S_OK)
  27. {
  28. spCreateErrorInfo->SetSource(pszProgId);
  29. CoTaskMemFree(pszProgId);
  30. }
  31. else
  32. {
  33. spCreateErrorInfo->SetSource(L"");
  34. }
  35. }
  36. else if (spErrorInfo)
  37. {
  38. BSTR bstrSource;
  39. HRESULT hr = spErrorInfo->GetSource(&bstrSource);
  40. if (SUCCEEDED(hr))
  41. {
  42. spCreateErrorInfo->SetSource(bstrSource);
  43. SysFreeString(bstrSource);
  44. }
  45. else
  46. {
  47. spCreateErrorInfo->SetSource(L"");
  48. }
  49. }
  50. else
  51. {
  52. spCreateErrorInfo->SetSource(L"");
  53. }
  54. // GUID
  55. bool bInterfaceSpecified = false;
  56. if (IsEqualIID(iid, GUID_NULL) == FALSE)
  57. {
  58. spCreateErrorInfo->SetGUID(iid);
  59. bInterfaceSpecified = true;
  60. }
  61. else if (spErrorInfo)
  62. {
  63. GUID guid;
  64. HRESULT hr = spErrorInfo->GetGUID(&guid);
  65. if (SUCCEEDED(hr))
  66. {
  67. spCreateErrorInfo->SetGUID(guid);
  68. }
  69. else
  70. {
  71. spCreateErrorInfo->SetGUID(GUID_NULL);
  72. }
  73. }
  74. else
  75. {
  76. spCreateErrorInfo->SetGUID(GUID_NULL);
  77. }
  78. // description
  79. _bstr_t strDescription = pszDescription;
  80. if (spErrorInfo)
  81. {
  82. /*
  83. BSTR bstrSource;
  84. spErrorInfo->GetSource(&bstrSource);
  85. if (SysStringLen(bstrSource) > 0)
  86. {
  87. if (strDescription.length() > 0)
  88. {
  89. strDescription += _T(" : ");
  90. }
  91. strDescription += bstrSource;
  92. }
  93. SysFreeString(bstrSource);
  94. */
  95. BSTR bstrDescription = NULL;
  96. HRESULT hr = spErrorInfo->GetDescription(&bstrDescription);
  97. if (SUCCEEDED(hr) && (SysStringLen(bstrDescription) > 0))
  98. {
  99. if (strDescription.length() > 0)
  100. {
  101. strDescription += _T(" ");
  102. }
  103. strDescription += bstrDescription;
  104. }
  105. else if (bInterfaceSpecified == false)
  106. {
  107. LPCTSTR pszErrorMessage = ce.ErrorMessage();
  108. if (pszErrorMessage)
  109. {
  110. if (strDescription.length() > 0)
  111. {
  112. strDescription += _T(" ");
  113. }
  114. strDescription += pszErrorMessage;
  115. }
  116. }
  117. if (bstrDescription)
  118. {
  119. SysFreeString(bstrDescription);
  120. }
  121. }
  122. else if (bInterfaceSpecified == false)
  123. {
  124. LPCTSTR pszErrorMessage = ce.ErrorMessage();
  125. if (pszErrorMessage)
  126. {
  127. if (strDescription.length() > 0)
  128. {
  129. strDescription += _T(" ");
  130. }
  131. strDescription += pszErrorMessage;
  132. }
  133. }
  134. spCreateErrorInfo->SetDescription(strDescription);
  135. // help file
  136. if (spErrorInfo)
  137. {
  138. BSTR bstrHelpFile;
  139. HRESULT hr = spErrorInfo->GetHelpFile(&bstrHelpFile);
  140. if (SUCCEEDED(hr))
  141. {
  142. spCreateErrorInfo->SetHelpFile(bstrHelpFile);
  143. SysFreeString(bstrHelpFile);
  144. }
  145. else
  146. {
  147. spCreateErrorInfo->SetHelpFile(L"");
  148. }
  149. }
  150. else
  151. {
  152. spCreateErrorInfo->SetHelpFile(L"");
  153. }
  154. // help context
  155. DWORD dwHelpContext = 0;
  156. if (spErrorInfo)
  157. {
  158. spErrorInfo->GetHelpContext(&dwHelpContext);
  159. }
  160. spCreateErrorInfo->SetHelpContext(dwHelpContext);
  161. }
  162. return IErrorInfoPtr(spCreateErrorInfo);
  163. }
  164. // ThrowErrorImpl Method
  165. inline void __stdcall ThrowErrorImpl(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription)
  166. {
  167. IErrorInfoPtr spErrorInfo = AdmtCreateErrorInfo(clsid, iid, ce, pszDescription);
  168. if (spErrorInfo)
  169. {
  170. _com_raise_error(ce.Error(), spErrorInfo.Detach());
  171. }
  172. else
  173. {
  174. _com_raise_error(ce.Error());
  175. }
  176. }
  177. // SetErrorImpl Method
  178. inline HRESULT __stdcall SetErrorImpl(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription)
  179. {
  180. IErrorInfoPtr spErrorInfo = AdmtCreateErrorInfo(clsid, iid, ce, pszDescription);
  181. if (spErrorInfo)
  182. {
  183. SetErrorInfo(0, spErrorInfo);
  184. }
  185. return ce.Error();
  186. }
  187. } // namespace
  188. //---------------------------------------------------------------------------
  189. // Error Methods
  190. //---------------------------------------------------------------------------
  191. //
  192. // ThrowError Methods
  193. //
  194. void __cdecl ThrowError(_com_error ce, UINT uId, ...)
  195. {
  196. _TCHAR szFormat[FORMAT_BUFFER_SIZE];
  197. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  198. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, COUNT_OF(szFormat)))
  199. {
  200. va_list args;
  201. va_start(args, uId);
  202. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  203. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  204. va_end(args);
  205. }
  206. else
  207. {
  208. szDescription[0] = _T('\0');
  209. }
  210. ThrowErrorImpl(GUID_NULL, GUID_NULL, ce, szDescription);
  211. }
  212. void __cdecl ThrowError(_com_error ce, LPCTSTR pszFormat, ...)
  213. {
  214. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  215. if (pszFormat)
  216. {
  217. va_list args;
  218. va_start(args, pszFormat);
  219. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  220. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  221. va_end(args);
  222. }
  223. else
  224. {
  225. szDescription[0] = _T('\0');
  226. }
  227. ThrowErrorImpl(GUID_NULL, GUID_NULL, ce, szDescription);
  228. }
  229. void __cdecl ThrowError(const CLSID& clsid, const IID& iid, _com_error ce, UINT uId, ...)
  230. {
  231. _TCHAR szFormat[FORMAT_BUFFER_SIZE];
  232. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  233. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, COUNT_OF(szFormat)))
  234. {
  235. va_list args;
  236. va_start(args, uId);
  237. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  238. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  239. va_end(args);
  240. }
  241. else
  242. {
  243. szDescription[0] = _T('\0');
  244. }
  245. ThrowErrorImpl(clsid, iid, ce, szDescription);
  246. }
  247. void __cdecl ThrowError(const CLSID& clsid, const IID& iid, _com_error ce, LPCTSTR pszFormat, ...)
  248. {
  249. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  250. if (pszFormat)
  251. {
  252. va_list args;
  253. va_start(args, pszFormat);
  254. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  255. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  256. va_end(args);
  257. }
  258. else
  259. {
  260. szDescription[0] = _T('\0');
  261. }
  262. ThrowErrorImpl(clsid, iid, ce, szDescription);
  263. }
  264. //
  265. // SetError Methods
  266. //
  267. HRESULT __cdecl SetError(_com_error ce, UINT uId, ...)
  268. {
  269. _TCHAR szFormat[FORMAT_BUFFER_SIZE];
  270. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  271. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, COUNT_OF(szFormat)))
  272. {
  273. va_list args;
  274. va_start(args, uId);
  275. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  276. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  277. va_end(args);
  278. }
  279. else
  280. {
  281. szDescription[0] = _T('\0');
  282. }
  283. return SetErrorImpl(GUID_NULL, GUID_NULL, ce, szDescription);
  284. }
  285. HRESULT __cdecl SetError(_com_error ce, LPCTSTR pszFormat, ...)
  286. {
  287. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  288. if (pszFormat)
  289. {
  290. va_list args;
  291. va_start(args, pszFormat);
  292. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  293. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  294. va_end(args);
  295. }
  296. else
  297. {
  298. szDescription[0] = _T('\0');
  299. }
  300. return SetErrorImpl(GUID_NULL, GUID_NULL, ce, szDescription);
  301. }
  302. HRESULT __cdecl SetError(const CLSID& clsid, const IID& iid, _com_error ce, UINT uId, ...)
  303. {
  304. _TCHAR szFormat[FORMAT_BUFFER_SIZE];
  305. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  306. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, COUNT_OF(szFormat)))
  307. {
  308. va_list args;
  309. va_start(args, uId);
  310. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  311. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  312. va_end(args);
  313. }
  314. else
  315. {
  316. szDescription[0] = _T('\0');
  317. }
  318. return SetErrorImpl(clsid, iid, ce, szDescription);
  319. }
  320. HRESULT __cdecl SetError(const CLSID& clsid, const IID& iid, _com_error ce, LPCTSTR pszFormat, ...)
  321. {
  322. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  323. if (pszFormat)
  324. {
  325. va_list args;
  326. va_start(args, pszFormat);
  327. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  328. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  329. va_end(args);
  330. }
  331. else
  332. {
  333. szDescription[0] = _T('\0');
  334. }
  335. return SetErrorImpl(clsid, iid, ce, szDescription);
  336. }