PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/include/comet/error.h

https://bitbucket.org/sofusmortensen/comet
C Header | 365 lines | 251 code | 41 blank | 73 comment | 35 complexity | 3262f6e2d9f820b41645cf478c993635 MD5 | raw file
  1. /** \file
  2. * Provide COM Error support.
  3. */
  4. /*
  5. * Copyright Š 2000, 2001 Sofus Mortensen
  6. *
  7. * This material is provided "as is", with absolutely no warranty
  8. * expressed or implied. Any use is at your own risk. Permission to
  9. * use or copy this software for any purpose is hereby granted without
  10. * fee, provided the above notices are retained on all copies.
  11. * Permission to modify the code and to distribute modified code is
  12. * granted, provided the above notices are retained, and a notice that
  13. * the code was modified is included with the above copyright notice.
  14. *
  15. * This header is part of comet.
  16. * http://www.lambdasoft.dk/comet
  17. */
  18. #ifndef COMET_ERROR_H
  19. #define COMET_ERROR_H
  20. #include <comet/config.h>
  21. #include <stdexcept>
  22. #include <string>
  23. #include <comet/error_fwd.h>
  24. #include <comet/bstr.h>
  25. #include <comet/ptr.h>
  26. #include <comet/uuid_fwd.h>
  27. #pragma warning(push)
  28. #pragma warning(disable : 4290)
  29. namespace comet {
  30. namespace impl {
  31. inline com_ptr<IErrorInfo> GetErrorInfo() throw()
  32. {
  33. IErrorInfo* ei = 0;
  34. ::GetErrorInfo(0, &ei);
  35. return com_ptr<IErrorInfo>(auto_attach(ei));
  36. }
  37. inline com_ptr<ICreateErrorInfo> CreateErrorInfo() throw()
  38. {
  39. ICreateErrorInfo* cei = 0;
  40. ::CreateErrorInfo(&cei);
  41. return com_ptr<ICreateErrorInfo>(auto_attach(cei));
  42. }
  43. template<typename Itf> inline bool supports_ErrorInfo(Itf* p)
  44. {
  45. com_ptr<ISupportErrorInfo> q = com_cast(com_ptr<Itf>(p));
  46. if (q == 0) return false;
  47. return S_OK == q->InterfaceSupportsErrorInfo(uuidof<Itf>());
  48. }
  49. }
  50. /** \class com_error error.h comet/error.h
  51. * COM error.
  52. */
  53. class com_error : public std::runtime_error
  54. {
  55. public:
  56. //! Construct com_error from HRESULT.
  57. /*!
  58. \param hr
  59. HRESULT value of error.
  60. */
  61. explicit com_error(HRESULT hr) : hr_(hr), std::runtime_error("")
  62. {}
  63. //! Construct com_error from HRESULT and textual description.
  64. /*!
  65. \param msg
  66. Description of error.
  67. \param hr
  68. HRESULT value of error.
  69. */
  70. explicit com_error(const bstr_t& msg, HRESULT hr = E_FAIL) : hr_(hr), std::runtime_error("")
  71. {
  72. com_ptr<ICreateErrorInfo> cei(impl::CreateErrorInfo());
  73. if ( !cei.is_null() ) {
  74. try {
  75. cei->SetDescription(msg.in());
  76. ei_ = com_ptr<IErrorInfo>( com_cast(cei)) ;
  77. } catch (std::bad_alloc&)
  78. {}
  79. }
  80. }
  81. //! Construct com_error from HRESULT, textual description, source, iid, help.
  82. /*!
  83. \param msg Description of error.
  84. \param hr HRESULT value of error.
  85. \param src Description of source line
  86. \param iid Interface the error was on
  87. \param helpFile Name of help file
  88. \param helpContext Name of help Context
  89. */
  90. explicit com_error(const bstr_t &msg, HRESULT hr, const bstr_t &src, const uuid_t &iid = uuid_t(),
  91. const bstr_t &helpFile=bstr_t(), DWORD helpContext = -1)
  92. : hr_(hr), std::runtime_error("")
  93. {
  94. com_ptr<ICreateErrorInfo> cei(impl::CreateErrorInfo());
  95. if ( ! cei.is_null() )
  96. {
  97. try {
  98. cei->SetDescription(msg.in());
  99. if (!src.is_null() )
  100. cei->SetSource( src.in() );
  101. if (iid != uuid_t())
  102. cei->SetGUID( iid );
  103. if (!helpFile.is_null())
  104. {
  105. cei->SetHelpFile( helpFile.in() );
  106. cei->SetHelpContext( helpContext );
  107. }
  108. ei_ = com_ptr<IErrorInfo>( com_cast(cei)) ;
  109. } catch (std::bad_alloc&)
  110. {}
  111. }
  112. }
  113. /// Construct with an error-info and hresult.
  114. explicit com_error(HRESULT hr, const com_ptr<IErrorInfo>& ei) : hr_(hr), ei_(ei), std::runtime_error("")
  115. {}
  116. public:
  117. //! Return a string with a description of the error
  118. /*!
  119. what() uses Description from IErrorInfo if such is present, otherwise FormatMessage is used
  120. to create a description of the HRESULT error value.
  121. \retval
  122. A const char* string with a textual description of the error.
  123. */
  124. const char* what() const throw()
  125. {
  126. try {
  127. if (what_.empty()) {
  128. what_ = s_str();
  129. }
  130. } catch (std::bad_alloc&) {
  131. return 0;
  132. }
  133. return what_.c_str();
  134. }
  135. /// Returns a std::string with a description of the error.
  136. std::string s_str() const
  137. {
  138. std::string ret;
  139. get_str(ret);
  140. return ret;
  141. }
  142. /// Returns a std::wstring with a description of the error.
  143. std::wstring w_str() const
  144. {
  145. std::wstring ret;
  146. get_str(ret);
  147. return ret;
  148. }
  149. /// Returns a tstring with a description of the error.
  150. tstring t_str() const
  151. {
  152. tstring ret;
  153. get_str(ret);
  154. return ret;
  155. }
  156. private:
  157. void get_str( std::string &ret) const throw()
  158. {
  159. if (ei_.is_null() == false) {
  160. bstr_t bs;
  161. if (SUCCEEDED(ei_->GetDescription(bs.out())) && !bs.is_empty()) {
  162. ret= bs.s_str();
  163. return;
  164. }
  165. }
  166. LPVOID lpMsgBuf;
  167. if (FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  168. NULL, hr_, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  169. (LPSTR) &lpMsgBuf, 0, NULL))
  170. {
  171. ret = (const char*)lpMsgBuf;
  172. LocalFree(lpMsgBuf);
  173. }
  174. }
  175. void get_str( std::wstring &ret) const throw()
  176. {
  177. if (ei_.is_null() == false) {
  178. bstr_t bs;
  179. if (SUCCEEDED(ei_->GetDescription(bs.out())) && !bs.is_empty()) {
  180. ret = bs.w_str();
  181. return;
  182. }
  183. }
  184. LPVOID lpMsgBuf;
  185. if (FormatMessageW( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
  186. NULL, hr_, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
  187. (LPWSTR) &lpMsgBuf, 0, NULL))
  188. {
  189. ret = (const wchar_t*)lpMsgBuf;
  190. LocalFree(lpMsgBuf);
  191. }
  192. }
  193. public:
  194. /** Return the HRESULT for the error.
  195. */
  196. HRESULT hr() const throw()
  197. {
  198. return hr_;
  199. }
  200. /// \name wrappers for IErrorInfo
  201. //@{
  202. /// Description of the error.
  203. bstr_t description() const
  204. {
  205. bstr_t rv;
  206. if (!ei_.is_null()) ei_->GetDescription( rv.out() ) | raise_exception;
  207. return rv;
  208. }
  209. /// The error source description.
  210. bstr_t source() const
  211. {
  212. bstr_t rv;
  213. if (!ei_.is_null()) ei_->GetSource( rv.out() ) | raise_exception;
  214. return rv;
  215. }
  216. /// Interface IID.
  217. GUID guid() const
  218. {
  219. GUID rv;
  220. if (!ei_.is_null()) ei_->GetGUID( &rv ) | raise_exception;
  221. else ZeroMemory(&rv, sizeof(rv));
  222. return rv;
  223. }
  224. /// Helpfile name.
  225. bstr_t help_file() const
  226. {
  227. bstr_t rv;
  228. if (!ei_.is_null()) ei_->GetHelpFile( rv.out() ) | raise_exception;
  229. return rv;
  230. }
  231. /// Help conext.
  232. DWORD help_context() const
  233. {
  234. DWORD rv = 0;
  235. if (!ei_.is_null()) ei_->GetHelpContext( &rv ) | raise_exception;
  236. return rv;
  237. }
  238. //@}
  239. /// Return the error-info object.
  240. com_ptr<IErrorInfo> get_ei() const
  241. {
  242. return ei_;
  243. }
  244. private:
  245. mutable std::string what_;
  246. com_ptr<IErrorInfo> ei_;
  247. HRESULT hr_;
  248. };
  249. namespace impl {
  250. inline HRESULT return_com_error(HRESULT hr, const bstr_t &desc, const bstr_t &src = auto_attach(BSTR(NULL)),
  251. const uuid_t &iid=CLSID_NULL, const bstr_t &helpFile=bstr_t(), DWORD helpContext = -1)
  252. {
  253. com_ptr<ICreateErrorInfo> cei(impl::CreateErrorInfo());
  254. if (cei.is_null() == false) {
  255. try {
  256. cei->SetDescription(desc.in());
  257. if (!src.is_null() )
  258. cei->SetSource( src.in() );
  259. if (iid != uuid_t())
  260. cei->SetGUID( iid );
  261. if (!helpFile.is_null())
  262. {
  263. cei->SetHelpFile( helpFile.in() );
  264. cei->SetHelpContext( helpContext );
  265. }
  266. com_ptr<IErrorInfo> ei = com_cast(cei);
  267. ::SetErrorInfo(0, ei.in());
  268. } catch (std::bad_alloc&)
  269. {}
  270. }
  271. return hr;
  272. }
  273. inline HRESULT return_com_error(const std::exception& err, const bstr_t &src = auto_attach(BSTR(NULL)),
  274. const uuid_t &iid=CLSID_NULL, const bstr_t &helpFile=bstr_t(), DWORD helpContext = -1)
  275. {
  276. return return_com_error( E_FAIL, bstr_t(err.what()),src,iid,helpFile, helpContext );
  277. }
  278. inline HRESULT return_com_error(const com_error& err, const bstr_t &src = bstr_t(), const uuid_t &iid = CLSID_NULL) throw()
  279. {
  280. const bstr_t &cursrc =err.source();
  281. const uuid_t &curiid =err.guid();
  282. // Return error info with more info.
  283. return return_com_error( err.hr(), err.description(), cursrc.is_null()?src:cursrc,
  284. curiid.is_null()?iid:curiid, err.help_file(), err.help_context());
  285. }
  286. inline void throw_com_error_(HRESULT hr, const com_ptr<IErrorInfo>& ei)
  287. {
  288. throw_error_handler<true>::throw_error(hr, ei);
  289. }
  290. // Raising an HRESULT with a message
  291. inline void raise_exception_t::operator()(const bstr_t& msg, HRESULT hr/* = E_FAIL*/) const
  292. {
  293. throw com_error(msg, hr);
  294. }
  295. // Raising an HRESULT
  296. inline void raise_exception_t::operator()(HRESULT hr) const
  297. {
  298. throw com_error(hr);
  299. }
  300. } // namespace impl
  301. /*! \addtogroup ErrorHandling
  302. */
  303. //@{
  304. /** Throw COM error using com_error, using HRESULT and IErrorInfo.
  305. */
  306. template<typename Itf> inline void throw_com_error(Itf* p, HRESULT hr)
  307. {
  308. if (impl::supports_ErrorInfo(p))
  309. {
  310. com_ptr<IErrorInfo> ei = impl::GetErrorInfo();
  311. if (ei.is_null() == false) impl::throw_com_error_(hr, ei);
  312. }
  313. throw com_error(hr);
  314. }
  315. template<bool OVERRIDE>
  316. inline void throw_error_handler<OVERRIDE>::throw_error(HRESULT hr, const com_ptr<IErrorInfo> &ei)
  317. {
  318. throw com_error(hr, ei);
  319. }
  320. //@}
  321. }
  322. #pragma warning(pop)
  323. #endif