]>
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() ;
152 else if (type
== wxT("longlong"))
154 oleVariant
.vt
= VT_I8
;
155 oleVariant
.llVal
= variant
.GetLongLong().GetValue();
158 else if (type
== wxT("char"))
160 oleVariant
.vt
=VT_I1
; // Signed Char
161 oleVariant
.cVal
=variant
.GetChar();
163 else if (type
== wxT("double"))
165 oleVariant
.vt
= VT_R8
;
166 oleVariant
.dblVal
= variant
.GetDouble();
168 else if (type
== wxT("bool"))
170 oleVariant
.vt
= VT_BOOL
;
171 oleVariant
.boolVal
= variant
.GetBool() ? VARIANT_TRUE
: VARIANT_FALSE
;
173 else if (type
== wxT("string"))
175 wxString
str( variant
.GetString() );
176 oleVariant
.vt
= VT_BSTR
;
177 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
180 else if (type
== wxT("datetime"))
182 wxDateTime
date( variant
.GetDateTime() );
183 oleVariant
.vt
= VT_DATE
;
186 date
.GetAsMSWSysTime(&st
);
188 SystemTimeToVariantTime(&st
, &oleVariant
.date
);
191 else if (type
== wxT("void*"))
193 oleVariant
.vt
= VT_DISPATCH
;
194 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
196 else if (type
== wxT("list") || type
== wxT("stringlist"))
198 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
201 SAFEARRAYBOUND saBound
;
202 VARIANTARG
*pvargBase
;
206 int iCount
= variant
.GetCount();
209 saBound
.cElements
= iCount
;
211 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
215 SafeArrayAccessData(psa
, (void**)&pvargBase
);
218 for (i
= 0; i
< iCount
; i
++)
220 // copy each string in the list of strings
221 wxVariant
eachVariant(variant
[i
]);
222 if (!wxConvertVariantToOle(eachVariant
, * pvarg
))
224 // memory failure: back out and free strings alloc'ed up to
225 // now, and then the array itself.
227 for (j
= 0; j
< i
; j
++)
229 SysFreeString(pvarg
->bstrVal
);
232 SafeArrayDestroy(psa
);
238 SafeArrayUnaccessData(psa
);
240 oleVariant
.parray
= psa
;
244 oleVariant
.vt
= VT_NULL
;
251 #define VT_TYPEMASK 0xfff
255 wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
258 if ( oleVariant
.vt
& VT_ARRAY
)
261 // Compute the total number of elements in all array dimensions
263 for ( int cDims
= 0; cDims
< oleVariant
.parray
->cDims
; cDims
++ )
264 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
266 // Get a pointer to the data
268 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, &pvdata
);
272 switch (oleVariant
.vt
& VT_TYPEMASK
)
277 VARIANTARG
*variant_data
=(VARIANTARG
*)pvdata
;
278 for ( int i
= 0; i
< cElements
; i
++ )
280 VARIANTARG
& oleElement
= variant_data
[i
];
282 if ( !wxConvertOleToVariant(oleElement
, vElement
) )
289 variant
.Append(vElement
);
296 wxArrayString strings
;
297 BSTR
*string_val
=(BSTR
*)pvdata
;
298 for ( int i
= 0; i
< cElements
; ++i
)
300 wxString str
=wxConvertStringFromOle(*string_val
);
309 wxLogDebug(wxT("unhandled VT_ARRAY type %x in wxConvertOleToVariant"),
310 oleVariant
.vt
& VT_TYPEMASK
);
311 variant
= wxVariant();
316 SafeArrayUnaccessData(oleVariant
.parray
);
318 else if ( oleVariant
.vt
& VT_BYREF
)
320 switch ( oleVariant
.vt
& VT_TYPEMASK
)
324 VARIANTARG
& oleReference
= *((LPVARIANT
)oleVariant
.byref
);
325 if (!wxConvertOleToVariant(oleReference
,variant
))
331 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"),
336 else // simply type (not array or reference)
338 switch ( oleVariant
.vt
& VT_TYPEMASK
)
342 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
351 VariantTimeToSystemTime(oleVariant
.date
, &st
);
354 date
.SetFromMSWSysTime(st
);
357 #endif // wxUSE_DATETIME
362 variant
= wxLongLong(oleVariant
.llVal
);
364 #endif // wxUSE_LONGLONG
367 variant
= (long) oleVariant
.lVal
;
371 variant
= (long) oleVariant
.iVal
;
375 variant
= oleVariant
.boolVal
!= 0;
379 variant
= oleVariant
.fltVal
;
383 variant
= oleVariant
.dblVal
;
387 variant
= (void*) oleVariant
.pdispVal
;
395 break; // Ignore Empty Variant, used only during destruction of objects
398 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"),
399 oleVariant
.vt
,oleVariant
.vt
&VT_TYPEMASK
);
407 #endif // wxUSE_VARIANT
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
416 #if wxDEBUG_LEVEL && (( defined(__VISUALC__) && (__VISUALC__ > 1000) ))
417 static wxString
GetIidName(REFIID riid
)
419 // an association between symbolic name and numeric value of an IID
422 const wxChar
*szName
;
425 // construct the table containing all known interfaces
426 #define ADD_KNOWN_IID(name) { &IID_I##name, wxT(#name) }
428 static const KNOWN_IID aKnownIids
[] = {
429 ADD_KNOWN_IID(AdviseSink
),
430 ADD_KNOWN_IID(AdviseSink2
),
431 ADD_KNOWN_IID(BindCtx
),
432 ADD_KNOWN_IID(ClassFactory
),
433 #if ( !defined( __VISUALC__) || (__VISUALC__!=1010) )
434 ADD_KNOWN_IID(ContinueCallback
),
435 ADD_KNOWN_IID(EnumOleDocumentViews
),
436 ADD_KNOWN_IID(OleCommandTarget
),
437 ADD_KNOWN_IID(OleDocument
),
438 ADD_KNOWN_IID(OleDocumentSite
),
439 ADD_KNOWN_IID(OleDocumentView
),
440 ADD_KNOWN_IID(Print
),
442 ADD_KNOWN_IID(DataAdviseHolder
),
443 ADD_KNOWN_IID(DataObject
),
444 ADD_KNOWN_IID(Debug
),
445 ADD_KNOWN_IID(DebugStream
),
446 ADD_KNOWN_IID(DfReserved1
),
447 ADD_KNOWN_IID(DfReserved2
),
448 ADD_KNOWN_IID(DfReserved3
),
449 ADD_KNOWN_IID(Dispatch
),
450 ADD_KNOWN_IID(DropSource
),
451 ADD_KNOWN_IID(DropTarget
),
452 ADD_KNOWN_IID(EnumCallback
),
453 ADD_KNOWN_IID(EnumFORMATETC
),
454 ADD_KNOWN_IID(EnumGeneric
),
455 ADD_KNOWN_IID(EnumHolder
),
456 ADD_KNOWN_IID(EnumMoniker
),
457 ADD_KNOWN_IID(EnumOLEVERB
),
458 ADD_KNOWN_IID(EnumSTATDATA
),
459 ADD_KNOWN_IID(EnumSTATSTG
),
460 ADD_KNOWN_IID(EnumString
),
461 ADD_KNOWN_IID(EnumUnknown
),
462 ADD_KNOWN_IID(EnumVARIANT
),
463 ADD_KNOWN_IID(ExternalConnection
),
464 ADD_KNOWN_IID(InternalMoniker
),
465 ADD_KNOWN_IID(LockBytes
),
466 ADD_KNOWN_IID(Malloc
),
467 ADD_KNOWN_IID(Marshal
),
468 ADD_KNOWN_IID(MessageFilter
),
469 ADD_KNOWN_IID(Moniker
),
470 ADD_KNOWN_IID(OleAdviseHolder
),
471 ADD_KNOWN_IID(OleCache
),
472 ADD_KNOWN_IID(OleCache2
),
473 ADD_KNOWN_IID(OleCacheControl
),
474 ADD_KNOWN_IID(OleClientSite
),
475 ADD_KNOWN_IID(OleContainer
),
476 ADD_KNOWN_IID(OleInPlaceActiveObject
),
477 ADD_KNOWN_IID(OleInPlaceFrame
),
478 ADD_KNOWN_IID(OleInPlaceObject
),
479 ADD_KNOWN_IID(OleInPlaceSite
),
480 ADD_KNOWN_IID(OleInPlaceUIWindow
),
481 ADD_KNOWN_IID(OleItemContainer
),
482 ADD_KNOWN_IID(OleLink
),
483 ADD_KNOWN_IID(OleManager
),
484 ADD_KNOWN_IID(OleObject
),
485 ADD_KNOWN_IID(OlePresObj
),
486 ADD_KNOWN_IID(OleWindow
),
487 ADD_KNOWN_IID(PSFactory
),
488 ADD_KNOWN_IID(ParseDisplayName
),
489 ADD_KNOWN_IID(Persist
),
490 ADD_KNOWN_IID(PersistFile
),
491 ADD_KNOWN_IID(PersistStorage
),
492 ADD_KNOWN_IID(PersistStream
),
493 ADD_KNOWN_IID(ProxyManager
),
494 ADD_KNOWN_IID(RootStorage
),
495 ADD_KNOWN_IID(RpcChannel
),
496 ADD_KNOWN_IID(RpcProxy
),
497 ADD_KNOWN_IID(RpcStub
),
498 ADD_KNOWN_IID(RunnableObject
),
499 ADD_KNOWN_IID(RunningObjectTable
),
500 ADD_KNOWN_IID(StdMarshalInfo
),
501 ADD_KNOWN_IID(Storage
),
502 ADD_KNOWN_IID(Stream
),
503 ADD_KNOWN_IID(StubManager
),
504 ADD_KNOWN_IID(Unknown
),
505 ADD_KNOWN_IID(ViewObject
),
506 ADD_KNOWN_IID(ViewObject2
),
509 // don't clobber preprocessor name space
512 // try to find the interface in the table
513 for ( size_t ui
= 0; ui
< WXSIZEOF(aKnownIids
); ui
++ ) {
514 if ( riid
== *aKnownIids
[ui
].pIid
) {
515 return aKnownIids
[ui
].szName
;
520 // unknown IID, just transform to string
522 return wxString((const wxChar
*)uuid
);
524 return wxEmptyString
;
528 WXDLLEXPORT
void wxLogQueryInterface(const wxChar
*szInterface
, REFIID riid
)
530 wxLogTrace(wxTRACE_OleCalls
, wxT("%s::QueryInterface (iid = %s)"),
531 szInterface
, GetIidName(riid
).c_str());
534 WXDLLEXPORT
void wxLogAddRef(const wxChar
*szInterface
, ULONG cRef
)
536 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::AddRef: m_cRef = %d"), szInterface
, cRef
+ 1);
539 WXDLLEXPORT
void wxLogRelease(const wxChar
*szInterface
, ULONG cRef
)
541 wxLogTrace(wxTRACE_OleCalls
, wxT("After %s::Release: m_cRef = %d"), szInterface
, cRef
- 1);
544 #endif // wxDEBUG_LEVEL
546 #endif // wxUSE_DATAOBJ
548 #endif // __CYGWIN10__