comdef.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * This file has no copyright assigned and is placed in the Public Domain.
  3. * This file is part of the mingw-w64 runtime package.
  4. * No warranty is given; refer to the file DISCLAIMER.PD within this package.
  5. */
  6. #ifndef _INC_COMDEF
  7. #define _INC_COMDEF
  8. #include <_mingw.h>
  9. #ifndef RC_INVOKED
  10. #ifndef __cplusplus
  11. #error Native Compiler support only available in C++ compiler
  12. #endif
  13. #include <ole2.h>
  14. #include <olectl.h>
  15. #include <comutil.h>
  16. #ifndef WINAPI
  17. #if defined(_ARM_)
  18. #define WINAPI
  19. #else
  20. #define WINAPI __stdcall
  21. #endif
  22. #endif
  23. #ifdef __cplusplus
  24. class _com_error;
  25. void WINAPI _com_issue_errorex(HRESULT,IUnknown*,REFIID);
  26. HRESULT WINAPI _com_dispatch_propget(IDispatch*,DISPID,VARTYPE,void*);
  27. HRESULT __cdecl _com_dispatch_propput(IDispatch*,DISPID,VARTYPE,...);
  28. HRESULT __cdecl _com_dispatch_method(IDispatch*,DISPID,WORD,VARTYPE,void*,const wchar_t*,...);
  29. HRESULT WINAPI _com_dispatch_raw_propget(IDispatch*,DISPID,VARTYPE,void*) throw();
  30. HRESULT __cdecl _com_dispatch_raw_propput(IDispatch*,DISPID,VARTYPE,...) throw();
  31. HRESULT __cdecl _com_dispatch_raw_method(IDispatch*,DISPID,WORD,VARTYPE,void*,const wchar_t*,...) throw();
  32. class _com_error {
  33. public:
  34. _com_error(HRESULT hr,IErrorInfo *perrinfo = NULL,bool fAddRef = false) throw();
  35. _com_error(const _com_error &that) throw();
  36. virtual ~_com_error() throw();
  37. _com_error &operator=(const _com_error &that) throw();
  38. HRESULT Error() const throw();
  39. WORD WCode() const throw();
  40. IErrorInfo *ErrorInfo() const throw();
  41. _bstr_t Description() const;
  42. DWORD HelpContext() const throw();
  43. _bstr_t HelpFile() const;
  44. _bstr_t Source() const;
  45. GUID GUID_() const throw();
  46. const TCHAR *ErrorMessage() const throw();
  47. static HRESULT WCodeToHRESULT(WORD wCode) throw();
  48. static WORD HRESULTToWCode(HRESULT hr) throw();
  49. private:
  50. void Dtor() throw();
  51. void Ctor(const _com_error &that) throw();
  52. enum {
  53. WCODE_HRESULT_FIRST = MAKE_HRESULT(SEVERITY_ERROR,FACILITY_ITF,0x200),WCODE_HRESULT_LAST = MAKE_HRESULT(SEVERITY_ERROR,FACILITY_ITF+1,0) - 1
  54. };
  55. HRESULT m_hresult;
  56. IErrorInfo *m_perrinfo;
  57. mutable TCHAR *m_pszMsg;
  58. };
  59. inline _com_error::_com_error(HRESULT hr,IErrorInfo *perrinfo,bool fAddRef) throw() : m_hresult(hr),m_perrinfo(perrinfo),m_pszMsg(NULL) {
  60. if(m_perrinfo!=NULL && fAddRef) m_perrinfo->AddRef();
  61. }
  62. inline _com_error::_com_error(const _com_error &that) throw() {
  63. Ctor(that);
  64. }
  65. inline _com_error::~_com_error() throw() {
  66. Dtor();
  67. }
  68. inline _com_error &_com_error::operator=(const _com_error &that) throw() {
  69. if(this!=&that) {
  70. Dtor();
  71. Ctor(that);
  72. }
  73. return *this;
  74. }
  75. inline HRESULT _com_error::Error() const throw() { return m_hresult; }
  76. inline WORD _com_error::WCode() const throw() { return HRESULTToWCode(m_hresult); }
  77. inline IErrorInfo *_com_error::ErrorInfo() const throw() {
  78. if(m_perrinfo!=NULL) m_perrinfo->AddRef();
  79. return m_perrinfo;
  80. }
  81. inline _bstr_t _com_error::Description() const {
  82. BSTR bstr = NULL;
  83. if(m_perrinfo!=NULL) m_perrinfo->GetDescription(&bstr);
  84. return _bstr_t(bstr,false);
  85. }
  86. inline DWORD _com_error::HelpContext() const throw() {
  87. DWORD dwHelpContext = 0;
  88. if(m_perrinfo!=NULL) m_perrinfo->GetHelpContext(&dwHelpContext);
  89. return dwHelpContext;
  90. }
  91. inline _bstr_t _com_error::HelpFile() const {
  92. BSTR bstr = NULL;
  93. if(m_perrinfo!=NULL) m_perrinfo->GetHelpFile(&bstr);
  94. return _bstr_t(bstr,false);
  95. }
  96. inline _bstr_t _com_error::Source() const {
  97. BSTR bstr = NULL;
  98. if(m_perrinfo!=NULL) m_perrinfo->GetSource(&bstr);
  99. return _bstr_t(bstr,false);
  100. }
  101. inline _GUID _com_error::GUID_() const throw() {
  102. _GUID guid;
  103. memset (&guid, 0, sizeof (_GUID));
  104. if(m_perrinfo!=NULL) m_perrinfo->GetGUID(&guid);
  105. return guid;
  106. }
  107. inline const TCHAR *_com_error::ErrorMessage() const throw() {
  108. if(!m_pszMsg) {
  109. FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,NULL,m_hresult,MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),(LPTSTR)&m_pszMsg,0,NULL);
  110. if(m_pszMsg!=NULL) {
  111. int nLen = lstrlen(m_pszMsg);
  112. if(nLen > 1 && m_pszMsg[nLen - 1]=='\n') {
  113. m_pszMsg[nLen-1] = 0;
  114. if(m_pszMsg[nLen - 2]=='\r') m_pszMsg[nLen-2] = 0;
  115. }
  116. } else {
  117. m_pszMsg = (LPTSTR)LocalAlloc(0,32 *sizeof(TCHAR));
  118. if(m_pszMsg!=NULL) {
  119. WORD wCode = WCode();
  120. if(wCode!=0) {
  121. _COM_PRINTF_S_1(m_pszMsg,32,TEXT("IDispatch error #%d"),wCode);
  122. } else {
  123. _COM_PRINTF_S_1(m_pszMsg,32,TEXT("Unknown error 0x%0lX"),m_hresult);
  124. }
  125. }
  126. }
  127. }
  128. return m_pszMsg;
  129. }
  130. inline HRESULT _com_error::WCodeToHRESULT(WORD wCode) throw() { return wCode >= 0xFE00 ? WCODE_HRESULT_LAST : WCODE_HRESULT_FIRST + wCode; }
  131. inline WORD _com_error::HRESULTToWCode(HRESULT hr) throw() { return (hr >= WCODE_HRESULT_FIRST && hr <= WCODE_HRESULT_LAST) ? WORD(hr - WCODE_HRESULT_FIRST) : 0; }
  132. inline void _com_error::Dtor() throw() {
  133. if(m_perrinfo!=NULL) m_perrinfo->Release();
  134. if(m_pszMsg!=NULL) LocalFree((HLOCAL)m_pszMsg);
  135. }
  136. inline void _com_error::Ctor(const _com_error &that) throw() {
  137. m_hresult = that.m_hresult;
  138. m_perrinfo = that.m_perrinfo;
  139. m_pszMsg = NULL;
  140. if(m_perrinfo!=NULL) m_perrinfo->AddRef();
  141. }
  142. inline void WINAPI _com_raise_error(HRESULT hr, IErrorInfo *perrinfo = 0) {
  143. #if __EXCEPTIONS
  144. throw _com_error(hr, perrinfo);
  145. #else
  146. /* This is designed to use exceptions. If exceptions are disabled, there is not much we can do here. */
  147. __debugbreak();
  148. #endif
  149. }
  150. __MINGW_SELECTANY void (WINAPI *__mingw_com_error_handler)(HRESULT hr,IErrorInfo *perrinfo) = _com_raise_error;
  151. inline void WINAPI _set_com_error_handler(void (WINAPI *pHandler)(HRESULT hr,IErrorInfo *perrinfo)) {
  152. __mingw_com_error_handler = pHandler;
  153. }
  154. inline void WINAPI _com_issue_error(HRESULT hr) {
  155. __mingw_com_error_handler(hr, NULL);
  156. }
  157. typedef int __missing_type__;
  158. #if !defined(_COM_SMARTPTR)
  159. #if !defined(_INC_COMIP)
  160. #include <comip.h>
  161. #endif
  162. #define _COM_SMARTPTR _com_ptr_t
  163. #define _COM_SMARTPTR_LEVEL2 _com_IIID
  164. #endif
  165. #if defined(_COM_SMARTPTR)
  166. #if !defined(_COM_SMARTPTR_TYPEDEF)
  167. #if defined(_COM_SMARTPTR_LEVEL2)
  168. #ifdef __CRT_UUID_DECL
  169. /* With our __uuidof, its result can't be passed directly as a template argument. We have _com_IIID_getter to work around that. */
  170. #define _COM_SMARTPTR_TYPEDEF(Interface,aIID) inline const IID &__##Interface##_IID_getter(void) { return aIID; } typedef _COM_SMARTPTR< _com_IIID_getter<Interface, __##Interface##_IID_getter > > Interface ## Ptr
  171. #else
  172. #define _COM_SMARTPTR_TYPEDEF(Interface,IID) typedef _COM_SMARTPTR< _COM_SMARTPTR_LEVEL2<Interface, &IID > > Interface ## Ptr
  173. #endif
  174. #else
  175. #define _COM_SMARTPTR_TYPEDEF(Interface,IID) typedef _COM_SMARTPTR<Interface,&IID > Interface ## Ptr
  176. #endif
  177. #endif
  178. #endif
  179. #if !defined(_COM_NO_STANDARD_GUIDS_)
  180. #if defined(__IFontDisp_INTERFACE_DEFINED__)
  181. #if !defined(Font)
  182. struct Font : IFontDisp {};
  183. #endif
  184. _COM_SMARTPTR_TYPEDEF(Font,__uuidof(IDispatch));
  185. #endif
  186. #if defined(__IFontEventsDisp_INTERFACE_DEFINED__)
  187. #if !defined(FontEvents)
  188. struct FontEvents : IFontEventsDisp {};
  189. #endif
  190. _COM_SMARTPTR_TYPEDEF(FontEvents,__uuidof(IDispatch));
  191. #endif
  192. #if defined(__IPictureDisp_INTERFACE_DEFINED__)
  193. #if !defined(Picture)
  194. struct Picture : IPictureDisp {};
  195. #endif
  196. _COM_SMARTPTR_TYPEDEF(Picture,__uuidof(IDispatch));
  197. #endif
  198. #include "comdefsp.h"
  199. #endif
  200. #endif
  201. #endif /* __cplusplus */
  202. #endif