]> git.saurik.com Git - wxWidgets.git/blob - include/wx/msw/ole/oleutils.h
6409b6ffeff6941e33eec5fe3211049655f48598
[wxWidgets.git] / include / wx / msw / ole / oleutils.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: oleutils.h
3 // Purpose: OLE helper routines, OLE debugging support &c
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 19.02.1998
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 #ifndef _WX_OLEUTILS_H
13 #define _WX_OLEUTILS_H
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "oleutils.h"
17 #endif
18
19 #include "wx/defs.h"
20
21 #if wxUSE_OLE
22
23 // ole2.h includes windows.h, so include wrapwin.h first
24 #include "wx/msw/wrapwin.h"
25 // get IUnknown, REFIID &c
26 #include <ole2.h>
27 #include "wx/intl.h"
28 #include "wx/log.h"
29
30 // ============================================================================
31 // General purpose functions and macros
32 // ============================================================================
33
34 // ----------------------------------------------------------------------------
35 // initialize/cleanup OLE
36 // ----------------------------------------------------------------------------
37
38 // call OleInitialize() or CoInitialize[Ex]() depending on the platform
39 //
40 // return true if ok, false otherwise
41 inline bool wxOleInitialize()
42 {
43 // we need to initialize OLE library
44 #ifdef __WXWINCE__
45 if ( FAILED(::CoInitializeEx(NULL, COINIT_MULTITHREADED)) )
46 #else
47 if ( FAILED(::OleInitialize(NULL)) )
48 #endif
49 {
50 wxLogError(_("Cannot initialize OLE"));
51
52 return false;
53 }
54
55 return true;
56 }
57
58 inline void wxOleUninitialize()
59 {
60 #ifdef __WXWINCE__
61 ::CoUninitialize();
62 #else
63 ::OleUninitialize();
64 #endif
65 }
66
67 // ----------------------------------------------------------------------------
68 // misc helper functions/macros
69 // ----------------------------------------------------------------------------
70
71 // release the interface pointer (if !NULL)
72 inline void ReleaseInterface(IUnknown *pIUnk)
73 {
74 if ( pIUnk != NULL )
75 pIUnk->Release();
76 }
77
78 // release the interface pointer (if !NULL) and make it NULL
79 #define RELEASE_AND_NULL(p) if ( (p) != NULL ) { p->Release(); p = NULL; };
80
81 // return true if the iid is in the array
82 extern bool IsIidFromList(REFIID riid, const IID *aIids[], size_t nCount);
83
84 // ============================================================================
85 // IUnknown implementation helpers
86 // ============================================================================
87
88 /*
89 The most dumb implementation of IUnknown methods. We don't support
90 aggregation nor containment, but for 99% of cases this simple
91 implementation is quite enough.
92
93 Usage is trivial: here is all you should have
94 1) DECLARE_IUNKNOWN_METHODS in your (IUnknown derived!) class declaration
95 2) BEGIN/END_IID_TABLE with ADD_IID in between for all interfaces you
96 support (at least all for which you intent to return 'this' from QI,
97 i.e. you should derive from IFoo if you have ADD_IID(Foo)) somewhere else
98 3) IMPLEMENT_IUNKNOWN_METHODS somewhere also
99
100 These macros are quite simple: AddRef and Release are trivial and QI does
101 lookup in a static member array of IIDs and returns 'this' if it founds
102 the requested interface in it or E_NOINTERFACE if not.
103 */
104
105 /*
106 wxAutoULong: this class is used for automatically initalising m_cRef to 0
107 */
108 class wxAutoULong
109 {
110 public:
111 wxAutoULong(ULONG value = 0) : m_Value(value) { }
112
113 operator ULONG&() { return m_Value; }
114 ULONG& operator=(ULONG value) { m_Value = value; return m_Value; }
115
116 wxAutoULong& operator++() { ++m_Value; return *this; }
117 const wxAutoULong operator++( int ) { wxAutoULong temp = *this; ++m_Value; return temp; }
118
119 wxAutoULong& operator--() { --m_Value; return *this; }
120 const wxAutoULong operator--( int ) { wxAutoULong temp = *this; --m_Value; return temp; }
121
122 private:
123 ULONG m_Value;
124 };
125
126 // declare the methods and the member variable containing reference count
127 // you must also define the ms_aIids array somewhere with BEGIN_IID_TABLE
128 // and friends (see below)
129
130 #define DECLARE_IUNKNOWN_METHODS \
131 public: \
132 STDMETHODIMP QueryInterface(REFIID, void **); \
133 STDMETHODIMP_(ULONG) AddRef(); \
134 STDMETHODIMP_(ULONG) Release(); \
135 private: \
136 static const IID *ms_aIids[]; \
137 wxAutoULong m_cRef
138
139 // macros for declaring supported interfaces
140 // NB: you should write ADD_INTERFACE(Foo) and not ADD_INTERFACE(IID_IFoo)!
141 #define BEGIN_IID_TABLE(cname) const IID *cname::ms_aIids[] = {
142 #define ADD_IID(iid) &IID_I##iid,
143 #define END_IID_TABLE }
144
145 // implementation is as straightforward as possible
146 // Parameter: classname - the name of the class
147 #define IMPLEMENT_IUNKNOWN_METHODS(classname) \
148 STDMETHODIMP classname::QueryInterface(REFIID riid, void **ppv) \
149 { \
150 wxLogQueryInterface(_T(#classname), riid); \
151 \
152 if ( IsIidFromList(riid, ms_aIids, WXSIZEOF(ms_aIids)) ) { \
153 *ppv = this; \
154 AddRef(); \
155 \
156 return S_OK; \
157 } \
158 else { \
159 *ppv = NULL; \
160 \
161 return (HRESULT) E_NOINTERFACE; \
162 } \
163 } \
164 \
165 STDMETHODIMP_(ULONG) classname::AddRef() \
166 { \
167 wxLogAddRef(_T(#classname), m_cRef); \
168 \
169 return ++m_cRef; \
170 } \
171 \
172 STDMETHODIMP_(ULONG) classname::Release() \
173 { \
174 wxLogRelease(_T(#classname), m_cRef); \
175 \
176 if ( --m_cRef == wxAutoULong(0) ) { \
177 delete this; \
178 return 0; \
179 } \
180 else \
181 return m_cRef; \
182 }
183
184 // ============================================================================
185 // Debugging support
186 // ============================================================================
187
188 // VZ: I don't know it's not done for compilers other than VC++ but I leave it
189 // as is. Please note, though, that tracing OLE interface calls may be
190 // incredibly useful when debugging OLE programs.
191 #if defined(__WXDEBUG__) && ( ( defined(__VISUALC__) && (__VISUALC__ >= 1000) ) || defined(__MWERKS__) )
192 // ----------------------------------------------------------------------------
193 // All OLE specific log functions have DebugTrace level (as LogTrace)
194 // ----------------------------------------------------------------------------
195
196 // tries to translate riid into a symbolic name, if possible
197 void wxLogQueryInterface(const wxChar *szInterface, REFIID riid);
198
199 // these functions print out the new value of reference counter
200 void wxLogAddRef (const wxChar *szInterface, ULONG cRef);
201 void wxLogRelease(const wxChar *szInterface, ULONG cRef);
202
203 #else //!__WXDEBUG__
204 #define wxLogQueryInterface(szInterface, riid)
205 #define wxLogAddRef(szInterface, cRef)
206 #define wxLogRelease(szInterface, cRef)
207 #endif //__WXDEBUG__
208
209 // wrapper around BSTR type (by Vadim Zeitlin)
210
211 class WXDLLEXPORT wxBasicString
212 {
213 public:
214 // ctors & dtor
215 wxBasicString(const char *sz);
216 wxBasicString(const wxString& str);
217 ~wxBasicString();
218
219 void Init(const char* sz);
220
221 // accessors
222 // just get the string
223 operator BSTR() const { return m_wzBuf; }
224 // retrieve a copy of our string - caller must SysFreeString() it later!
225 BSTR Get() const { return SysAllocString(m_wzBuf); }
226
227 private:
228 // @@@ not implemented (but should be)
229 wxBasicString(const wxBasicString&);
230 wxBasicString& operator=(const wxBasicString&);
231
232 OLECHAR *m_wzBuf; // actual string
233 };
234
235 // Convert variants
236 class WXDLLIMPEXP_BASE wxVariant;
237
238 bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant) ;
239 bool wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant) ;
240
241 // Convert string to Unicode
242 BSTR wxConvertStringToOle(const wxString& str);
243
244 // Convert string from BSTR to wxString
245 wxString wxConvertStringFromOle(BSTR bStr);
246
247 #else // !wxUSE_OLE
248
249 // ----------------------------------------------------------------------------
250 // stub functions to avoid #if wxUSE_OLE in the main code
251 // ----------------------------------------------------------------------------
252
253 inline bool wxOleInitialize() { return false; }
254 inline void wxOleUninitialize() { }
255
256 #endif // wxUSE_OLE/!wxUSE_OLE
257
258 #endif //_WX_OLEUTILS_H