Source code of Windows XP (NT5)
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.

387 lines
7.3 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. spErrorInfo->GetSource(&bstrSource);
  40. spCreateErrorInfo->SetSource(bstrSource);
  41. SysFreeString(bstrSource);
  42. }
  43. else
  44. {
  45. spCreateErrorInfo->SetSource(L"");
  46. }
  47. // GUID
  48. bool bInterfaceSpecified = false;
  49. if (IsEqualIID(iid, GUID_NULL) == FALSE)
  50. {
  51. spCreateErrorInfo->SetGUID(iid);
  52. bInterfaceSpecified = true;
  53. }
  54. else if (spErrorInfo)
  55. {
  56. GUID guid;
  57. spErrorInfo->GetGUID(&guid);
  58. spCreateErrorInfo->SetGUID(guid);
  59. }
  60. else
  61. {
  62. spCreateErrorInfo->SetGUID(GUID_NULL);
  63. }
  64. // description
  65. _bstr_t strDescription = pszDescription;
  66. if (spErrorInfo)
  67. {
  68. /*
  69. BSTR bstrSource;
  70. spErrorInfo->GetSource(&bstrSource);
  71. if (SysStringLen(bstrSource) > 0)
  72. {
  73. if (strDescription.length() > 0)
  74. {
  75. strDescription += _T(" : ");
  76. }
  77. strDescription += bstrSource;
  78. }
  79. SysFreeString(bstrSource);
  80. */
  81. BSTR bstrDescription;
  82. spErrorInfo->GetDescription(&bstrDescription);
  83. if (SysStringLen(bstrDescription) > 0)
  84. {
  85. if (strDescription.length() > 0)
  86. {
  87. strDescription += _T(" ");
  88. }
  89. strDescription += bstrDescription;
  90. }
  91. else if (bInterfaceSpecified == false)
  92. {
  93. LPCTSTR pszErrorMessage = ce.ErrorMessage();
  94. if (pszErrorMessage)
  95. {
  96. if (strDescription.length() > 0)
  97. {
  98. strDescription += _T(" ");
  99. }
  100. strDescription += pszErrorMessage;
  101. }
  102. }
  103. SysFreeString(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. spCreateErrorInfo->SetDescription(strDescription);
  118. // help file
  119. if (spErrorInfo)
  120. {
  121. BSTR bstrHelpFile;
  122. spErrorInfo->GetHelpFile(&bstrHelpFile);
  123. spCreateErrorInfo->SetHelpFile(bstrHelpFile);
  124. SysFreeString(bstrHelpFile);
  125. }
  126. else
  127. {
  128. spCreateErrorInfo->SetHelpFile(L"");
  129. }
  130. // help context
  131. DWORD dwHelpContext = 0;
  132. if (spErrorInfo)
  133. {
  134. spErrorInfo->GetHelpContext(&dwHelpContext);
  135. }
  136. spCreateErrorInfo->SetHelpContext(dwHelpContext);
  137. }
  138. return IErrorInfoPtr(spCreateErrorInfo);
  139. }
  140. // ThrowErrorImpl Method
  141. inline void __stdcall ThrowErrorImpl(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription)
  142. {
  143. IErrorInfoPtr spErrorInfo = AdmtCreateErrorInfo(clsid, iid, ce, pszDescription);
  144. if (spErrorInfo)
  145. {
  146. _com_raise_error(ce.Error(), spErrorInfo.Detach());
  147. }
  148. else
  149. {
  150. _com_raise_error(ce.Error());
  151. }
  152. }
  153. // SetErrorImpl Method
  154. inline HRESULT __stdcall SetErrorImpl(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription)
  155. {
  156. SetErrorInfo(0, AdmtCreateErrorInfo(clsid, iid, ce, pszDescription));
  157. return ce.Error();
  158. }
  159. } // namespace
  160. //---------------------------------------------------------------------------
  161. // Error Methods
  162. //---------------------------------------------------------------------------
  163. //
  164. // ThrowError Methods
  165. //
  166. void __cdecl ThrowError(_com_error ce, UINT uId, ...)
  167. {
  168. _TCHAR szFormat[FORMAT_BUFFER_SIZE];
  169. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  170. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, COUNT_OF(szFormat)))
  171. {
  172. va_list args;
  173. va_start(args, uId);
  174. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  175. va_end(args);
  176. }
  177. else
  178. {
  179. szDescription[0] = _T('\0');
  180. }
  181. ThrowErrorImpl(GUID_NULL, GUID_NULL, ce, szDescription);
  182. }
  183. void __cdecl ThrowError(_com_error ce, LPCTSTR pszFormat, ...)
  184. {
  185. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  186. if (pszFormat)
  187. {
  188. va_list args;
  189. va_start(args, pszFormat);
  190. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  191. va_end(args);
  192. }
  193. else
  194. {
  195. szDescription[0] = _T('\0');
  196. }
  197. ThrowErrorImpl(GUID_NULL, GUID_NULL, ce, szDescription);
  198. }
  199. void __cdecl ThrowError(const CLSID& clsid, const IID& iid, _com_error ce, UINT uId, ...)
  200. {
  201. _TCHAR szFormat[FORMAT_BUFFER_SIZE];
  202. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  203. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, COUNT_OF(szFormat)))
  204. {
  205. va_list args;
  206. va_start(args, uId);
  207. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  208. va_end(args);
  209. }
  210. else
  211. {
  212. szDescription[0] = _T('\0');
  213. }
  214. ThrowErrorImpl(clsid, iid, ce, szDescription);
  215. }
  216. void __cdecl ThrowError(const CLSID& clsid, const IID& iid, _com_error ce, LPCTSTR pszFormat, ...)
  217. {
  218. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  219. if (pszFormat)
  220. {
  221. va_list args;
  222. va_start(args, pszFormat);
  223. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  224. va_end(args);
  225. }
  226. else
  227. {
  228. szDescription[0] = _T('\0');
  229. }
  230. ThrowErrorImpl(clsid, iid, ce, szDescription);
  231. }
  232. //
  233. // SetError Methods
  234. //
  235. HRESULT __cdecl SetError(_com_error ce, UINT uId, ...)
  236. {
  237. _TCHAR szFormat[FORMAT_BUFFER_SIZE];
  238. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  239. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, COUNT_OF(szFormat)))
  240. {
  241. va_list args;
  242. va_start(args, uId);
  243. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  244. va_end(args);
  245. }
  246. else
  247. {
  248. szDescription[0] = _T('\0');
  249. }
  250. return SetErrorImpl(GUID_NULL, GUID_NULL, ce, szDescription);
  251. }
  252. HRESULT __cdecl SetError(_com_error ce, LPCTSTR pszFormat, ...)
  253. {
  254. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  255. if (pszFormat)
  256. {
  257. va_list args;
  258. va_start(args, pszFormat);
  259. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  260. va_end(args);
  261. }
  262. else
  263. {
  264. szDescription[0] = _T('\0');
  265. }
  266. return SetErrorImpl(GUID_NULL, GUID_NULL, ce, szDescription);
  267. }
  268. HRESULT __cdecl SetError(const CLSID& clsid, const IID& iid, _com_error ce, UINT uId, ...)
  269. {
  270. _TCHAR szFormat[FORMAT_BUFFER_SIZE];
  271. _TCHAR szDescription[DESCRIPTION_BUFFER_SIZE];
  272. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, COUNT_OF(szFormat)))
  273. {
  274. va_list args;
  275. va_start(args, uId);
  276. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  277. va_end(args);
  278. }
  279. else
  280. {
  281. szDescription[0] = _T('\0');
  282. }
  283. return SetErrorImpl(clsid, iid, ce, szDescription);
  284. }
  285. HRESULT __cdecl SetError(const CLSID& clsid, const IID& iid, _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. va_end(args);
  294. }
  295. else
  296. {
  297. szDescription[0] = _T('\0');
  298. }
  299. return SetErrorImpl(clsid, iid, ce, szDescription);
  300. }