]>
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 // ----------------------------------------------------------------------------
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 // Original VC6 came with SDK too old to contain VARIANT::llVal declaration
152 // and there doesn't seem to be any way to test for it as Microsoft simply
153 // added it to the later version of oaidl.h without changing anything else.
154 // So assume it's not present for VC6, even though it might be if an
155 // updated SDK is used. In this case the user would need to disable this
157 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
158 else if (type
== wxT("longlong"))
160 oleVariant
.vt
= VT_I8
;
161 oleVariant
.llVal
= variant
.GetLongLong().GetValue();
164 else if (type
== wxT("char"))
166 oleVariant
.vt
=VT_I1
; // Signed Char
167 oleVariant
.cVal
=variant
.GetChar();
169 else if (type
== wxT("double"))
171 oleVariant
.vt
= VT_R8
;
172 oleVariant
.dblVal
= variant
.GetDouble();
174 else if (type
== wxT("bool"))
176 oleVariant
.vt
= VT_BOOL
;
177 oleVariant
.boolVal
= variant
.GetBool() ? VARIANT_TRUE
: VARIANT_FALSE
;
179 else if (type
== wxT("string"))
181 wxString
str( variant
.GetString() );
182 oleVariant
.vt
= VT_BSTR
;
183 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
186 else if (type
== wxT("datetime"))
188 wxDateTime
date( variant
.GetDateTime() );
189 oleVariant
.vt
= VT_DATE
;
192 date
.GetAsMSWSysTime(&st
);
194 SystemTimeToVariantTime(&st
, &oleVariant
.date
);
197 else if (type
== wxT("void*"))
199 oleVariant
.vt
= VT_DISPATCH
;
200 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
202 else if (type
== wxT("list") || type
== wxT("stringlist"))
204 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
207 SAFEARRAYBOUND saBound
;
208 VARIANTARG
*pvargBase
;
212 int iCount
= variant
.GetCount();
215 saBound
.cElements
= iCount
;
217 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
221 SafeArrayAccessData(psa
, (void**)&pvargBase
);
224 for (i
= 0; i
< iCount
; i
++)
226 // copy each string in the list of strings
227 wxVariant
eachVariant(variant
[i
]);
228 if (!wxConvertVariantToOle(eachVariant
, * pvarg
))
230 // memory failure: back out and free strings alloc'ed up to
231 // now, and then the array itself.
233 for (j
= 0; j
< i
; j
++)
235 SysFreeString(pvarg
->bstrVal
);
238 SafeArrayDestroy(psa
);
244 SafeArrayUnaccessData(psa
);
246 oleVariant
.parray
= psa
;
250 oleVariant
.vt
= VT_NULL
;
257 #define VT_TYPEMASK 0xfff
261 wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
264 if ( oleVariant
.vt
& VT_ARRAY
)
267 // Compute the total number of elements in all array dimensions
269 for ( int cDims
= 0; cDims
< oleVariant
.parray
->cDims
; cDims
++ )
270 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
272 // Get a pointer to the data
274 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, &pvdata
);
278 switch (oleVariant
.vt
& VT_TYPEMASK
)
283 VARIANTARG
*variant_data
=(VARIANTARG
*)pvdata
;
284 for ( int i
= 0; i
< cElements
; i
++ )
286 VARIANTARG
& oleElement
= variant_data
[i
];
288 if ( !wxConvertOleToVariant(oleElement
, vElement
) )
295 variant
.Append(vElement
);
302 wxArrayString strings
;
303 BSTR
*string_val
=(BSTR
*)pvdata
;
304 for ( int i
= 0; i
< cElements
; ++i
)
306 wxString str
=wxConvertStringFromOle(*string_val
);
315 wxLogDebug(wxT("unhandled VT_ARRAY type %x in wxConvertOleToVariant"),
316 oleVariant
.vt
& VT_TYPEMASK
);
317 variant
= wxVariant();
322 SafeArrayUnaccessData(oleVariant
.parray
);
324 else if ( oleVariant
.vt
& VT_BYREF
)
326 switch ( oleVariant
.vt
& VT_TYPEMASK
)
330 VARIANTARG
& oleReference
= *((LPVARIANT
)oleVariant
.byref
);
331 if (!wxConvertOleToVariant(oleReference
,variant
))
337 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"),
342 else // simply type (not array or reference)
344 switch ( oleVariant
.vt
& VT_TYPEMASK
)
348 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
357 VariantTimeToSystemTime(oleVariant
.date
, &st
);
360 date
.SetFromMSWSysTime(st
);
363 #endif // wxUSE_DATETIME
366 // See the comment before the __VISUALC6__ test above.
367 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
369 variant
= wxLongLong(oleVariant
.llVal
);
371 #endif // wxUSE_LONGLONG
374 variant
= (long) oleVariant
.lVal
;
378 variant
= (long) oleVariant
.iVal
;
382 variant
= oleVariant
.boolVal
!= 0;
386 variant
= oleVariant
.fltVal
;
390 variant
= oleVariant
.dblVal
;
394 variant
= (void*) oleVariant
.pdispVal
;
402 break; // Ignore Empty Variant, used only during destruction of objects
405 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"),
406 oleVariant
.vt
,oleVariant
.vt
&VT_TYPEMASK
);
414 #endif // wxUSE_VARIANT
417 // ----------------------------------------------------------------------------
419 // ----------------------------------------------------------------------------
423 #if wxDEBUG_LEVEL && (( defined(__VISUALC__) && (__VISUALC__ > 1000) ))
424 static wxString
GetIidName(REFIID riid
)
426 // an association between symbolic name and numeric value of an IID
429 const wxChar
*szName
;
432 // construct the table containing all known interfaces
433 #define ADD_KNOWN_IID(name) { &IID_I##name, wxT(#name) }
435 static const KNOWN_IID aKnownIids
[] = {
436 ADD_KNOWN_IID(AdviseSink
),
437 ADD_KNOWN_IID(AdviseSink2
),
438 ADD_KNOWN_IID(BindCtx
),
439 ADD_KNOWN_IID(ClassFactory
),
440 #if ( !defined( __VISUALC__) || (__VISUALC__!=1010) )
441 ADD_KNOWN_IID(ContinueCallback
),
442 ADD_KNOWN_IID(EnumOleDocumentViews
),
443 ADD_KNOWN_IID(OleCommandTarget
),
444 ADD_KNOWN_IID(OleDocument
),
445 ADD_KNOWN_IID(OleDocumentSite
),
446 ADD_KNOWN_IID(OleDocumentView
),
447 ADD_KNOWN_IID(Print
),
449 ADD_KNOWN_IID(DataAdviseHolder
),
450 ADD_KNOWN_IID(DataObject
),
451 ADD_KNOWN_IID(Debug
),
452 ADD_KNOWN_IID(DebugStream
),
453 ADD_KNOWN_IID(DfReserved1
),
454 ADD_KNOWN_IID(DfReserved2
),
455 ADD_KNOWN_IID(DfReserved3
),
456 ADD_KNOWN_IID(Dispatch
),
457 ADD_KNOWN_IID(DropSource
),
458 ADD_KNOWN_IID(DropTarget
),
459 ADD_KNOWN_IID(EnumCallback
),
460 ADD_KNOWN_IID(EnumFORMATETC
),
461 ADD_KNOWN_IID(EnumGeneric
),
462 ADD_KNOWN_IID(EnumHolder
),
463 ADD_KNOWN_IID(EnumMoniker
),
464 ADD_KNOWN_IID(EnumOLEVERB
),
465 ADD_KNOWN_IID(EnumSTATDATA
),
466 ADD_KNOWN_IID(EnumSTATSTG
),
467 ADD_KNOWN_IID(EnumString
),
468 ADD_KNOWN_IID(EnumUnknown
),
469 ADD_KNOWN_IID(EnumVARIANT
),
470 ADD_KNOWN_IID(ExternalConnection
),
471 ADD_KNOWN_IID(InternalMoniker
),
472 ADD_KNOWN_IID(LockBytes
),
473 ADD_KNOWN_IID(Malloc
),
474 ADD_KNOWN_IID(Marshal
),
475 ADD_KNOWN_IID(MessageFilter
),
476 ADD_KNOWN_IID(Moniker
),
477 ADD_KNOWN_IID(OleAdviseHolder
),
478 ADD_KNOWN_IID(OleCache
),
479 ADD_KNOWN_IID(OleCache2
),
480 ADD_KNOWN_IID(OleCacheControl
),
481 ADD_KNOWN_IID(OleClientSite
),
482 ADD_KNOWN_IID(OleContainer
),
483 ADD_KNOWN_IID(OleInPlaceActiveObject
),
484 ADD_KNOWN_IID(OleInPlaceFrame
),
485 ADD_KNOWN_IID(OleInPlaceObject
),
486 ADD_KNOWN_IID(OleInPlaceSite
),
487 ADD_KNOWN_IID(OleInPlaceUIWindow
),
488 ADD_KNOWN_IID(OleItemContainer
),
489 ADD_KNOWN_IID(OleLink
),
490 ADD_KNOWN_IID(OleManager
),
491 ADD_KNOWN_IID(OleObject
),
492 ADD_KNOWN_IID(OlePresObj
),
493 ADD_KNOWN_IID(OleWindow
),
494 ADD_KNOWN_IID(PSFactory
),
495 ADD_KNOWN_IID(ParseDisplayName
),
496 ADD_KNOWN_IID(Persist
),
497 ADD_KNOWN_IID(PersistFile
),
498 ADD_KNOWN_IID(PersistStorage
),
499 ADD_KNOWN_IID(PersistStream
),
500 ADD_KNOWN_IID(ProxyManager
),
501 ADD_KNOWN_IID(RootStorage
),
502 ADD_KNOWN_IID(RpcChannel
),
503 ADD_KNOWN_IID(RpcProxy
),
504 ADD_KNOWN_IID(RpcStub
),
505 ADD_KNOWN_IID(RunnableObject
),
506 ADD_KNOWN_IID(RunningObjectTable
),
507 ADD_KNOWN_IID(StdMarshalInfo
),
508 ADD_KNOWN_IID(Storage
),
509 ADD_KNOWN_IID(Stream
),
510 ADD_KNOWN_IID(StubManager
),
511 ADD_KNOWN_IID(Unknown
),
512 ADD_KNOWN_IID(ViewObject
),
513 ADD_KNOWN_IID(ViewObject2
),
516 // don't clobber preprocessor name space
519 // try to find the interface in the table
520 for ( size_t ui
= 0; ui
< WXSIZEOF(aKnownIids
); ui
++ ) {
521 if ( riid
== *aKnownIids
[ui
].pIid
) {
522 return aKnownIids
[ui
].szName
;
527 // unknown IID, just transform to string
529 return wxString((const wxChar
*)uuid
);
531 return wxEmptyString
;
535 WXDLLEXPORT
void wxLogQueryInterface(const wxChar
*szInterface
, REFIID riid
)
537 wxLogTrace(wxTRACE_OleCalls
, wxT("%s::QueryInterface (iid = %s)"),
538 szInterface
, GetIidName(riid
).c_str());
541 WXDLLEXPORT
void wxLogAddRef(const wxChar
*szInterface
, ULONG cRef
)
543 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::AddRef: m_cRef = %d"), szInterface
, cRef
+ 1);
546 WXDLLEXPORT
void wxLogRelease(const wxChar
*szInterface
, ULONG cRef
)
548 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::Release: m_cRef = %d"), szInterface
, cRef
- 1);
551 #endif // wxDEBUG_LEVEL
553 #endif // wxUSE_DATAOBJ
555 #endif // __CYGWIN10__