]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/oleutils.cpp
Make public headers compatible with Objective-C++ with ARC.
[wxWidgets.git] / src / msw / ole / oleutils.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ole/oleutils.cpp
3 // Purpose: implementation of OLE helper functions
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 19.02.98
7 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // Declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
21
22 #if defined(__BORLANDC__)
23 #pragma hdrstop
24 #endif
25
26 #if wxUSE_OLE
27
28 #ifndef WX_PRECOMP
29 #include "wx/log.h"
30 #endif
31
32 #ifndef __CYGWIN10__
33
34 #include "wx/msw/private.h"
35
36 #ifdef __WXWINCE__
37 #include <winreg.h>
38 #include <ole2.h>
39
40 #define GUID_DEFINED
41 #define UUID_DEFINED
42 #endif
43
44 // OLE
45 #ifndef __WXWINCE__
46 #include "wx/msw/ole/uuid.h"
47 #endif
48
49 #include "wx/msw/ole/oleutils.h"
50 #include "wx/msw/ole/safearray.h"
51
52 #if defined(__VISUALC__) && (__VISUALC__ > 1000)
53 #include <docobj.h>
54 #endif
55
56 // ============================================================================
57 // Implementation
58 // ============================================================================
59
60 // return true if the iid is in the array
61 WXDLLEXPORT bool IsIidFromList(REFIID riid, const IID *aIids[], size_t nCount)
62 {
63 for ( size_t i = 0; i < nCount; i++ ) {
64 if ( riid == *aIids[i] )
65 return true;
66 }
67
68 return false;
69 }
70
71 WXDLLEXPORT BSTR wxConvertStringToOle(const wxString& str)
72 {
73 return wxBasicString(str).Get();
74 }
75
76 WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr)
77 {
78 // NULL BSTR is equivalent to an empty string (this is the convention used
79 // by VB and hence we must follow it)
80 if ( !bStr )
81 return wxString();
82
83 const int len = SysStringLen(bStr);
84
85 #if wxUSE_UNICODE
86 wxString str(bStr, len);
87 #else
88 wxString str;
89 if (len)
90 {
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 */,
94 buf, len,
95 NULL, NULL /* no default char */));
96 }
97 #endif
98
99 return str;
100 }
101
102 // ----------------------------------------------------------------------------
103 // wxBasicString
104 // ----------------------------------------------------------------------------
105
106 wxBasicString::wxBasicString(const wxString& str)
107 {
108 m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent));
109 }
110
111 wxBasicString::wxBasicString(const wxBasicString& src)
112 {
113 m_bstrBuf = src.Get();
114 }
115
116 wxBasicString& wxBasicString::operator=(const wxBasicString& src)
117 {
118 SysReAllocString(&m_bstrBuf, src);
119 return *this;
120 }
121
122 wxBasicString::~wxBasicString()
123 {
124 SysFreeString(m_bstrBuf);
125 }
126
127
128 // ----------------------------------------------------------------------------
129 // Convert variants
130 // ----------------------------------------------------------------------------
131
132 #if wxUSE_VARIANT
133
134 // ----------------------------------------------------------------------------
135 // wxVariantDataCurrency
136 // ----------------------------------------------------------------------------
137
138
139 #if wxUSE_ANY
140
141 bool wxVariantDataCurrency::GetAsAny(wxAny* any) const
142 {
143 *any = m_value;
144 return true;
145 }
146
147 wxVariantData* wxVariantDataCurrency::VariantDataFactory(const wxAny& any)
148 {
149 return new wxVariantDataCurrency(wxANY_AS(any, CURRENCY));
150 }
151
152 REGISTER_WXANY_CONVERSION(CURRENCY, wxVariantDataCurrency)
153
154 #endif // wxUSE_ANY
155
156 bool wxVariantDataCurrency::Eq(wxVariantData& data) const
157 {
158 wxASSERT_MSG( (data.GetType() == wxS("currency")),
159 "wxVariantDataCurrency::Eq: argument mismatch" );
160
161 wxVariantDataCurrency& otherData = (wxVariantDataCurrency&) data;
162
163 return otherData.m_value.int64 == m_value.int64;
164 }
165
166 #if wxUSE_STD_IOSTREAM
167 bool wxVariantDataCurrency::Write(wxSTD ostream& str) const
168 {
169 wxString s;
170 Write(s);
171 str << s;
172 return true;
173 }
174 #endif
175
176 bool wxVariantDataCurrency::Write(wxString& str) const
177 {
178 BSTR bStr = NULL;
179 if ( SUCCEEDED(VarBstrFromCy(m_value, LOCALE_USER_DEFAULT, 0, &bStr)) )
180 {
181 str = wxConvertStringFromOle(bStr);
182 SysFreeString(bStr);
183 return true;
184 }
185 return false;
186 }
187
188 // ----------------------------------------------------------------------------
189 // wxVariantDataErrorCode
190 // ----------------------------------------------------------------------------
191
192 #if wxUSE_ANY
193
194 bool wxVariantDataErrorCode::GetAsAny(wxAny* any) const
195 {
196 *any = m_value;
197 return true;
198 }
199
200 wxVariantData* wxVariantDataErrorCode::VariantDataFactory(const wxAny& any)
201 {
202 return new wxVariantDataErrorCode(wxANY_AS(any, SCODE));
203 }
204
205 REGISTER_WXANY_CONVERSION(SCODE, wxVariantDataErrorCode)
206
207 #endif // wxUSE_ANY
208
209 bool wxVariantDataErrorCode::Eq(wxVariantData& data) const
210 {
211 wxASSERT_MSG( (data.GetType() == wxS("errorcode")),
212 "wxVariantDataErrorCode::Eq: argument mismatch" );
213
214 wxVariantDataErrorCode& otherData = (wxVariantDataErrorCode&) data;
215
216 return otherData.m_value == m_value;
217 }
218
219 #if wxUSE_STD_IOSTREAM
220 bool wxVariantDataErrorCode::Write(wxSTD ostream& str) const
221 {
222 wxString s;
223 Write(s);
224 str << s;
225 return true;
226 }
227 #endif
228
229 bool wxVariantDataErrorCode::Write(wxString& str) const
230 {
231 str << m_value;
232 return true;
233 }
234
235
236 // ----------------------------------------------------------------------------
237 // wxVariantDataSafeArray
238 // ----------------------------------------------------------------------------
239
240 #if wxUSE_ANY
241
242 bool wxVariantDataSafeArray::GetAsAny(wxAny* any) const
243 {
244 *any = m_value;
245 return true;
246 }
247
248 wxVariantData* wxVariantDataSafeArray::VariantDataFactory(const wxAny& any)
249 {
250 return new wxVariantDataSafeArray(wxANY_AS(any, SAFEARRAY*));
251 }
252
253 REGISTER_WXANY_CONVERSION(SAFEARRAY*, wxVariantDataSafeArray)
254
255 #endif // wxUSE_ANY
256
257 bool wxVariantDataSafeArray::Eq(wxVariantData& data) const
258 {
259 wxASSERT_MSG( (data.GetType() == wxS("safearray")),
260 "wxVariantDataSafeArray::Eq: argument mismatch" );
261
262 wxVariantDataSafeArray& otherData = (wxVariantDataSafeArray&) data;
263
264 return otherData.m_value == m_value;
265 }
266
267 #if wxUSE_STD_IOSTREAM
268 bool wxVariantDataSafeArray::Write(wxSTD ostream& str) const
269 {
270 wxString s;
271 Write(s);
272 str << s;
273 return true;
274 }
275 #endif
276
277 bool wxVariantDataSafeArray::Write(wxString& str) const
278 {
279 str.Printf(wxS("SAFEARRAY: %p"), (void*)m_value);
280 return true;
281 }
282
283 WXDLLEXPORT bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant)
284 {
285 VariantInit(&oleVariant);
286 if (variant.IsNull())
287 {
288 oleVariant.vt = VT_NULL;
289 return true;
290 }
291
292 wxString type(variant.GetType());
293
294 if (type == wxT("errorcode"))
295 {
296 wxVariantDataErrorCode* const
297 ec = wxStaticCastVariantData(variant.GetData(),
298 wxVariantDataErrorCode);
299 oleVariant.vt = VT_ERROR;
300 oleVariant.scode = ec->GetValue();
301 }
302 else if (type == wxT("currency"))
303 {
304 wxVariantDataCurrency* const
305 c = wxStaticCastVariantData(variant.GetData(),
306 wxVariantDataCurrency);
307 oleVariant.vt = VT_CY;
308 oleVariant.cyVal = c->GetValue();
309 }
310 else if (type == wxT("safearray"))
311 {
312 wxVariantDataSafeArray* const
313 vsa = wxStaticCastVariantData(variant.GetData(),
314 wxVariantDataSafeArray);
315 SAFEARRAY* psa = vsa->GetValue();
316 VARTYPE vt;
317
318 wxCHECK(psa, false);
319 HRESULT hr = SafeArrayGetVartype(psa, &vt);
320 if ( FAILED(hr) )
321 {
322 wxLogApiError(wxS("SafeArrayGetVartype()"), hr);
323 SafeArrayDestroy(psa);
324 return false;
325 }
326 oleVariant.vt = vt | VT_ARRAY;
327 oleVariant.parray = psa;
328 }
329 else if (type == wxT("long"))
330 {
331 oleVariant.vt = VT_I4;
332 oleVariant.lVal = variant.GetLong() ;
333 }
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
339 // check himself.
340 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
341 else if (type == wxT("longlong"))
342 {
343 oleVariant.vt = VT_I8;
344 oleVariant.llVal = variant.GetLongLong().GetValue();
345 }
346 #endif
347 else if (type == wxT("char"))
348 {
349 oleVariant.vt=VT_I1; // Signed Char
350 oleVariant.cVal=variant.GetChar();
351 }
352 else if (type == wxT("double"))
353 {
354 oleVariant.vt = VT_R8;
355 oleVariant.dblVal = variant.GetDouble();
356 }
357 else if (type == wxT("bool"))
358 {
359 oleVariant.vt = VT_BOOL;
360 oleVariant.boolVal = variant.GetBool() ? VARIANT_TRUE : VARIANT_FALSE;
361 }
362 else if (type == wxT("string"))
363 {
364 wxString str( variant.GetString() );
365 oleVariant.vt = VT_BSTR;
366 oleVariant.bstrVal = wxConvertStringToOle(str);
367 }
368 #if wxUSE_DATETIME
369 else if (type == wxT("datetime"))
370 {
371 wxDateTime date( variant.GetDateTime() );
372 oleVariant.vt = VT_DATE;
373
374 SYSTEMTIME st;
375 date.GetAsMSWSysTime(&st);
376
377 SystemTimeToVariantTime(&st, &oleVariant.date);
378 }
379 #endif
380 else if (type == wxT("void*"))
381 {
382 oleVariant.vt = VT_DISPATCH;
383 oleVariant.pdispVal = (IDispatch*) variant.GetVoidPtr();
384 }
385 else if (type == wxT("list"))
386 {
387 wxSafeArray<VT_VARIANT> safeArray;
388 if (!safeArray.CreateFromListVariant(variant))
389 return false;
390
391 oleVariant.vt = VT_VARIANT | VT_ARRAY;
392 oleVariant.parray = safeArray.Detach();
393 }
394 else if (type == wxT("arrstring"))
395 {
396 wxSafeArray<VT_BSTR> safeArray;
397
398 if (!safeArray.CreateFromArrayString(variant.GetArrayString()))
399 return false;
400
401 oleVariant.vt = VT_BSTR | VT_ARRAY;
402 oleVariant.parray = safeArray.Detach();
403 }
404 else
405 {
406 oleVariant.vt = VT_NULL;
407 return false;
408 }
409 return true;
410 }
411
412 #ifndef VT_TYPEMASK
413 #define VT_TYPEMASK 0xfff
414 #endif
415
416 WXDLLEXPORT bool
417 wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant)
418 {
419 bool ok = true;
420 if ( oleVariant.vt & VT_ARRAY )
421 {
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:
426 //
427 // if ( oleVariant.parray && SafeArrayGetDim(oleVariant.parray) > 1 )
428 // {
429 // variant.SetData(new wxVariantDataSafeArray(oleVariant.parray));
430 // }
431 //
432 // but currently we don't do it for compatibility reasons.
433 switch (oleVariant.vt & VT_TYPEMASK)
434 {
435 case VT_I2:
436 ok = wxSafeArray<VT_I2>::ConvertToVariant(oleVariant.parray, variant);
437 break;
438 case VT_I4:
439 ok = wxSafeArray<VT_I4>::ConvertToVariant(oleVariant.parray, variant);
440 break;
441 case VT_R4:
442 ok = wxSafeArray<VT_R4>::ConvertToVariant(oleVariant.parray, variant);
443 break;
444 case VT_R8:
445 ok = wxSafeArray<VT_R8>::ConvertToVariant(oleVariant.parray, variant);
446 break;
447 case VT_VARIANT:
448 ok = wxSafeArray<VT_VARIANT>::ConvertToVariant(oleVariant.parray, variant);
449 break;
450 case VT_BSTR:
451 {
452 wxArrayString strings;
453 if ( wxSafeArray<VT_BSTR>::ConvertToArrayString(oleVariant.parray, strings) )
454 variant = strings;
455 else
456 ok = false;
457 }
458 break;
459 default:
460 ok = false;
461 break;
462 }
463 if ( !ok )
464 {
465 wxLogDebug(wxT("unhandled VT_ARRAY type %x in wxConvertOleToVariant"),
466 oleVariant.vt & VT_TYPEMASK);
467 variant = wxVariant();
468 }
469 }
470 else if ( oleVariant.vt & VT_BYREF )
471 {
472 switch ( oleVariant.vt & VT_TYPEMASK )
473 {
474 case VT_VARIANT:
475 {
476 VARIANTARG& oleReference = *((LPVARIANT)oleVariant.byref);
477 if (!wxConvertOleToVariant(oleReference,variant))
478 return false;
479 break;
480 }
481
482 default:
483 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"),
484 oleVariant.vt);
485 return false;
486 }
487 }
488 else // simply type (not array or reference)
489 {
490 switch ( oleVariant.vt & VT_TYPEMASK )
491 {
492 case VT_ERROR:
493 variant.SetData(new wxVariantDataErrorCode(oleVariant.scode));
494 break;
495
496 case VT_CY:
497 variant.SetData(new wxVariantDataCurrency(oleVariant.cyVal));
498 break;
499
500 case VT_BSTR:
501 {
502 wxString str(wxConvertStringFromOle(oleVariant.bstrVal));
503 variant = str;
504 }
505 break;
506
507 case VT_DATE:
508 #if wxUSE_DATETIME
509 {
510 SYSTEMTIME st;
511 VariantTimeToSystemTime(oleVariant.date, &st);
512
513 wxDateTime date;
514 date.SetFromMSWSysTime(st);
515 variant = date;
516 }
517 #endif // wxUSE_DATETIME
518 break;
519
520 // See the comment before the __VISUALC6__ test above.
521 #if wxUSE_LONGLONG && !defined(__VISUALC6__)
522 case VT_I8:
523 variant = wxLongLong(oleVariant.llVal);
524 break;
525 #endif // wxUSE_LONGLONG
526
527 case VT_I4:
528 variant = (long) oleVariant.lVal;
529 break;
530
531 case VT_I2:
532 variant = (long) oleVariant.iVal;
533 break;
534
535 case VT_BOOL:
536 variant = oleVariant.boolVal != 0;
537 break;
538
539 case VT_R4:
540 variant = oleVariant.fltVal;
541 break;
542
543 case VT_R8:
544 variant = oleVariant.dblVal;
545 break;
546
547 case VT_DISPATCH:
548 variant = (void*) oleVariant.pdispVal;
549 break;
550
551 case VT_NULL:
552 variant.MakeNull();
553 break;
554
555 case VT_EMPTY:
556 break; // Ignore Empty Variant, used only during destruction of objects
557
558 default:
559 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"),
560 oleVariant.vt,oleVariant.vt&VT_TYPEMASK);
561 return false;
562 }
563 }
564
565 return ok;
566 }
567
568 #endif // wxUSE_VARIANT
569
570
571 // ----------------------------------------------------------------------------
572 // Debug support
573 // ----------------------------------------------------------------------------
574
575 #if wxUSE_DATAOBJ
576
577 #if wxDEBUG_LEVEL && (( defined(__VISUALC__) && (__VISUALC__ > 1000) ))
578 static wxString GetIidName(REFIID riid)
579 {
580 // an association between symbolic name and numeric value of an IID
581 struct KNOWN_IID {
582 const IID *pIid;
583 const wxChar *szName;
584 };
585
586 // construct the table containing all known interfaces
587 #define ADD_KNOWN_IID(name) { &IID_I##name, wxT(#name) }
588
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),
602 #endif
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),
668 };
669
670 // don't clobber preprocessor name space
671 #undef ADD_KNOWN_IID
672
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;
677 }
678 }
679
680 #ifndef __WXWINCE__
681 // unknown IID, just transform to string
682 Uuid uuid(riid);
683 return wxString((const wxChar *)uuid);
684 #else
685 return wxEmptyString;
686 #endif
687 }
688
689 WXDLLEXPORT void wxLogQueryInterface(const wxChar *szInterface, REFIID riid)
690 {
691 wxLogTrace(wxTRACE_OleCalls, wxT("%s::QueryInterface (iid = %s)"),
692 szInterface, GetIidName(riid).c_str());
693 }
694
695 WXDLLEXPORT void wxLogAddRef(const wxChar *szInterface, ULONG cRef)
696 {
697 wxLogTrace(wxTRACE_OleCalls, wxT("After %s::AddRef: m_cRef = %d"), szInterface, cRef + 1);
698 }
699
700 WXDLLEXPORT void wxLogRelease(const wxChar *szInterface, ULONG cRef)
701 {
702 wxLogTrace(wxTRACE_OleCalls, wxT("After %s::Release: m_cRef = %d"), szInterface, cRef - 1);
703 }
704
705 #endif // wxDEBUG_LEVEL
706
707 #endif // wxUSE_DATAOBJ
708
709 #endif // __CYGWIN10__
710
711 #endif // wxUSE_OLE