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.

310 lines
6.2 KiB

  1. #include "StdAfx.h"
  2. #include "Error.h"
  3. #include <ComDef.h>
  4. using namespace _com_util;
  5. #define COUNT_OF(a) (sizeof(a) / sizeof(a[0]))
  6. namespace Error_cpp
  7. {
  8. IErrorInfoPtr __stdcall AdmtCreateErrorInfo(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription);
  9. // AdmtSetErrorImpl Method
  10. inline HRESULT __stdcall AdmtSetErrorImpl(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription)
  11. {
  12. SetErrorInfo(0, AdmtCreateErrorInfo(clsid, iid, ce, pszDescription));
  13. return ce.Error();
  14. }
  15. // AdmtThrowErrorImpl Method
  16. inline void __stdcall AdmtThrowErrorImpl(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription)
  17. {
  18. IErrorInfoPtr spErrorInfo = AdmtCreateErrorInfo(clsid, iid, ce, pszDescription);
  19. if (spErrorInfo)
  20. {
  21. _com_raise_error(ce.Error(), spErrorInfo.Detach());
  22. }
  23. else
  24. {
  25. _com_raise_error(ce.Error());
  26. }
  27. }
  28. }
  29. using namespace Error_cpp;
  30. //---------------------------------------------------------------------------
  31. // Error Methods
  32. //---------------------------------------------------------------------------
  33. // AdmtSetError Methods -------------------------------------------------
  34. HRESULT __cdecl AdmtSetError(const CLSID& clsid, const IID& iid, _com_error ce, UINT uId, ...)
  35. {
  36. _TCHAR szFormat[512];
  37. _TCHAR szDescription[1024];
  38. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, 512))
  39. {
  40. va_list args;
  41. va_start(args, uId);
  42. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  43. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  44. va_end(args);
  45. }
  46. else
  47. {
  48. szDescription[0] = _T('\0');
  49. }
  50. return AdmtSetErrorImpl(clsid, iid, ce, szDescription);
  51. }
  52. HRESULT __cdecl AdmtSetError(const CLSID& clsid, const IID& iid, _com_error ce, LPCTSTR pszFormat, ...)
  53. {
  54. _TCHAR szDescription[1024];
  55. if (pszFormat)
  56. {
  57. va_list args;
  58. va_start(args, pszFormat);
  59. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  60. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  61. va_end(args);
  62. }
  63. else
  64. {
  65. szDescription[0] = _T('\0');
  66. }
  67. return AdmtSetErrorImpl(clsid, iid, ce, szDescription);
  68. }
  69. // AdmtThrowError Methods -----------------------------------------------
  70. void __cdecl AdmtThrowError(const CLSID& clsid, const IID& iid, _com_error ce, UINT uId, ...)
  71. {
  72. _TCHAR szFormat[512];
  73. _TCHAR szDescription[1024];
  74. if (LoadString(_Module.GetResourceInstance(), uId, szFormat, 512))
  75. {
  76. va_list args;
  77. va_start(args, uId);
  78. _vsntprintf(szDescription, COUNT_OF(szDescription), szFormat, args);
  79. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  80. va_end(args);
  81. }
  82. else
  83. {
  84. szDescription[0] = _T('\0');
  85. }
  86. AdmtThrowErrorImpl(clsid, iid, ce, szDescription);
  87. }
  88. void __cdecl AdmtThrowError(const CLSID& clsid, const IID& iid, _com_error ce, LPCTSTR pszFormat, ...)
  89. {
  90. _TCHAR szDescription[1024];
  91. if (pszFormat)
  92. {
  93. va_list args;
  94. va_start(args, pszFormat);
  95. _vsntprintf(szDescription, COUNT_OF(szDescription), pszFormat, args);
  96. szDescription[COUNT_OF(szDescription) - 1] = _T('\0');
  97. va_end(args);
  98. }
  99. else
  100. {
  101. szDescription[0] = _T('\0');
  102. }
  103. AdmtThrowErrorImpl(clsid, iid, ce, szDescription);
  104. }
  105. // Implementation -----------------------------------------------------------
  106. namespace Error_cpp
  107. {
  108. // AdmtCreateErrorInfo Method
  109. IErrorInfoPtr __stdcall AdmtCreateErrorInfo(const CLSID& clsid, const IID& iid, const _com_error& ce, LPCTSTR pszDescription)
  110. {
  111. ICreateErrorInfoPtr spCreateErrorInfo;
  112. CreateErrorInfo(&spCreateErrorInfo);
  113. if (spCreateErrorInfo)
  114. {
  115. IErrorInfoPtr spErrorInfo = ce.ErrorInfo();
  116. if (spErrorInfo == NULL)
  117. {
  118. GetErrorInfo(0, &spErrorInfo);
  119. }
  120. // source
  121. if (IsEqualCLSID(clsid, GUID_NULL) == FALSE)
  122. {
  123. LPOLESTR pszProgId;
  124. if (ProgIDFromCLSID(clsid, &pszProgId) == S_OK)
  125. {
  126. spCreateErrorInfo->SetSource(pszProgId);
  127. CoTaskMemFree(pszProgId);
  128. }
  129. else
  130. {
  131. spCreateErrorInfo->SetSource(L"");
  132. }
  133. }
  134. else if (spErrorInfo)
  135. {
  136. BSTR bstrSource;
  137. spErrorInfo->GetSource(&bstrSource);
  138. spCreateErrorInfo->SetSource(bstrSource);
  139. SysFreeString(bstrSource);
  140. }
  141. else
  142. {
  143. spCreateErrorInfo->SetSource(L"");
  144. }
  145. // GUID
  146. if (IsEqualIID(iid, GUID_NULL) == FALSE)
  147. {
  148. spCreateErrorInfo->SetGUID(iid);
  149. }
  150. else if (spErrorInfo)
  151. {
  152. GUID guid;
  153. spErrorInfo->GetGUID(&guid);
  154. spCreateErrorInfo->SetGUID(guid);
  155. }
  156. else
  157. {
  158. spCreateErrorInfo->SetGUID(GUID_NULL);
  159. }
  160. // description
  161. _bstr_t strDescription = pszDescription;
  162. if (spErrorInfo)
  163. {
  164. BSTR bstrSource;
  165. spErrorInfo->GetSource(&bstrSource);
  166. if (SysStringLen(bstrSource) > 0)
  167. {
  168. if (strDescription.length() > 0)
  169. {
  170. strDescription += _T(" : ");
  171. }
  172. strDescription += bstrSource;
  173. }
  174. SysFreeString(bstrSource);
  175. BSTR bstrDescription;
  176. spErrorInfo->GetDescription(&bstrDescription);
  177. if (SysStringLen(bstrDescription) > 0)
  178. {
  179. if (strDescription.length() > 0)
  180. {
  181. strDescription += _T(" ");
  182. }
  183. strDescription += bstrDescription;
  184. }
  185. else
  186. {
  187. LPCTSTR pszErrorMessage = ce.ErrorMessage();
  188. if (pszErrorMessage)
  189. {
  190. if (strDescription.length() > 0)
  191. {
  192. strDescription += _T(" : ");
  193. }
  194. strDescription += pszErrorMessage;
  195. }
  196. }
  197. SysFreeString(bstrDescription);
  198. }
  199. else
  200. {
  201. LPCTSTR pszErrorMessage = ce.ErrorMessage();
  202. if (pszErrorMessage)
  203. {
  204. if (strDescription.length() > 0)
  205. {
  206. strDescription += _T(" ");
  207. }
  208. strDescription += pszErrorMessage;
  209. }
  210. }
  211. spCreateErrorInfo->SetDescription(strDescription);
  212. // help file
  213. if (spErrorInfo)
  214. {
  215. BSTR bstrHelpFile;
  216. spErrorInfo->GetHelpFile(&bstrHelpFile);
  217. spCreateErrorInfo->SetHelpFile(bstrHelpFile);
  218. SysFreeString(bstrHelpFile);
  219. }
  220. else
  221. {
  222. spCreateErrorInfo->SetHelpFile(L"");
  223. }
  224. // help context
  225. DWORD dwHelpContext = 0;
  226. if (spErrorInfo)
  227. {
  228. spErrorInfo->GetHelpContext(&dwHelpContext);
  229. }
  230. spCreateErrorInfo->SetHelpContext(dwHelpContext);
  231. }
  232. return IErrorInfoPtr(spCreateErrorInfo);
  233. }
  234. } // namespace Error_cpp