1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: OLE helper routines, OLE debugging support &c
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_OLEUTILS_H
13 #define _WX_OLEUTILS_H
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "oleutils.h"
23 // ole2.h includes windows.h, so include wrapwin.h first
24 #include "wx/msw/wrapwin.h"
25 // get IUnknown, REFIID &c
30 // ============================================================================
31 // General purpose functions and macros
32 // ============================================================================
34 // ----------------------------------------------------------------------------
35 // initialize/cleanup OLE
36 // ----------------------------------------------------------------------------
38 // call OleInitialize() or CoInitialize[Ex]() depending on the platform
40 // return true if ok, false otherwise
41 inline bool wxOleInitialize()
43 // we need to initialize OLE library
45 if ( FAILED(::CoInitializeEx(NULL
, COINIT_MULTITHREADED
)) )
47 if ( FAILED(::OleInitialize(NULL
)) )
50 wxLogError(_("Cannot initialize OLE"));
58 inline void wxOleUninitialize()
67 // ----------------------------------------------------------------------------
68 // misc helper functions/macros
69 // ----------------------------------------------------------------------------
71 // release the interface pointer (if !NULL)
72 inline void ReleaseInterface(IUnknown
*pIUnk
)
78 // release the interface pointer (if !NULL) and make it NULL
79 #define RELEASE_AND_NULL(p) if ( (p) != NULL ) { p->Release(); p = NULL; };
81 // return true if the iid is in the array
82 extern bool IsIidFromList(REFIID riid
, const IID
*aIids
[], size_t nCount
);
84 // ============================================================================
85 // IUnknown implementation helpers
86 // ============================================================================
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.
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
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.
106 wxAutoULong: this class is used for automatically initalising m_cRef to 0
111 wxAutoULong(ULONG value
= 0) : m_Value(value
) { }
113 operator ULONG
&() { return m_Value
; }
114 ULONG
& operator=(ULONG value
) { m_Value
= value
; return m_Value
; }
116 wxAutoULong
& operator++() { ++m_Value
; return *this; }
117 const wxAutoULong
operator++( int ) { wxAutoULong temp
= *this; ++m_Value
; return temp
; }
119 wxAutoULong
& operator--() { --m_Value
; return *this; }
120 const wxAutoULong
operator--( int ) { wxAutoULong temp
= *this; --m_Value
; return temp
; }
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)
130 #define DECLARE_IUNKNOWN_METHODS \
132 STDMETHODIMP QueryInterface(REFIID, void **); \
133 STDMETHODIMP_(ULONG) AddRef(); \
134 STDMETHODIMP_(ULONG) Release(); \
136 static const IID *ms_aIids[]; \
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 }
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) \
150 wxLogQueryInterface(_T(#classname), riid); \
152 if ( IsIidFromList(riid, ms_aIids, WXSIZEOF(ms_aIids)) ) { \
161 return (HRESULT) E_NOINTERFACE; \
165 STDMETHODIMP_(ULONG) classname::AddRef() \
167 wxLogAddRef(_T(#classname), m_cRef); \
172 STDMETHODIMP_(ULONG) classname::Release() \
174 wxLogRelease(_T(#classname), m_cRef); \
176 if ( --m_cRef == wxAutoULong(0) ) { \
184 // ============================================================================
186 // ============================================================================
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 // ----------------------------------------------------------------------------
196 // tries to translate riid into a symbolic name, if possible
197 void wxLogQueryInterface(const wxChar
*szInterface
, REFIID riid
);
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
);
204 #define wxLogQueryInterface(szInterface, riid)
205 #define wxLogAddRef(szInterface, cRef)
206 #define wxLogRelease(szInterface, cRef)
209 // wrapper around BSTR type (by Vadim Zeitlin)
211 class WXDLLEXPORT wxBasicString
215 wxBasicString(const char *sz
);
216 wxBasicString(const wxString
& str
);
219 void Init(const char* sz
);
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
); }
228 // @@@ not implemented (but should be)
229 wxBasicString(const wxBasicString
&);
230 wxBasicString
& operator=(const wxBasicString
&);
232 OLECHAR
*m_wzBuf
; // actual string
236 class WXDLLIMPEXP_BASE wxVariant
;
238 bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
) ;
239 bool wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
) ;
241 // Convert string to Unicode
242 BSTR
wxConvertStringToOle(const wxString
& str
);
244 // Convert string from BSTR to wxString
245 wxString
wxConvertStringFromOle(BSTR bStr
);
249 // ----------------------------------------------------------------------------
250 // stub functions to avoid #if wxUSE_OLE in the main code
251 // ----------------------------------------------------------------------------
253 inline bool wxOleInitialize() { return false; }
254 inline void wxOleUninitialize() { }
256 #endif // wxUSE_OLE/!wxUSE_OLE
258 #endif //_WX_OLEUTILS_H