]>
Commit | Line | Data |
---|---|---|
bbf1f0e5 KB |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: oleutils.h | |
3 | // Purpose: OLE helper routines, OLE debugging support &c | |
4 | // Author: Vadim Zeitlin | |
3f4a0c5b | 5 | // Modified by: |
bbf1f0e5 KB |
6 | // Created: 19.02.1998 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
bbf1f0e5 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
bbcdf8bc JS |
12 | #ifndef _WX_OLEUTILS_H |
13 | #define _WX_OLEUTILS_H | |
bbf1f0e5 | 14 | |
12028905 | 15 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
bbf1f0e5 KB |
16 | #pragma interface "oleutils.h" |
17 | #endif | |
18 | ||
e5ad6961 | 19 | #include "wx/defs.h" |
82df67d9 | 20 | |
360ae33f VZ |
21 | #if wxUSE_OLE |
22 | ||
9dbe0022 MW |
23 | // ole2.h includes windows.h, so include wrapwin.h first |
24 | #include "wx/msw/wrapwin.h" | |
a7b4607f VZ |
25 | // get IUnknown, REFIID &c |
26 | #include <ole2.h> | |
eaac61c3 | 27 | #include "wx/intl.h" |
c493691d | 28 | #include "wx/log.h" |
82df67d9 | 29 | |
bbf1f0e5 KB |
30 | // ============================================================================ |
31 | // General purpose functions and macros | |
32 | // ============================================================================ | |
33 | ||
360ae33f VZ |
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 | ||
bbf1f0e5 KB |
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 | ||
0a0e6a5b | 81 | // return true if the iid is in the array |
82df67d9 | 82 | extern bool IsIidFromList(REFIID riid, const IID *aIids[], size_t nCount); |
bbf1f0e5 KB |
83 | |
84 | // ============================================================================ | |
85 | // IUnknown implementation helpers | |
86 | // ============================================================================ | |
87 | ||
88 | /* | |
3f4a0c5b | 89 | The most dumb implementation of IUnknown methods. We don't support |
bbf1f0e5 KB |
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 | |
82df67d9 | 94 | 1) DECLARE_IUNKNOWN_METHODS in your (IUnknown derived!) class declaration |
bbf1f0e5 KB |
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 | |
82df67d9 | 98 | 3) IMPLEMENT_IUNKNOWN_METHODS somewhere also |
bbf1f0e5 KB |
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 | ||
82df67d9 VZ |
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; } | |
81833075 | 114 | ULONG& operator=(ULONG value) { m_Value = value; return m_Value; } |
0a0e6a5b | 115 | |
018f2884 VZ |
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; } | |
82df67d9 VZ |
121 | |
122 | private: | |
123 | ULONG m_Value; | |
124 | }; | |
125 | ||
bbf1f0e5 KB |
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) | |
82df67d9 | 129 | |
bbf1f0e5 KB |
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[]; \ | |
82df67d9 | 137 | wxAutoULong m_cRef |
bbf1f0e5 KB |
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 | { \ | |
f6bcfd97 | 150 | wxLogQueryInterface(_T(#classname), riid); \ |
bbf1f0e5 | 151 | \ |
f6bcfd97 | 152 | if ( IsIidFromList(riid, ms_aIids, WXSIZEOF(ms_aIids)) ) { \ |
bbf1f0e5 KB |
153 | *ppv = this; \ |
154 | AddRef(); \ | |
155 | \ | |
156 | return S_OK; \ | |
157 | } \ | |
158 | else { \ | |
159 | *ppv = NULL; \ | |
160 | \ | |
f6bcfd97 | 161 | return (HRESULT) E_NOINTERFACE; \ |
bbf1f0e5 KB |
162 | } \ |
163 | } \ | |
164 | \ | |
165 | STDMETHODIMP_(ULONG) classname::AddRef() \ | |
166 | { \ | |
f6bcfd97 | 167 | wxLogAddRef(_T(#classname), m_cRef); \ |
bbf1f0e5 KB |
168 | \ |
169 | return ++m_cRef; \ | |
170 | } \ | |
171 | \ | |
172 | STDMETHODIMP_(ULONG) classname::Release() \ | |
173 | { \ | |
f6bcfd97 | 174 | wxLogRelease(_T(#classname), m_cRef); \ |
bbf1f0e5 | 175 | \ |
56689e1b | 176 | if ( --m_cRef == wxAutoULong(0) ) { \ |
bbf1f0e5 KB |
177 | delete this; \ |
178 | return 0; \ | |
179 | } \ | |
180 | else \ | |
181 | return m_cRef; \ | |
182 | } | |
183 | ||
184 | // ============================================================================ | |
185 | // Debugging support | |
186 | // ============================================================================ | |
187 | ||
3f4a0c5b VZ |
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. | |
ba14d986 | 191 | #if defined(__WXDEBUG__) && ( ( defined(__VISUALC__) && (__VISUALC__ >= 1000) ) || defined(__MWERKS__) ) |
bbf1f0e5 KB |
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 | |
f6bcfd97 | 197 | void wxLogQueryInterface(const wxChar *szInterface, REFIID riid); |
bbf1f0e5 KB |
198 | |
199 | // these functions print out the new value of reference counter | |
f6bcfd97 BP |
200 | void wxLogAddRef (const wxChar *szInterface, ULONG cRef); |
201 | void wxLogRelease(const wxChar *szInterface, ULONG cRef); | |
bbf1f0e5 | 202 | |
e755eb67 | 203 | #else //!__WXDEBUG__ |
bbf1f0e5 KB |
204 | #define wxLogQueryInterface(szInterface, riid) |
205 | #define wxLogAddRef(szInterface, cRef) | |
206 | #define wxLogRelease(szInterface, cRef) | |
e755eb67 | 207 | #endif //__WXDEBUG__ |
bbf1f0e5 | 208 | |
45a959a3 JS |
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(); | |
0a0e6a5b | 218 | |
45a959a3 | 219 | void Init(const char* sz); |
0a0e6a5b | 220 | |
45a959a3 JS |
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); } | |
0a0e6a5b | 226 | |
45a959a3 JS |
227 | private: |
228 | // @@@ not implemented (but should be) | |
229 | wxBasicString(const wxBasicString&); | |
230 | wxBasicString& operator=(const wxBasicString&); | |
0a0e6a5b | 231 | |
45a959a3 JS |
232 | OLECHAR *m_wzBuf; // actual string |
233 | }; | |
234 | ||
235 | // Convert variants | |
c9f00f2f | 236 | class WXDLLIMPEXP_BASE wxVariant; |
45a959a3 JS |
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 | ||
360ae33f VZ |
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 | |
45a959a3 | 257 | |
3f4a0c5b | 258 | #endif //_WX_OLEUTILS_H |