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