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