1998-05-20 14:21:00 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: oleutils.h
|
|
|
|
// Purpose: OLE helper routines, OLE debugging support &c
|
|
|
|
// Author: Vadim Zeitlin
|
1999-02-03 16:48:12 +00:00
|
|
|
// Modified by:
|
1998-05-20 14:21:00 +00:00
|
|
|
// Created: 19.02.1998
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
2004-05-23 20:53:33 +00:00
|
|
|
// Licence: wxWindows licence
|
1998-05-20 14:21:00 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
1998-08-07 23:52:45 +00:00
|
|
|
#ifndef _WX_OLEUTILS_H
|
|
|
|
#define _WX_OLEUTILS_H
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2003-08-09 12:38:21 +00:00
|
|
|
#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
|
1998-05-20 14:21:00 +00:00
|
|
|
#pragma interface "oleutils.h"
|
|
|
|
#endif
|
|
|
|
|
1998-07-08 10:05:51 +00:00
|
|
|
#include "wx/defs.h"
|
2002-07-09 11:52:11 +00:00
|
|
|
|
2003-01-22 17:19:23 +00:00
|
|
|
// get IUnknown, REFIID &c
|
|
|
|
#include <ole2.h>
|
2002-07-09 11:52:11 +00:00
|
|
|
|
1998-05-20 14:21:00 +00:00
|
|
|
// ============================================================================
|
|
|
|
// General purpose functions and macros
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// misc helper functions/macros
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// release the interface pointer (if !NULL)
|
|
|
|
inline void ReleaseInterface(IUnknown *pIUnk)
|
|
|
|
{
|
|
|
|
if ( pIUnk != NULL )
|
|
|
|
pIUnk->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
// release the interface pointer (if !NULL) and make it NULL
|
|
|
|
#define RELEASE_AND_NULL(p) if ( (p) != NULL ) { p->Release(); p = NULL; };
|
|
|
|
|
2004-08-16 12:45:46 +00:00
|
|
|
// return true if the iid is in the array
|
2002-07-09 11:52:11 +00:00
|
|
|
extern bool IsIidFromList(REFIID riid, const IID *aIids[], size_t nCount);
|
1998-05-20 14:21:00 +00:00
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// IUnknown implementation helpers
|
|
|
|
// ============================================================================
|
|
|
|
|
|
|
|
/*
|
1999-02-03 16:48:12 +00:00
|
|
|
The most dumb implementation of IUnknown methods. We don't support
|
1998-05-20 14:21:00 +00:00
|
|
|
aggregation nor containment, but for 99% of cases this simple
|
|
|
|
implementation is quite enough.
|
|
|
|
|
|
|
|
Usage is trivial: here is all you should have
|
2002-07-09 11:52:11 +00:00
|
|
|
1) DECLARE_IUNKNOWN_METHODS in your (IUnknown derived!) class declaration
|
1998-05-20 14:21:00 +00:00
|
|
|
2) BEGIN/END_IID_TABLE with ADD_IID in between for all interfaces you
|
|
|
|
support (at least all for which you intent to return 'this' from QI,
|
|
|
|
i.e. you should derive from IFoo if you have ADD_IID(Foo)) somewhere else
|
2002-07-09 11:52:11 +00:00
|
|
|
3) IMPLEMENT_IUNKNOWN_METHODS somewhere also
|
1998-05-20 14:21:00 +00:00
|
|
|
|
|
|
|
These macros are quite simple: AddRef and Release are trivial and QI does
|
|
|
|
lookup in a static member array of IIDs and returns 'this' if it founds
|
|
|
|
the requested interface in it or E_NOINTERFACE if not.
|
|
|
|
*/
|
|
|
|
|
2002-07-09 11:52:11 +00:00
|
|
|
/*
|
|
|
|
wxAutoULong: this class is used for automatically initalising m_cRef to 0
|
|
|
|
*/
|
|
|
|
class wxAutoULong
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
wxAutoULong(ULONG value = 0) : m_Value(value) { }
|
|
|
|
|
|
|
|
operator ULONG&() { return m_Value; }
|
2004-03-02 09:28:32 +00:00
|
|
|
ULONG& operator=(ULONG value) { m_Value = value; return m_Value; }
|
2004-08-16 12:45:46 +00:00
|
|
|
|
2002-07-23 21:59:06 +00:00
|
|
|
wxAutoULong& operator++() { ++m_Value; return *this; }
|
|
|
|
const wxAutoULong operator++( int ) { wxAutoULong temp = *this; ++m_Value; return temp; }
|
|
|
|
|
|
|
|
wxAutoULong& operator--() { --m_Value; return *this; }
|
|
|
|
const wxAutoULong operator--( int ) { wxAutoULong temp = *this; --m_Value; return temp; }
|
2002-07-09 11:52:11 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
ULONG m_Value;
|
|
|
|
};
|
|
|
|
|
1998-05-20 14:21:00 +00:00
|
|
|
// declare the methods and the member variable containing reference count
|
|
|
|
// you must also define the ms_aIids array somewhere with BEGIN_IID_TABLE
|
|
|
|
// and friends (see below)
|
2002-07-09 11:52:11 +00:00
|
|
|
|
1998-05-20 14:21:00 +00:00
|
|
|
#define DECLARE_IUNKNOWN_METHODS \
|
|
|
|
public: \
|
|
|
|
STDMETHODIMP QueryInterface(REFIID, void **); \
|
|
|
|
STDMETHODIMP_(ULONG) AddRef(); \
|
|
|
|
STDMETHODIMP_(ULONG) Release(); \
|
|
|
|
private: \
|
|
|
|
static const IID *ms_aIids[]; \
|
2002-07-09 11:52:11 +00:00
|
|
|
wxAutoULong m_cRef
|
1998-05-20 14:21:00 +00:00
|
|
|
|
|
|
|
// macros for declaring supported interfaces
|
|
|
|
// NB: you should write ADD_INTERFACE(Foo) and not ADD_INTERFACE(IID_IFoo)!
|
|
|
|
#define BEGIN_IID_TABLE(cname) const IID *cname::ms_aIids[] = {
|
|
|
|
#define ADD_IID(iid) &IID_I##iid,
|
|
|
|
#define END_IID_TABLE }
|
|
|
|
|
|
|
|
// implementation is as straightforward as possible
|
|
|
|
// Parameter: classname - the name of the class
|
|
|
|
#define IMPLEMENT_IUNKNOWN_METHODS(classname) \
|
|
|
|
STDMETHODIMP classname::QueryInterface(REFIID riid, void **ppv) \
|
|
|
|
{ \
|
2000-07-15 19:51:35 +00:00
|
|
|
wxLogQueryInterface(_T(#classname), riid); \
|
1998-05-20 14:21:00 +00:00
|
|
|
\
|
2000-07-15 19:51:35 +00:00
|
|
|
if ( IsIidFromList(riid, ms_aIids, WXSIZEOF(ms_aIids)) ) { \
|
1998-05-20 14:21:00 +00:00
|
|
|
*ppv = this; \
|
|
|
|
AddRef(); \
|
|
|
|
\
|
|
|
|
return S_OK; \
|
|
|
|
} \
|
|
|
|
else { \
|
|
|
|
*ppv = NULL; \
|
|
|
|
\
|
2000-07-15 19:51:35 +00:00
|
|
|
return (HRESULT) E_NOINTERFACE; \
|
1998-05-20 14:21:00 +00:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
STDMETHODIMP_(ULONG) classname::AddRef() \
|
|
|
|
{ \
|
2000-07-15 19:51:35 +00:00
|
|
|
wxLogAddRef(_T(#classname), m_cRef); \
|
1998-05-20 14:21:00 +00:00
|
|
|
\
|
|
|
|
return ++m_cRef; \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
STDMETHODIMP_(ULONG) classname::Release() \
|
|
|
|
{ \
|
2000-07-15 19:51:35 +00:00
|
|
|
wxLogRelease(_T(#classname), m_cRef); \
|
1998-05-20 14:21:00 +00:00
|
|
|
\
|
2003-04-24 11:15:36 +00:00
|
|
|
if ( --m_cRef == wxAutoULong(0) ) { \
|
1998-05-20 14:21:00 +00:00
|
|
|
delete this; \
|
|
|
|
return 0; \
|
|
|
|
} \
|
|
|
|
else \
|
|
|
|
return m_cRef; \
|
|
|
|
}
|
|
|
|
|
|
|
|
// ============================================================================
|
|
|
|
// Debugging support
|
|
|
|
// ============================================================================
|
|
|
|
|
1999-02-03 16:48:12 +00:00
|
|
|
// VZ: I don't know it's not done for compilers other than VC++ but I leave it
|
|
|
|
// as is. Please note, though, that tracing OLE interface calls may be
|
|
|
|
// incredibly useful when debugging OLE programs.
|
2002-05-09 22:34:44 +00:00
|
|
|
#if defined(__WXDEBUG__) && ( ( defined(__VISUALC__) && (__VISUALC__ >= 1000) ) || defined(__MWERKS__) )
|
1998-05-20 14:21:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// All OLE specific log functions have DebugTrace level (as LogTrace)
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// tries to translate riid into a symbolic name, if possible
|
2000-07-15 19:51:35 +00:00
|
|
|
void wxLogQueryInterface(const wxChar *szInterface, REFIID riid);
|
1998-05-20 14:21:00 +00:00
|
|
|
|
|
|
|
// these functions print out the new value of reference counter
|
2000-07-15 19:51:35 +00:00
|
|
|
void wxLogAddRef (const wxChar *szInterface, ULONG cRef);
|
|
|
|
void wxLogRelease(const wxChar *szInterface, ULONG cRef);
|
1998-05-20 14:21:00 +00:00
|
|
|
|
1998-07-10 11:14:17 +00:00
|
|
|
#else //!WXDEBUG
|
1998-05-20 14:21:00 +00:00
|
|
|
#define wxLogQueryInterface(szInterface, riid)
|
|
|
|
#define wxLogAddRef(szInterface, cRef)
|
|
|
|
#define wxLogRelease(szInterface, cRef)
|
1998-07-10 11:14:17 +00:00
|
|
|
#endif //WXDEBUG
|
1998-05-20 14:21:00 +00:00
|
|
|
|
2003-02-16 11:57:24 +00:00
|
|
|
// wrapper around BSTR type (by Vadim Zeitlin)
|
|
|
|
|
|
|
|
class WXDLLEXPORT wxBasicString
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
// ctors & dtor
|
|
|
|
wxBasicString(const char *sz);
|
|
|
|
wxBasicString(const wxString& str);
|
|
|
|
~wxBasicString();
|
2004-08-16 12:45:46 +00:00
|
|
|
|
2003-02-16 11:57:24 +00:00
|
|
|
void Init(const char* sz);
|
2004-08-16 12:45:46 +00:00
|
|
|
|
2003-02-16 11:57:24 +00:00
|
|
|
// accessors
|
|
|
|
// just get the string
|
|
|
|
operator BSTR() const { return m_wzBuf; }
|
|
|
|
// retrieve a copy of our string - caller must SysFreeString() it later!
|
|
|
|
BSTR Get() const { return SysAllocString(m_wzBuf); }
|
2004-08-16 12:45:46 +00:00
|
|
|
|
2003-02-16 11:57:24 +00:00
|
|
|
private:
|
|
|
|
// @@@ not implemented (but should be)
|
|
|
|
wxBasicString(const wxBasicString&);
|
|
|
|
wxBasicString& operator=(const wxBasicString&);
|
2004-08-16 12:45:46 +00:00
|
|
|
|
2003-02-16 11:57:24 +00:00
|
|
|
OLECHAR *m_wzBuf; // actual string
|
|
|
|
};
|
|
|
|
|
|
|
|
// Convert variants
|
|
|
|
class WXDLLEXPORT wxVariant;
|
|
|
|
|
|
|
|
bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant) ;
|
|
|
|
bool wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant) ;
|
|
|
|
|
|
|
|
// Convert string to Unicode
|
|
|
|
BSTR wxConvertStringToOle(const wxString& str);
|
|
|
|
|
|
|
|
// Convert string from BSTR to wxString
|
|
|
|
wxString wxConvertStringFromOle(BSTR bStr);
|
|
|
|
|
|
|
|
|
1999-02-03 16:48:12 +00:00
|
|
|
#endif //_WX_OLEUTILS_H
|
|
|
|
|