1 /*
2  * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 #ifndef AWT_OLE_H
27 #define AWT_OLE_H
28 
29 #include "awt.h"
30 #include <ole2.h>
31 #include <comdef.h>
32 #include <comutil.h>
33 
34 #ifdef _DEBUG
35     #define _SUN_DEBUG
36 #endif
37 
38 
39 #ifndef SUN_DBG_NS
40   #ifdef _LIB
41     #define SUN_DBG_NS SUN_dbg_lib
42   #else
43     #define SUN_DBG_NS SUN_dbg_glb
44   #endif //_LIB
45 #endif //SUN_DBG_NS
46 
47 
48 #ifndef  TRACE_SUFFIX
49   #define TRACE_SUFFIX
50 #endif
51 
52 namespace SUN_DBG_NS{
53   LPCTSTR CreateTimeStamp(LPTSTR lpBuffer, size_t iBufferSize);
snTraceEmp(LPCTSTR,...)54   inline void snTraceEmp(LPCTSTR, ...) { }
55   void snvTrace(LPCTSTR lpszFormat, va_list argList);
56   void snTrace(LPCTSTR lpszFormat, ... );
57 }//SUN_DBG_NS namespace end
58 
59 #define STRACE1       SUN_DBG_NS::snTrace
60 #ifdef _SUN_DEBUG
61   #define STRACE      SUN_DBG_NS::snTrace
62 #else
63   #define STRACE      SUN_DBG_NS::snTraceEmp
64 #endif
65 #define STRACE0       SUN_DBG_NS::snTraceEmp
66 
67 struct CLogEntryPoint1 {
68     LPCTSTR m_lpTitle;
CLogEntryPoint1CLogEntryPoint169     CLogEntryPoint1(LPCTSTR lpTitle):m_lpTitle(lpTitle) { STRACE(_T("{%s"), m_lpTitle); }
~CLogEntryPoint1CLogEntryPoint170     ~CLogEntryPoint1(){ STRACE(_T("}%s"), m_lpTitle); }
71 };
72 struct CLogEntryPoint0 {
73     LPCTSTR m_lpTitle;
CLogEntryPoint0CLogEntryPoint074     CLogEntryPoint0(LPCTSTR lpTitle):m_lpTitle(lpTitle) { STRACE0(_T("{%s"), m_lpTitle); }
~CLogEntryPoint0CLogEntryPoint075     ~CLogEntryPoint0(){ STRACE0(_T("}%s"), m_lpTitle); }
76 };
77 
78 #define SEP1(msg)    CLogEntryPoint1 _ep1_(msg);
79 #define SEP0(msg)    CLogEntryPoint0 _ep0_(msg);
80 #ifdef  _SUN_DEBUG
81   #define SEP(msg)   CLogEntryPoint1 _ep1_(msg);
82 #else
83   #define SEP(msg)   CLogEntryPoint0 _ep0_(msg);
84 #endif
85 
86 
87 #define OLE_BAD_COOKIE ((DWORD)-1)
88 
89 #define OLE_TRACENOTIMPL(msg)\
90         STRACE(_T("Warning:%s"), msg);\
91         return E_NOTIMPL;
92 
93 #define OLE_TRACEOK(msg)\
94         STRACE0(_T("Info:%s"), msg);\
95         return S_OK;
96 
97 
98 #define OLE_DECL\
99         HRESULT _hr_ = S_OK;
100 
101 #define OLE_NEXT_TRY\
102         try {
103 
104 #define OLE_TRY\
105         OLE_DECL\
106         try {
107 
108 #define OLE_HRT(fnc)\
109         _hr_ = fnc;\
110         if (FAILED(_hr_)) {\
111             STRACE1(_T("Error:%08x in ") _T(#fnc),  _hr_);\
112             _com_raise_error(_hr_);\
113         }
114 
115 #define OLE_WINERROR2HR(msg, erCode)\
116         _hr_ = erCode;\
117         STRACE1(_T("OSError:%d in ") msg,  _hr_);\
118         _hr_ = HRESULT_FROM_WIN32(_hr_);
119 
120 #define OLE_THROW_LASTERROR(msg)\
121         OLE_WINERROR2HR(msg, ::GetLastError())\
122         _com_raise_error(_hr_);
123 
124 #define OLE_CHECK_NOTNULL(x)\
125         if (!(x)) {\
126             STRACE1(_T("Null pointer:") _T(#x));\
127             _com_raise_error(_hr_ = E_POINTER);\
128         }
129 
130 #define OLE_CHECK_NOTNULLSP(x)\
131         if (!bool(x)) {\
132             STRACE1(_T("Null pointer:") _T(#x));\
133             _com_raise_error(_hr_ = E_POINTER);\
134         }
135 
136 #define OLE_HRW32(fnc)\
137         _hr_ = fnc;\
138         if (ERROR_SUCCESS != _hr_) {\
139             STRACE1(_T("OSError:%d in ") _T(#fnc),  _hr_);\
140             _com_raise_error(_hr_ = HRESULT_FROM_WIN32(_hr_));\
141         }
142 
143 #define OLE_HRW32_BOOL(fnc)\
144         if (!fnc) {\
145             OLE_THROW_LASTERROR(_T(#fnc))\
146         }
147 
148 #define OLE_CATCH\
149         } catch (_com_error &e) {\
150             _hr_ = e.Error();\
151             STRACE1(_T("COM Error:%08x %s"), _hr_, e.ErrorMessage());\
152         }
153 
154 #define OLE_CATCH_BAD_ALLOC\
155         } catch (_com_error &e) {\
156             _hr_ = e.Error();\
157             STRACE1(_T("COM Error:%08x %s"), _hr_, e.ErrorMessage());\
158         } catch (std::bad_alloc&) {\
159             _hr_ = E_OUTOFMEMORY;\
160             STRACE1(_T("Error: Out of Memory"));\
161         }
162 
163 #define OLE_CATCH_ALL\
164         } catch (_com_error &e) {\
165             _hr_ = e.Error();\
166             STRACE1(_T("COM Error:%08x %s"), _hr_, e.ErrorMessage());\
167         } catch(...) {\
168             _hr_ = E_FAIL;\
169             STRACE1(_T("Error: General Pritection Failor"));\
170         }
171 
172 #define OLE_RETURN_SUCCESS return SUCCEEDED(_hr_);
173 #define OLE_RETURN_HR      return _hr_;
174 #define OLE_HR             _hr_
175 
176 #define _B(x)    _bstr_t(x)
177 #define _BT(x)    (LPCTSTR)_bstr_t(x)
178 #define _V(x)    _variant_t(x)
179 #define _VV(vrt) _variant_t(vrt, false)
180 #define _VE      _variant_t()
181 #define _VB(b)   _variant_t(bool(b))
182 
183 struct OLEHolder
184 {
OLEHolderOLEHolder185     OLEHolder()
186     : m_hr(::OleInitialize(NULL))
187     {}
188 
~OLEHolderOLEHolder189     ~OLEHolder(){}
190     operator bool() const { return S_OK==SUCCEEDED(m_hr); }
191     HRESULT m_hr;
192 };
193 
194 #endif//AWT_OLE_H
195