]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/oleutils.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ole/oleutils.cpp
3 // Purpose: implementation of OLE helper functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 #if defined(__BORLANDC__)
35 #include "wx/msw/private.h"
47 #include "wx/msw/ole/uuid.h"
50 #include "wx/msw/ole/oleutils.h"
52 #if defined(__VISUALC__) && (__VISUALC__ > 1000)
56 // ============================================================================
58 // ============================================================================
60 // return true if the iid is in the array
61 bool IsIidFromList(REFIID riid
, const IID
*aIids
[], size_t nCount
)
63 for ( size_t i
= 0; i
< nCount
; i
++ ) {
64 if ( riid
== *aIids
[i
] )
71 WXDLLEXPORT BSTR
wxConvertStringToOle(const wxString
& str
)
73 return wxBasicString(str
).Get();
76 WXDLLEXPORT wxString
wxConvertStringFromOle(BSTR bStr
)
78 // NULL BSTR is equivalent to an empty string (this is the convention used
79 // by VB and hence we must follow it)
83 const int len
= SysStringLen(bStr
);
86 wxString
str(bStr
, len
);
91 wxStringBufferLength
buf(str
, len
); // asserts if len == 0
92 buf
.SetLength(WideCharToMultiByte(CP_ACP
, 0 /* no flags */,
93 bStr
, len
/* not necessarily NUL-terminated */,
95 NULL
, NULL
/* no default char */));
102 // ----------------------------------------------------------------------------
104 // ----------------------------------------------------------------------------
106 wxBasicString::wxBasicString(const wxString
& str
)
108 m_bstrBuf
= SysAllocString(str
.wc_str(*wxConvCurrent
));
111 wxBasicString::wxBasicString(const wxBasicString
& src
)
113 m_bstrBuf
= src
.Get();
116 wxBasicString
& wxBasicString::operator=(const wxBasicString
& src
)
118 SysReAllocString(&m_bstrBuf
, src
);
122 wxBasicString::~wxBasicString()
124 SysFreeString(m_bstrBuf
);
128 // ----------------------------------------------------------------------------
130 // ----------------------------------------------------------------------------
134 WXDLLEXPORT
bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
136 VariantInit(&oleVariant
);
137 if (variant
.IsNull())
139 oleVariant
.vt
= VT_NULL
;
143 wxString
type(variant
.GetType());
146 if (type
== wxT("long"))
148 oleVariant
.vt
= VT_I4
;
149 oleVariant
.lVal
= variant
.GetLong() ;
151 else if (type
== wxT("char"))
153 oleVariant
.vt
=VT_I1
; // Signed Char
154 oleVariant
.cVal
=variant
.GetChar();
156 else if (type
== wxT("double"))
158 oleVariant
.vt
= VT_R8
;
159 oleVariant
.dblVal
= variant
.GetDouble();
161 else if (type
== wxT("bool"))
163 oleVariant
.vt
= VT_BOOL
;
164 oleVariant
.boolVal
= variant
.GetBool() ? VARIANT_TRUE
: VARIANT_FALSE
;
166 else if (type
== wxT("string"))
168 wxString
str( variant
.GetString() );
169 oleVariant
.vt
= VT_BSTR
;
170 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
173 else if (type
== wxT("datetime"))
175 wxDateTime
date( variant
.GetDateTime() );
176 oleVariant
.vt
= VT_DATE
;
179 date
.GetAsMSWSysTime(&st
);
181 SystemTimeToVariantTime(&st
, &oleVariant
.date
);
184 else if (type
== wxT("void*"))
186 oleVariant
.vt
= VT_DISPATCH
;
187 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
189 else if (type
== wxT("list") || type
== wxT("stringlist"))
191 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
194 SAFEARRAYBOUND saBound
;
195 VARIANTARG
*pvargBase
;
199 int iCount
= variant
.GetCount();
202 saBound
.cElements
= iCount
;
204 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
208 SafeArrayAccessData(psa
, (void**)&pvargBase
);
211 for (i
= 0; i
< iCount
; i
++)
213 // copy each string in the list of strings
214 wxVariant
eachVariant(variant
[i
]);
215 if (!wxConvertVariantToOle(eachVariant
, * pvarg
))
217 // memory failure: back out and free strings alloc'ed up to
218 // now, and then the array itself.
220 for (j
= 0; j
< i
; j
++)
222 SysFreeString(pvarg
->bstrVal
);
225 SafeArrayDestroy(psa
);
231 SafeArrayUnaccessData(psa
);
233 oleVariant
.parray
= psa
;
237 oleVariant
.vt
= VT_NULL
;
244 #define VT_TYPEMASK 0xfff
248 wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
251 if ( oleVariant
.vt
& VT_ARRAY
)
254 // Compute the total number of elements in all array dimensions
256 for ( int cDims
= 0; cDims
< oleVariant
.parray
->cDims
; cDims
++ )
257 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
259 // Get a pointer to the data
261 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, &pvdata
);
265 switch (oleVariant
.vt
& VT_TYPEMASK
)
270 VARIANTARG
*variant_data
=(VARIANTARG
*)pvdata
;
271 for ( int i
= 0; i
< cElements
; i
++ )
273 VARIANTARG
& oleElement
= variant_data
[i
];
275 if ( !wxConvertOleToVariant(oleElement
, vElement
) )
282 variant
.Append(vElement
);
289 wxArrayString strings
;
290 BSTR
*string_val
=(BSTR
*)pvdata
;
291 for ( int i
= 0; i
< cElements
; ++i
)
293 wxString str
=wxConvertStringFromOle(*string_val
);
302 wxLogDebug(wxT("unhandled VT_ARRAY type %x in wxConvertOleToVariant"),
303 oleVariant
.vt
& VT_TYPEMASK
);
304 variant
= wxVariant();
309 SafeArrayUnaccessData(oleVariant
.parray
);
311 else if ( oleVariant
.vt
& VT_BYREF
)
313 switch ( oleVariant
.vt
& VT_TYPEMASK
)
317 VARIANTARG
& oleReference
= *((LPVARIANT
)oleVariant
.byref
);
318 if (!wxConvertOleToVariant(oleReference
,variant
))
324 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"),
329 else // simply type (not array or reference)
331 switch ( oleVariant
.vt
& VT_TYPEMASK
)
335 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
344 VariantTimeToSystemTime(oleVariant
.date
, &st
);
347 date
.SetFromMSWSysTime(st
);
350 #endif // wxUSE_DATETIME
354 variant
= (long) oleVariant
.lVal
;
358 variant
= (long) oleVariant
.iVal
;
362 variant
= oleVariant
.boolVal
!= 0;
366 variant
= oleVariant
.dblVal
;
370 variant
= (void*) oleVariant
.pdispVal
;
378 break; // Ignore Empty Variant, used only during destruction of objects
381 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"),
382 oleVariant
.vt
,oleVariant
.vt
&VT_TYPEMASK
);
390 #endif // wxUSE_VARIANT
393 // ----------------------------------------------------------------------------
395 // ----------------------------------------------------------------------------
399 #if wxDEBUG_LEVEL && ( ( defined(__VISUALC__) && (__VISUALC__ > 1000) ) || defined(__MWERKS__) )
400 static wxString
GetIidName(REFIID riid
)
402 // an association between symbolic name and numeric value of an IID
405 const wxChar
*szName
;
408 // construct the table containing all known interfaces
409 #define ADD_KNOWN_IID(name) { &IID_I##name, wxT(#name) }
411 static const KNOWN_IID aKnownIids
[] = {
412 ADD_KNOWN_IID(AdviseSink
),
413 ADD_KNOWN_IID(AdviseSink2
),
414 ADD_KNOWN_IID(BindCtx
),
415 ADD_KNOWN_IID(ClassFactory
),
416 #if ( !defined( __VISUALC__) || (__VISUALC__!=1010) ) && !defined(__MWERKS__)
417 ADD_KNOWN_IID(ContinueCallback
),
418 ADD_KNOWN_IID(EnumOleDocumentViews
),
419 ADD_KNOWN_IID(OleCommandTarget
),
420 ADD_KNOWN_IID(OleDocument
),
421 ADD_KNOWN_IID(OleDocumentSite
),
422 ADD_KNOWN_IID(OleDocumentView
),
423 ADD_KNOWN_IID(Print
),
425 ADD_KNOWN_IID(DataAdviseHolder
),
426 ADD_KNOWN_IID(DataObject
),
427 ADD_KNOWN_IID(Debug
),
428 ADD_KNOWN_IID(DebugStream
),
429 ADD_KNOWN_IID(DfReserved1
),
430 ADD_KNOWN_IID(DfReserved2
),
431 ADD_KNOWN_IID(DfReserved3
),
432 ADD_KNOWN_IID(Dispatch
),
433 ADD_KNOWN_IID(DropSource
),
434 ADD_KNOWN_IID(DropTarget
),
435 ADD_KNOWN_IID(EnumCallback
),
436 ADD_KNOWN_IID(EnumFORMATETC
),
437 ADD_KNOWN_IID(EnumGeneric
),
438 ADD_KNOWN_IID(EnumHolder
),
439 ADD_KNOWN_IID(EnumMoniker
),
440 ADD_KNOWN_IID(EnumOLEVERB
),
441 ADD_KNOWN_IID(EnumSTATDATA
),
442 ADD_KNOWN_IID(EnumSTATSTG
),
443 ADD_KNOWN_IID(EnumString
),
444 ADD_KNOWN_IID(EnumUnknown
),
445 ADD_KNOWN_IID(EnumVARIANT
),
446 ADD_KNOWN_IID(ExternalConnection
),
447 ADD_KNOWN_IID(InternalMoniker
),
448 ADD_KNOWN_IID(LockBytes
),
449 ADD_KNOWN_IID(Malloc
),
450 ADD_KNOWN_IID(Marshal
),
451 ADD_KNOWN_IID(MessageFilter
),
452 ADD_KNOWN_IID(Moniker
),
453 ADD_KNOWN_IID(OleAdviseHolder
),
454 ADD_KNOWN_IID(OleCache
),
455 ADD_KNOWN_IID(OleCache2
),
456 ADD_KNOWN_IID(OleCacheControl
),
457 ADD_KNOWN_IID(OleClientSite
),
458 ADD_KNOWN_IID(OleContainer
),
459 ADD_KNOWN_IID(OleInPlaceActiveObject
),
460 ADD_KNOWN_IID(OleInPlaceFrame
),
461 ADD_KNOWN_IID(OleInPlaceObject
),
462 ADD_KNOWN_IID(OleInPlaceSite
),
463 ADD_KNOWN_IID(OleInPlaceUIWindow
),
464 ADD_KNOWN_IID(OleItemContainer
),
465 ADD_KNOWN_IID(OleLink
),
466 ADD_KNOWN_IID(OleManager
),
467 ADD_KNOWN_IID(OleObject
),
468 ADD_KNOWN_IID(OlePresObj
),
469 ADD_KNOWN_IID(OleWindow
),
470 ADD_KNOWN_IID(PSFactory
),
471 ADD_KNOWN_IID(ParseDisplayName
),
472 ADD_KNOWN_IID(Persist
),
473 ADD_KNOWN_IID(PersistFile
),
474 ADD_KNOWN_IID(PersistStorage
),
475 ADD_KNOWN_IID(PersistStream
),
476 ADD_KNOWN_IID(ProxyManager
),
477 ADD_KNOWN_IID(RootStorage
),
478 ADD_KNOWN_IID(RpcChannel
),
479 ADD_KNOWN_IID(RpcProxy
),
480 ADD_KNOWN_IID(RpcStub
),
481 ADD_KNOWN_IID(RunnableObject
),
482 ADD_KNOWN_IID(RunningObjectTable
),
483 ADD_KNOWN_IID(StdMarshalInfo
),
484 ADD_KNOWN_IID(Storage
),
485 ADD_KNOWN_IID(Stream
),
486 ADD_KNOWN_IID(StubManager
),
487 ADD_KNOWN_IID(Unknown
),
488 ADD_KNOWN_IID(ViewObject
),
489 ADD_KNOWN_IID(ViewObject2
),
492 // don't clobber preprocessor name space
495 // try to find the interface in the table
496 for ( size_t ui
= 0; ui
< WXSIZEOF(aKnownIids
); ui
++ ) {
497 if ( riid
== *aKnownIids
[ui
].pIid
) {
498 return aKnownIids
[ui
].szName
;
503 // unknown IID, just transform to string
505 return wxString((const wxChar
*)uuid
);
507 return wxEmptyString
;
511 void wxLogQueryInterface(const wxChar
*szInterface
, REFIID riid
)
513 wxLogTrace(wxTRACE_OleCalls
, wxT("%s::QueryInterface (iid = %s)"),
514 szInterface
, GetIidName(riid
).c_str());
517 void wxLogAddRef(const wxChar
*szInterface
, ULONG cRef
)
519 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::AddRef: m_cRef = %d"), szInterface
, cRef
+ 1);
522 void wxLogRelease(const wxChar
*szInterface
, ULONG cRef
)
524 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::Release: m_cRef = %d"), szInterface
, cRef
- 1);
527 #endif // wxDEBUG_LEVEL
529 #endif // wxUSE_DATAOBJ
531 #endif // __CYGWIN10__