]>
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
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
22 #if defined(__BORLANDC__)
34 #include "wx/msw/private.h"
46 #include "wx/msw/ole/uuid.h"
49 #include "wx/msw/ole/oleutils.h"
50 #include "wx/msw/ole/safearray.h"
52 #if defined(__VISUALC__) && (__VISUALC__ > 1000)
56 // ============================================================================
58 // ============================================================================
60 // return true if the iid is in the array
61 WXDLLEXPORT
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 // ----------------------------------------------------------------------------
135 // wxVariantDataCurrency
136 // ----------------------------------------------------------------------------
141 bool wxVariantDataCurrency::GetAsAny(wxAny
* any
) const
147 wxVariantData
* wxVariantDataCurrency::VariantDataFactory(const wxAny
& any
)
149 return new wxVariantDataCurrency(wxANY_AS(any
, CURRENCY
));
152 REGISTER_WXANY_CONVERSION(CURRENCY
, wxVariantDataCurrency
)
156 bool wxVariantDataCurrency::Eq(wxVariantData
& data
) const
158 wxASSERT_MSG( (data
.GetType() == wxS("currency")),
159 "wxVariantDataCurrency::Eq: argument mismatch" );
161 wxVariantDataCurrency
& otherData
= (wxVariantDataCurrency
&) data
;
163 return otherData
.m_value
.int64
== m_value
.int64
;
166 #if wxUSE_STD_IOSTREAM
167 bool wxVariantDataCurrency::Write(wxSTD ostream
& str
) const
176 bool wxVariantDataCurrency::Write(wxString
& str
) const
179 if ( SUCCEEDED(VarBstrFromCy(m_value
, LOCALE_USER_DEFAULT
, 0, &bStr
)) )
181 str
= wxConvertStringFromOle(bStr
);
188 // ----------------------------------------------------------------------------
189 // wxVariantDataErrorCode
190 // ----------------------------------------------------------------------------
194 bool wxVariantDataErrorCode::GetAsAny(wxAny
* any
) const
200 wxVariantData
* wxVariantDataErrorCode::VariantDataFactory(const wxAny
& any
)
202 return new wxVariantDataErrorCode(wxANY_AS(any
, SCODE
));
205 REGISTER_WXANY_CONVERSION(SCODE
, wxVariantDataErrorCode
)
209 bool wxVariantDataErrorCode::Eq(wxVariantData
& data
) const
211 wxASSERT_MSG( (data
.GetType() == wxS("errorcode")),
212 "wxVariantDataErrorCode::Eq: argument mismatch" );
214 wxVariantDataErrorCode
& otherData
= (wxVariantDataErrorCode
&) data
;
216 return otherData
.m_value
== m_value
;
219 #if wxUSE_STD_IOSTREAM
220 bool wxVariantDataErrorCode::Write(wxSTD ostream
& str
) const
229 bool wxVariantDataErrorCode::Write(wxString
& str
) const
236 // ----------------------------------------------------------------------------
237 // wxVariantDataSafeArray
238 // ----------------------------------------------------------------------------
242 bool wxVariantDataSafeArray::GetAsAny(wxAny
* any
) const
248 wxVariantData
* wxVariantDataSafeArray::VariantDataFactory(const wxAny
& any
)
250 return new wxVariantDataSafeArray(wxANY_AS(any
, SAFEARRAY
*));
253 REGISTER_WXANY_CONVERSION(SAFEARRAY
*, wxVariantDataSafeArray
)
257 bool wxVariantDataSafeArray::Eq(wxVariantData
& data
) const
259 wxASSERT_MSG( (data
.GetType() == wxS("safearray")),
260 "wxVariantDataSafeArray::Eq: argument mismatch" );
262 wxVariantDataSafeArray
& otherData
= (wxVariantDataSafeArray
&) data
;
264 return otherData
.m_value
== m_value
;
267 #if wxUSE_STD_IOSTREAM
268 bool wxVariantDataSafeArray::Write(wxSTD ostream
& str
) const
277 bool wxVariantDataSafeArray::Write(wxString
& str
) const
279 str
.Printf(wxS("SAFEARRAY: %p"), (void*)m_value
);
283 WXDLLEXPORT
bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
285 VariantInit(&oleVariant
);
286 if (variant
.IsNull())
288 oleVariant
.vt
= VT_NULL
;
292 wxString
type(variant
.GetType());
294 if (type
== wxT("errorcode"))
296 wxVariantDataErrorCode
* const
297 ec
= wxStaticCastVariantData(variant
.GetData(),
298 wxVariantDataErrorCode
);
299 oleVariant
.vt
= VT_ERROR
;
300 oleVariant
.scode
= ec
->GetValue();
302 else if (type
== wxT("currency"))
304 wxVariantDataCurrency
* const
305 c
= wxStaticCastVariantData(variant
.GetData(),
306 wxVariantDataCurrency
);
307 oleVariant
.vt
= VT_CY
;
308 oleVariant
.cyVal
= c
->GetValue();
310 else if (type
== wxT("safearray"))
312 wxVariantDataSafeArray
* const
313 vsa
= wxStaticCastVariantData(variant
.GetData(),
314 wxVariantDataSafeArray
);
315 SAFEARRAY
* psa
= vsa
->GetValue();
319 HRESULT hr
= SafeArrayGetVartype(psa
, &vt
);
322 wxLogApiError(wxS("SafeArrayGetVartype()"), hr
);
323 SafeArrayDestroy(psa
);
326 oleVariant
.vt
= vt
| VT_ARRAY
;
327 oleVariant
.parray
= psa
;
329 else if (type
== wxT("long"))
331 oleVariant
.vt
= VT_I4
;
332 oleVariant
.lVal
= variant
.GetLong() ;
334 // Original VC6 came with SDK too old to contain VARIANT::llVal declaration
335 // and there doesn't seem to be any way to test for it as Microsoft simply
336 // added it to the later version of oaidl.h without changing anything else.
337 // So assume it's not present for VC6, even though it might be if an
338 // updated SDK is used. In this case the user would need to disable this
340 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
341 else if (type
== wxT("longlong"))
343 oleVariant
.vt
= VT_I8
;
344 oleVariant
.llVal
= variant
.GetLongLong().GetValue();
347 else if (type
== wxT("char"))
349 oleVariant
.vt
=VT_I1
; // Signed Char
350 oleVariant
.cVal
=variant
.GetChar();
352 else if (type
== wxT("double"))
354 oleVariant
.vt
= VT_R8
;
355 oleVariant
.dblVal
= variant
.GetDouble();
357 else if (type
== wxT("bool"))
359 oleVariant
.vt
= VT_BOOL
;
360 oleVariant
.boolVal
= variant
.GetBool() ? VARIANT_TRUE
: VARIANT_FALSE
;
362 else if (type
== wxT("string"))
364 wxString
str( variant
.GetString() );
365 oleVariant
.vt
= VT_BSTR
;
366 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
369 else if (type
== wxT("datetime"))
371 wxDateTime
date( variant
.GetDateTime() );
372 oleVariant
.vt
= VT_DATE
;
375 date
.GetAsMSWSysTime(&st
);
377 SystemTimeToVariantTime(&st
, &oleVariant
.date
);
380 else if (type
== wxT("void*"))
382 oleVariant
.vt
= VT_DISPATCH
;
383 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
385 else if (type
== wxT("list"))
387 wxSafeArray
<VT_VARIANT
> safeArray
;
388 if (!safeArray
.CreateFromListVariant(variant
))
391 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
392 oleVariant
.parray
= safeArray
.Detach();
394 else if (type
== wxT("arrstring"))
396 wxSafeArray
<VT_BSTR
> safeArray
;
398 if (!safeArray
.CreateFromArrayString(variant
.GetArrayString()))
401 oleVariant
.vt
= VT_BSTR
| VT_ARRAY
;
402 oleVariant
.parray
= safeArray
.Detach();
406 oleVariant
.vt
= VT_NULL
;
413 #define VT_TYPEMASK 0xfff
417 wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
420 if ( oleVariant
.vt
& VT_ARRAY
)
422 // TODO: We currently return arrays as wxVariant of the list type
423 // containing the flattened form of array but we should allow
424 // getting it as wxVariantDataSafeArray instead. Doing this is
425 // simple, we'd just need to do something like this:
427 // if ( oleVariant.parray && SafeArrayGetDim(oleVariant.parray) > 1 )
429 // variant.SetData(new wxVariantDataSafeArray(oleVariant.parray));
432 // but currently we don't do it for compatibility reasons.
433 switch (oleVariant
.vt
& VT_TYPEMASK
)
436 ok
= wxSafeArray
<VT_I2
>::ConvertToVariant(oleVariant
.parray
, variant
);
439 ok
= wxSafeArray
<VT_I4
>::ConvertToVariant(oleVariant
.parray
, variant
);
442 ok
= wxSafeArray
<VT_R4
>::ConvertToVariant(oleVariant
.parray
, variant
);
445 ok
= wxSafeArray
<VT_R8
>::ConvertToVariant(oleVariant
.parray
, variant
);
448 ok
= wxSafeArray
<VT_VARIANT
>::ConvertToVariant(oleVariant
.parray
, variant
);
452 wxArrayString strings
;
453 if ( wxSafeArray
<VT_BSTR
>::ConvertToArrayString(oleVariant
.parray
, strings
) )
465 wxLogDebug(wxT("unhandled VT_ARRAY type %x in wxConvertOleToVariant"),
466 oleVariant
.vt
& VT_TYPEMASK
);
467 variant
= wxVariant();
470 else if ( oleVariant
.vt
& VT_BYREF
)
472 switch ( oleVariant
.vt
& VT_TYPEMASK
)
476 VARIANTARG
& oleReference
= *((LPVARIANT
)oleVariant
.byref
);
477 if (!wxConvertOleToVariant(oleReference
,variant
))
483 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"),
488 else // simply type (not array or reference)
490 switch ( oleVariant
.vt
& VT_TYPEMASK
)
493 variant
.SetData(new wxVariantDataErrorCode(oleVariant
.scode
));
497 variant
.SetData(new wxVariantDataCurrency(oleVariant
.cyVal
));
502 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
511 VariantTimeToSystemTime(oleVariant
.date
, &st
);
514 date
.SetFromMSWSysTime(st
);
517 #endif // wxUSE_DATETIME
520 // See the comment before the __VISUALC6__ test above.
521 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
523 variant
= wxLongLong(oleVariant
.llVal
);
525 #endif // wxUSE_LONGLONG
528 variant
= (long) oleVariant
.lVal
;
532 variant
= (long) oleVariant
.iVal
;
536 variant
= oleVariant
.boolVal
!= 0;
540 variant
= oleVariant
.fltVal
;
544 variant
= oleVariant
.dblVal
;
548 variant
= (void*) oleVariant
.pdispVal
;
556 break; // Ignore Empty Variant, used only during destruction of objects
559 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"),
560 oleVariant
.vt
,oleVariant
.vt
&VT_TYPEMASK
);
568 #endif // wxUSE_VARIANT
571 // ----------------------------------------------------------------------------
573 // ----------------------------------------------------------------------------
577 #if wxDEBUG_LEVEL && (( defined(__VISUALC__) && (__VISUALC__ > 1000) ))
578 static wxString
GetIidName(REFIID riid
)
580 // an association between symbolic name and numeric value of an IID
583 const wxChar
*szName
;
586 // construct the table containing all known interfaces
587 #define ADD_KNOWN_IID(name) { &IID_I##name, wxT(#name) }
589 static const KNOWN_IID aKnownIids
[] = {
590 ADD_KNOWN_IID(AdviseSink
),
591 ADD_KNOWN_IID(AdviseSink2
),
592 ADD_KNOWN_IID(BindCtx
),
593 ADD_KNOWN_IID(ClassFactory
),
594 #if ( !defined( __VISUALC__) || (__VISUALC__!=1010) )
595 ADD_KNOWN_IID(ContinueCallback
),
596 ADD_KNOWN_IID(EnumOleDocumentViews
),
597 ADD_KNOWN_IID(OleCommandTarget
),
598 ADD_KNOWN_IID(OleDocument
),
599 ADD_KNOWN_IID(OleDocumentSite
),
600 ADD_KNOWN_IID(OleDocumentView
),
601 ADD_KNOWN_IID(Print
),
603 ADD_KNOWN_IID(DataAdviseHolder
),
604 ADD_KNOWN_IID(DataObject
),
605 ADD_KNOWN_IID(Debug
),
606 ADD_KNOWN_IID(DebugStream
),
607 ADD_KNOWN_IID(DfReserved1
),
608 ADD_KNOWN_IID(DfReserved2
),
609 ADD_KNOWN_IID(DfReserved3
),
610 ADD_KNOWN_IID(Dispatch
),
611 ADD_KNOWN_IID(DropSource
),
612 ADD_KNOWN_IID(DropTarget
),
613 ADD_KNOWN_IID(EnumCallback
),
614 ADD_KNOWN_IID(EnumFORMATETC
),
615 ADD_KNOWN_IID(EnumGeneric
),
616 ADD_KNOWN_IID(EnumHolder
),
617 ADD_KNOWN_IID(EnumMoniker
),
618 ADD_KNOWN_IID(EnumOLEVERB
),
619 ADD_KNOWN_IID(EnumSTATDATA
),
620 ADD_KNOWN_IID(EnumSTATSTG
),
621 ADD_KNOWN_IID(EnumString
),
622 ADD_KNOWN_IID(EnumUnknown
),
623 ADD_KNOWN_IID(EnumVARIANT
),
624 ADD_KNOWN_IID(ExternalConnection
),
625 ADD_KNOWN_IID(InternalMoniker
),
626 ADD_KNOWN_IID(LockBytes
),
627 ADD_KNOWN_IID(Malloc
),
628 ADD_KNOWN_IID(Marshal
),
629 ADD_KNOWN_IID(MessageFilter
),
630 ADD_KNOWN_IID(Moniker
),
631 ADD_KNOWN_IID(OleAdviseHolder
),
632 ADD_KNOWN_IID(OleCache
),
633 ADD_KNOWN_IID(OleCache2
),
634 ADD_KNOWN_IID(OleCacheControl
),
635 ADD_KNOWN_IID(OleClientSite
),
636 ADD_KNOWN_IID(OleContainer
),
637 ADD_KNOWN_IID(OleInPlaceActiveObject
),
638 ADD_KNOWN_IID(OleInPlaceFrame
),
639 ADD_KNOWN_IID(OleInPlaceObject
),
640 ADD_KNOWN_IID(OleInPlaceSite
),
641 ADD_KNOWN_IID(OleInPlaceUIWindow
),
642 ADD_KNOWN_IID(OleItemContainer
),
643 ADD_KNOWN_IID(OleLink
),
644 ADD_KNOWN_IID(OleManager
),
645 ADD_KNOWN_IID(OleObject
),
646 ADD_KNOWN_IID(OlePresObj
),
647 ADD_KNOWN_IID(OleWindow
),
648 ADD_KNOWN_IID(PSFactory
),
649 ADD_KNOWN_IID(ParseDisplayName
),
650 ADD_KNOWN_IID(Persist
),
651 ADD_KNOWN_IID(PersistFile
),
652 ADD_KNOWN_IID(PersistStorage
),
653 ADD_KNOWN_IID(PersistStream
),
654 ADD_KNOWN_IID(ProxyManager
),
655 ADD_KNOWN_IID(RootStorage
),
656 ADD_KNOWN_IID(RpcChannel
),
657 ADD_KNOWN_IID(RpcProxy
),
658 ADD_KNOWN_IID(RpcStub
),
659 ADD_KNOWN_IID(RunnableObject
),
660 ADD_KNOWN_IID(RunningObjectTable
),
661 ADD_KNOWN_IID(StdMarshalInfo
),
662 ADD_KNOWN_IID(Storage
),
663 ADD_KNOWN_IID(Stream
),
664 ADD_KNOWN_IID(StubManager
),
665 ADD_KNOWN_IID(Unknown
),
666 ADD_KNOWN_IID(ViewObject
),
667 ADD_KNOWN_IID(ViewObject2
),
670 // don't clobber preprocessor name space
673 // try to find the interface in the table
674 for ( size_t ui
= 0; ui
< WXSIZEOF(aKnownIids
); ui
++ ) {
675 if ( riid
== *aKnownIids
[ui
].pIid
) {
676 return aKnownIids
[ui
].szName
;
681 // unknown IID, just transform to string
683 return wxString((const wxChar
*)uuid
);
685 return wxEmptyString
;
689 WXDLLEXPORT
void wxLogQueryInterface(const wxChar
*szInterface
, REFIID riid
)
691 wxLogTrace(wxTRACE_OleCalls
, wxT("%s::QueryInterface (iid = %s)"),
692 szInterface
, GetIidName(riid
).c_str());
695 WXDLLEXPORT
void wxLogAddRef(const wxChar
*szInterface
, ULONG cRef
)
697 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::AddRef: m_cRef = %d"), szInterface
, cRef
+ 1);
700 WXDLLEXPORT
void wxLogRelease(const wxChar
*szInterface
, ULONG cRef
)
702 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::Release: m_cRef = %d"), szInterface
, cRef
- 1);
705 #endif // wxDEBUG_LEVEL
707 #endif // wxUSE_DATAOBJ
709 #endif // __CYGWIN10__