]>
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 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 // ----------------------------------------------------------------------------
137 // Helper class for creating and filling SAFEARRAY. To use it, call Create()
138 // first, then SetElement() for each element and finally Detach() the SAFEARRAY
139 // from it if you don't want it to be deleted when this class is.
140 class wxSafeArrayHelper
144 ~wxSafeArrayHelper();
146 bool Create(VARTYPE vt
, long count
); // creates and locks the array
148 bool SetElement(size_t index
, const wxVariant
& variant
);
149 bool SetElement(size_t index
, const wxString
& str
);
151 SAFEARRAY
* Detach(); // unlocks the array and gives up its ownership
159 wxSafeArrayHelper::wxSafeArrayHelper()
164 wxSafeArrayHelper::~wxSafeArrayHelper()
169 SafeArrayDestroy(m_array
);
173 bool wxSafeArrayHelper::Create(VARTYPE vt
, long count
)
175 SAFEARRAYBOUND saBound
;
178 saBound
.cElements
= count
;
179 m_array
= SafeArrayCreate(vt
, 1, &saBound
);
182 return SUCCEEDED( SafeArrayLock(m_array
) );
185 bool wxSafeArrayHelper::SetElement(size_t index
, const wxVariant
& variant
)
187 VARIANT
* data
= (VARIANT
*)m_array
->pvData
;
188 return wxConvertVariantToOle(variant
, data
[index
]);
191 bool wxSafeArrayHelper::SetElement(size_t index
, const wxString
& str
)
193 BSTR bstr
= wxConvertStringToOle(str
);
195 if ( !bstr
&& !str
.empty() )
197 // BSTR can be NULL for empty strings but if the string was
198 // not empty, it means we failed to allocate memory for it.
202 BSTR
* data
= (BSTR
*)m_array
->pvData
;
207 SAFEARRAY
* wxSafeArrayHelper::Detach()
210 SAFEARRAY
* result
= m_array
;
215 void wxSafeArrayHelper::Unlock()
218 SafeArrayUnlock(m_array
);
221 } // unnamed namespace
224 WXDLLEXPORT
bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
226 VariantInit(&oleVariant
);
227 if (variant
.IsNull())
229 oleVariant
.vt
= VT_NULL
;
233 wxString
type(variant
.GetType());
236 if (type
== wxT("long"))
238 oleVariant
.vt
= VT_I4
;
239 oleVariant
.lVal
= variant
.GetLong() ;
241 // Original VC6 came with SDK too old to contain VARIANT::llVal declaration
242 // and there doesn't seem to be any way to test for it as Microsoft simply
243 // added it to the later version of oaidl.h without changing anything else.
244 // So assume it's not present for VC6, even though it might be if an
245 // updated SDK is used. In this case the user would need to disable this
247 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
248 else if (type
== wxT("longlong"))
250 oleVariant
.vt
= VT_I8
;
251 oleVariant
.llVal
= variant
.GetLongLong().GetValue();
254 else if (type
== wxT("char"))
256 oleVariant
.vt
=VT_I1
; // Signed Char
257 oleVariant
.cVal
=variant
.GetChar();
259 else if (type
== wxT("double"))
261 oleVariant
.vt
= VT_R8
;
262 oleVariant
.dblVal
= variant
.GetDouble();
264 else if (type
== wxT("bool"))
266 oleVariant
.vt
= VT_BOOL
;
267 oleVariant
.boolVal
= variant
.GetBool() ? VARIANT_TRUE
: VARIANT_FALSE
;
269 else if (type
== wxT("string"))
271 wxString
str( variant
.GetString() );
272 oleVariant
.vt
= VT_BSTR
;
273 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
276 else if (type
== wxT("datetime"))
278 wxDateTime
date( variant
.GetDateTime() );
279 oleVariant
.vt
= VT_DATE
;
282 date
.GetAsMSWSysTime(&st
);
284 SystemTimeToVariantTime(&st
, &oleVariant
.date
);
287 else if (type
== wxT("void*"))
289 oleVariant
.vt
= VT_DISPATCH
;
290 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
292 else if (type
== wxT("list"))
294 wxSafeArrayHelper sah
;
296 if (!sah
.Create(VT_VARIANT
, variant
.GetCount()))
299 for (size_t i
= 0; i
< variant
.GetCount(); i
++)
301 if (!sah
.SetElement(i
, variant
[i
]))
305 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
306 oleVariant
.parray
= sah
.Detach();
308 else if (type
== wxT("arrstring"))
310 wxArrayString
strings(variant
.GetArrayString());
311 wxSafeArrayHelper sah
;
313 if (!sah
.Create(VT_BSTR
, strings
.GetCount()))
316 for (size_t i
= 0; i
< strings
.GetCount(); i
++)
318 if (!sah
.SetElement(i
, strings
[i
]))
322 oleVariant
.vt
= VT_BSTR
| VT_ARRAY
;
323 oleVariant
.parray
= sah
.Detach();
327 oleVariant
.vt
= VT_NULL
;
334 #define VT_TYPEMASK 0xfff
338 wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
341 if ( oleVariant
.vt
& VT_ARRAY
)
344 // Compute the total number of elements in all array dimensions
346 for ( int cDims
= 0; cDims
< oleVariant
.parray
->cDims
; cDims
++ )
347 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
349 // Get a pointer to the data
351 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, &pvdata
);
355 switch (oleVariant
.vt
& VT_TYPEMASK
)
360 VARIANTARG
*variant_data
=(VARIANTARG
*)pvdata
;
361 for ( int i
= 0; i
< cElements
; i
++ )
363 VARIANTARG
& oleElement
= variant_data
[i
];
365 if ( !wxConvertOleToVariant(oleElement
, vElement
) )
372 variant
.Append(vElement
);
379 wxArrayString strings
;
380 BSTR
*string_val
=(BSTR
*)pvdata
;
381 for ( int i
= 0; i
< cElements
; ++i
)
383 wxString str
=wxConvertStringFromOle(*string_val
);
392 wxLogDebug(wxT("unhandled VT_ARRAY type %x in wxConvertOleToVariant"),
393 oleVariant
.vt
& VT_TYPEMASK
);
394 variant
= wxVariant();
399 SafeArrayUnaccessData(oleVariant
.parray
);
401 else if ( oleVariant
.vt
& VT_BYREF
)
403 switch ( oleVariant
.vt
& VT_TYPEMASK
)
407 VARIANTARG
& oleReference
= *((LPVARIANT
)oleVariant
.byref
);
408 if (!wxConvertOleToVariant(oleReference
,variant
))
414 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"),
419 else // simply type (not array or reference)
421 switch ( oleVariant
.vt
& VT_TYPEMASK
)
425 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
434 VariantTimeToSystemTime(oleVariant
.date
, &st
);
437 date
.SetFromMSWSysTime(st
);
440 #endif // wxUSE_DATETIME
443 // See the comment before the __VISUALC6__ test above.
444 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
446 variant
= wxLongLong(oleVariant
.llVal
);
448 #endif // wxUSE_LONGLONG
451 variant
= (long) oleVariant
.lVal
;
455 variant
= (long) oleVariant
.iVal
;
459 variant
= oleVariant
.boolVal
!= 0;
463 variant
= oleVariant
.fltVal
;
467 variant
= oleVariant
.dblVal
;
471 variant
= (void*) oleVariant
.pdispVal
;
479 break; // Ignore Empty Variant, used only during destruction of objects
482 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"),
483 oleVariant
.vt
,oleVariant
.vt
&VT_TYPEMASK
);
491 #endif // wxUSE_VARIANT
494 // ----------------------------------------------------------------------------
496 // ----------------------------------------------------------------------------
500 #if wxDEBUG_LEVEL && (( defined(__VISUALC__) && (__VISUALC__ > 1000) ))
501 static wxString
GetIidName(REFIID riid
)
503 // an association between symbolic name and numeric value of an IID
506 const wxChar
*szName
;
509 // construct the table containing all known interfaces
510 #define ADD_KNOWN_IID(name) { &IID_I##name, wxT(#name) }
512 static const KNOWN_IID aKnownIids
[] = {
513 ADD_KNOWN_IID(AdviseSink
),
514 ADD_KNOWN_IID(AdviseSink2
),
515 ADD_KNOWN_IID(BindCtx
),
516 ADD_KNOWN_IID(ClassFactory
),
517 #if ( !defined( __VISUALC__) || (__VISUALC__!=1010) )
518 ADD_KNOWN_IID(ContinueCallback
),
519 ADD_KNOWN_IID(EnumOleDocumentViews
),
520 ADD_KNOWN_IID(OleCommandTarget
),
521 ADD_KNOWN_IID(OleDocument
),
522 ADD_KNOWN_IID(OleDocumentSite
),
523 ADD_KNOWN_IID(OleDocumentView
),
524 ADD_KNOWN_IID(Print
),
526 ADD_KNOWN_IID(DataAdviseHolder
),
527 ADD_KNOWN_IID(DataObject
),
528 ADD_KNOWN_IID(Debug
),
529 ADD_KNOWN_IID(DebugStream
),
530 ADD_KNOWN_IID(DfReserved1
),
531 ADD_KNOWN_IID(DfReserved2
),
532 ADD_KNOWN_IID(DfReserved3
),
533 ADD_KNOWN_IID(Dispatch
),
534 ADD_KNOWN_IID(DropSource
),
535 ADD_KNOWN_IID(DropTarget
),
536 ADD_KNOWN_IID(EnumCallback
),
537 ADD_KNOWN_IID(EnumFORMATETC
),
538 ADD_KNOWN_IID(EnumGeneric
),
539 ADD_KNOWN_IID(EnumHolder
),
540 ADD_KNOWN_IID(EnumMoniker
),
541 ADD_KNOWN_IID(EnumOLEVERB
),
542 ADD_KNOWN_IID(EnumSTATDATA
),
543 ADD_KNOWN_IID(EnumSTATSTG
),
544 ADD_KNOWN_IID(EnumString
),
545 ADD_KNOWN_IID(EnumUnknown
),
546 ADD_KNOWN_IID(EnumVARIANT
),
547 ADD_KNOWN_IID(ExternalConnection
),
548 ADD_KNOWN_IID(InternalMoniker
),
549 ADD_KNOWN_IID(LockBytes
),
550 ADD_KNOWN_IID(Malloc
),
551 ADD_KNOWN_IID(Marshal
),
552 ADD_KNOWN_IID(MessageFilter
),
553 ADD_KNOWN_IID(Moniker
),
554 ADD_KNOWN_IID(OleAdviseHolder
),
555 ADD_KNOWN_IID(OleCache
),
556 ADD_KNOWN_IID(OleCache2
),
557 ADD_KNOWN_IID(OleCacheControl
),
558 ADD_KNOWN_IID(OleClientSite
),
559 ADD_KNOWN_IID(OleContainer
),
560 ADD_KNOWN_IID(OleInPlaceActiveObject
),
561 ADD_KNOWN_IID(OleInPlaceFrame
),
562 ADD_KNOWN_IID(OleInPlaceObject
),
563 ADD_KNOWN_IID(OleInPlaceSite
),
564 ADD_KNOWN_IID(OleInPlaceUIWindow
),
565 ADD_KNOWN_IID(OleItemContainer
),
566 ADD_KNOWN_IID(OleLink
),
567 ADD_KNOWN_IID(OleManager
),
568 ADD_KNOWN_IID(OleObject
),
569 ADD_KNOWN_IID(OlePresObj
),
570 ADD_KNOWN_IID(OleWindow
),
571 ADD_KNOWN_IID(PSFactory
),
572 ADD_KNOWN_IID(ParseDisplayName
),
573 ADD_KNOWN_IID(Persist
),
574 ADD_KNOWN_IID(PersistFile
),
575 ADD_KNOWN_IID(PersistStorage
),
576 ADD_KNOWN_IID(PersistStream
),
577 ADD_KNOWN_IID(ProxyManager
),
578 ADD_KNOWN_IID(RootStorage
),
579 ADD_KNOWN_IID(RpcChannel
),
580 ADD_KNOWN_IID(RpcProxy
),
581 ADD_KNOWN_IID(RpcStub
),
582 ADD_KNOWN_IID(RunnableObject
),
583 ADD_KNOWN_IID(RunningObjectTable
),
584 ADD_KNOWN_IID(StdMarshalInfo
),
585 ADD_KNOWN_IID(Storage
),
586 ADD_KNOWN_IID(Stream
),
587 ADD_KNOWN_IID(StubManager
),
588 ADD_KNOWN_IID(Unknown
),
589 ADD_KNOWN_IID(ViewObject
),
590 ADD_KNOWN_IID(ViewObject2
),
593 // don't clobber preprocessor name space
596 // try to find the interface in the table
597 for ( size_t ui
= 0; ui
< WXSIZEOF(aKnownIids
); ui
++ ) {
598 if ( riid
== *aKnownIids
[ui
].pIid
) {
599 return aKnownIids
[ui
].szName
;
604 // unknown IID, just transform to string
606 return wxString((const wxChar
*)uuid
);
608 return wxEmptyString
;
612 WXDLLEXPORT
void wxLogQueryInterface(const wxChar
*szInterface
, REFIID riid
)
614 wxLogTrace(wxTRACE_OleCalls
, wxT("%s::QueryInterface (iid = %s)"),
615 szInterface
, GetIidName(riid
).c_str());
618 WXDLLEXPORT
void wxLogAddRef(const wxChar
*szInterface
, ULONG cRef
)
620 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::AddRef: m_cRef = %d"), szInterface
, cRef
+ 1);
623 WXDLLEXPORT
void wxLogRelease(const wxChar
*szInterface
, ULONG cRef
)
625 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::Release: m_cRef = %d"), szInterface
, cRef
- 1);
628 #endif // wxDEBUG_LEVEL
630 #endif // wxUSE_DATAOBJ
632 #endif // __CYGWIN10__