]>
Commit | Line | Data |
---|---|---|
bbf1f0e5 | 1 | /////////////////////////////////////////////////////////////////////////////// |
521bf4ff | 2 | // Name: src/msw/ole/oleutils.cpp |
bbf1f0e5 KB |
3 | // Purpose: implementation of OLE helper functions |
4 | // Author: Vadim Zeitlin | |
3f4a0c5b | 5 | // Modified by: |
bbf1f0e5 KB |
6 | // Created: 19.02.98 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
65571936 | 9 | // Licence: wxWindows licence |
bbf1f0e5 KB |
10 | /////////////////////////////////////////////////////////////////////////////// |
11 | ||
12 | // ============================================================================ | |
13 | // Declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
bbf1f0e5 | 20 | // For compilers that support precompilation, includes "wx.h". |
bbf1f0e5 KB |
21 | #include "wx/wxprec.h" |
22 | ||
23 | #if defined(__BORLANDC__) | |
e4db172a | 24 | #pragma hdrstop |
bbf1f0e5 KB |
25 | #endif |
26 | ||
21709999 JS |
27 | #if wxUSE_OLE |
28 | ||
e4db172a WS |
29 | #ifndef WX_PRECOMP |
30 | #include "wx/log.h" | |
31 | #endif | |
32 | ||
ae090fdb JS |
33 | #ifndef __CYGWIN10__ |
34 | ||
4676948b JS |
35 | #include "wx/msw/private.h" |
36 | ||
37 | #ifdef __WXWINCE__ | |
38 | #include <winreg.h> | |
39 | #include <ole2.h> | |
40 | ||
41 | #define GUID_DEFINED | |
42 | #define UUID_DEFINED | |
43 | #endif | |
bbf1f0e5 KB |
44 | |
45 | // OLE | |
4676948b | 46 | #ifndef __WXWINCE__ |
3096bd2f | 47 | #include "wx/msw/ole/uuid.h" |
4676948b JS |
48 | #endif |
49 | ||
3096bd2f | 50 | #include "wx/msw/ole/oleutils.h" |
bbf1f0e5 | 51 | |
3f4a0c5b VZ |
52 | #if defined(__VISUALC__) && (__VISUALC__ > 1000) |
53 | #include <docobj.h> | |
bbf1f0e5 KB |
54 | #endif |
55 | ||
56 | // ============================================================================ | |
57 | // Implementation | |
58 | // ============================================================================ | |
59 | ||
0a0e6a5b | 60 | // return true if the iid is in the array |
bbf1f0e5 KB |
61 | 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] ) | |
0a0e6a5b | 65 | return true; |
bbf1f0e5 KB |
66 | } |
67 | ||
0a0e6a5b | 68 | return false; |
bbf1f0e5 KB |
69 | } |
70 | ||
e2898549 | 71 | WXDLLEXPORT BSTR wxConvertStringToOle(const wxString& str) |
94113cc5 | 72 | { |
c45ead7b | 73 | return wxBasicString(str).Get(); |
94113cc5 JS |
74 | } |
75 | ||
e2898549 | 76 | WXDLLEXPORT wxString wxConvertStringFromOle(BSTR bStr) |
94113cc5 | 77 | { |
795b0f14 VZ |
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 | ||
59af032a VZ |
83 | const int len = SysStringLen(bStr); |
84 | ||
94113cc5 | 85 | #if wxUSE_UNICODE |
59af032a | 86 | wxString str(bStr, len); |
94113cc5 | 87 | #else |
bba85010 | 88 | wxString str; |
9968f7b4 | 89 | if (len) |
bba85010 | 90 | { |
9968f7b4 VZ |
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 */)); | |
bba85010 | 96 | } |
94113cc5 | 97 | #endif |
59af032a | 98 | |
94113cc5 JS |
99 | return str; |
100 | } | |
101 | ||
102 | // ---------------------------------------------------------------------------- | |
103 | // wxBasicString | |
104 | // ---------------------------------------------------------------------------- | |
105 | ||
a518e508 | 106 | wxBasicString::wxBasicString(const wxString& str) |
94113cc5 | 107 | { |
a518e508 | 108 | m_bstrBuf = SysAllocString(str.wc_str(*wxConvCurrent)); |
94113cc5 JS |
109 | } |
110 | ||
a518e508 | 111 | wxBasicString::wxBasicString(const wxBasicString& src) |
94113cc5 | 112 | { |
a518e508 | 113 | m_bstrBuf = src.Get(); |
94113cc5 JS |
114 | } |
115 | ||
a518e508 | 116 | wxBasicString& wxBasicString::operator=(const wxBasicString& src) |
94113cc5 | 117 | { |
a518e508 VZ |
118 | SysReAllocString(&m_bstrBuf, src); |
119 | return *this; | |
94113cc5 JS |
120 | } |
121 | ||
94113cc5 JS |
122 | wxBasicString::~wxBasicString() |
123 | { | |
a518e508 | 124 | SysFreeString(m_bstrBuf); |
94113cc5 JS |
125 | } |
126 | ||
a2fd865b VS |
127 | |
128 | // ---------------------------------------------------------------------------- | |
129 | // Convert variants | |
130 | // ---------------------------------------------------------------------------- | |
131 | ||
132 | #if wxUSE_VARIANT | |
133 | ||
a2fd865b VS |
134 | WXDLLEXPORT bool wxConvertVariantToOle(const wxVariant& variant, VARIANTARG& oleVariant) |
135 | { | |
1fbfbfb0 | 136 | VariantInit(&oleVariant); |
a2fd865b VS |
137 | if (variant.IsNull()) |
138 | { | |
139 | oleVariant.vt = VT_NULL; | |
140 | return true; | |
141 | } | |
142 | ||
143 | wxString type(variant.GetType()); | |
144 | ||
145 | ||
146 | if (type == wxT("long")) | |
147 | { | |
148 | oleVariant.vt = VT_I4; | |
149 | oleVariant.lVal = variant.GetLong() ; | |
150 | } | |
a2fd865b VS |
151 | else if (type == wxT("char")) |
152 | { | |
153 | oleVariant.vt=VT_I1; // Signed Char | |
154 | oleVariant.cVal=variant.GetChar(); | |
155 | } | |
a2fd865b VS |
156 | else if (type == wxT("double")) |
157 | { | |
158 | oleVariant.vt = VT_R8; | |
159 | oleVariant.dblVal = variant.GetDouble(); | |
160 | } | |
161 | else if (type == wxT("bool")) | |
162 | { | |
163 | oleVariant.vt = VT_BOOL; | |
85ecb1d5 | 164 | oleVariant.boolVal = variant.GetBool() ? VARIANT_TRUE : VARIANT_FALSE; |
a2fd865b VS |
165 | } |
166 | else if (type == wxT("string")) | |
167 | { | |
168 | wxString str( variant.GetString() ); | |
169 | oleVariant.vt = VT_BSTR; | |
170 | oleVariant.bstrVal = wxConvertStringToOle(str); | |
171 | } | |
172 | #if wxUSE_DATETIME | |
173 | else if (type == wxT("datetime")) | |
174 | { | |
175 | wxDateTime date( variant.GetDateTime() ); | |
176 | oleVariant.vt = VT_DATE; | |
177 | ||
178 | SYSTEMTIME st; | |
179 | date.GetAsMSWSysTime(&st); | |
180 | ||
181 | SystemTimeToVariantTime(&st, &oleVariant.date); | |
182 | } | |
183 | #endif | |
184 | else if (type == wxT("void*")) | |
185 | { | |
186 | oleVariant.vt = VT_DISPATCH; | |
187 | oleVariant.pdispVal = (IDispatch*) variant.GetVoidPtr(); | |
188 | } | |
189 | else if (type == wxT("list") || type == wxT("stringlist")) | |
190 | { | |
191 | oleVariant.vt = VT_VARIANT | VT_ARRAY; | |
192 | ||
193 | SAFEARRAY *psa; | |
194 | SAFEARRAYBOUND saBound; | |
195 | VARIANTARG *pvargBase; | |
196 | VARIANTARG *pvarg; | |
197 | int i, j; | |
198 | ||
199 | int iCount = variant.GetCount(); | |
200 | ||
201 | saBound.lLbound = 0; | |
202 | saBound.cElements = iCount; | |
203 | ||
204 | psa = SafeArrayCreate(VT_VARIANT, 1, &saBound); | |
205 | if (psa == NULL) | |
206 | return false; | |
207 | ||
208 | SafeArrayAccessData(psa, (void**)&pvargBase); | |
209 | ||
210 | pvarg = pvargBase; | |
211 | for (i = 0; i < iCount; i++) | |
212 | { | |
213 | // copy each string in the list of strings | |
214 | wxVariant eachVariant(variant[i]); | |
215 | if (!wxConvertVariantToOle(eachVariant, * pvarg)) | |
216 | { | |
217 | // memory failure: back out and free strings alloc'ed up to | |
218 | // now, and then the array itself. | |
219 | pvarg = pvargBase; | |
220 | for (j = 0; j < i; j++) | |
221 | { | |
222 | SysFreeString(pvarg->bstrVal); | |
223 | pvarg++; | |
224 | } | |
225 | SafeArrayDestroy(psa); | |
226 | return false; | |
227 | } | |
228 | pvarg++; | |
229 | } | |
230 | ||
231 | SafeArrayUnaccessData(psa); | |
232 | ||
233 | oleVariant.parray = psa; | |
234 | } | |
235 | else | |
236 | { | |
237 | oleVariant.vt = VT_NULL; | |
238 | return false; | |
239 | } | |
240 | return true; | |
241 | } | |
242 | ||
243 | #ifndef VT_TYPEMASK | |
244 | #define VT_TYPEMASK 0xfff | |
245 | #endif | |
246 | ||
247 | WXDLLEXPORT bool | |
248 | wxConvertOleToVariant(const VARIANTARG& oleVariant, wxVariant& variant) | |
249 | { | |
250 | bool ok = true; | |
251 | if ( oleVariant.vt & VT_ARRAY ) | |
252 | { | |
253 | ||
254 | // Compute the total number of elements in all array dimensions | |
255 | int cElements = 1; | |
256 | for ( int cDims = 0; cDims < oleVariant.parray->cDims; cDims++ ) | |
257 | cElements *= oleVariant.parray->rgsabound[cDims].cElements; | |
258 | ||
259 | // Get a pointer to the data | |
260 | void* pvdata; | |
261 | HRESULT hr = SafeArrayAccessData(oleVariant.parray, &pvdata); | |
262 | if ( FAILED(hr) ) | |
263 | return false; | |
264 | ||
265 | switch (oleVariant.vt & VT_TYPEMASK) | |
266 | { | |
267 | case VT_VARIANT: | |
268 | { | |
269 | variant.ClearList(); | |
270 | VARIANTARG *variant_data=(VARIANTARG*)pvdata; | |
271 | for ( int i = 0; i < cElements; i++ ) | |
272 | { | |
273 | VARIANTARG& oleElement = variant_data[i]; | |
274 | wxVariant vElement; | |
275 | if ( !wxConvertOleToVariant(oleElement, vElement) ) | |
276 | { | |
277 | ok = false; | |
278 | variant.ClearList(); | |
279 | break; | |
280 | } | |
281 | ||
282 | variant.Append(vElement); | |
283 | } | |
284 | } | |
285 | break; | |
286 | ||
287 | case VT_BSTR: | |
288 | { | |
289 | wxArrayString strings; | |
290 | BSTR *string_val=(BSTR*)pvdata; | |
291 | for ( int i = 0; i < cElements; ++i ) | |
292 | { | |
293 | wxString str=wxConvertStringFromOle(*string_val); | |
294 | strings.Add(str); | |
295 | ++string_val; | |
296 | } | |
297 | variant=strings; | |
298 | } | |
299 | break; | |
300 | ||
301 | default: | |
302 | wxLogDebug(wxT("unhandled VT_ARRAY type %x in wxConvertOleToVariant"), | |
303 | oleVariant.vt & VT_TYPEMASK); | |
304 | variant = wxVariant(); | |
305 | ok = false; | |
306 | break; | |
307 | } | |
308 | ||
309 | SafeArrayUnaccessData(oleVariant.parray); | |
310 | } | |
311 | else if ( oleVariant.vt & VT_BYREF ) | |
312 | { | |
313 | switch ( oleVariant.vt & VT_TYPEMASK ) | |
314 | { | |
315 | case VT_VARIANT: | |
316 | { | |
317 | VARIANTARG& oleReference = *((LPVARIANT)oleVariant.byref); | |
318 | if (!wxConvertOleToVariant(oleReference,variant)) | |
319 | return false; | |
320 | break; | |
321 | } | |
322 | ||
323 | default: | |
324 | wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: [as yet] unhandled reference %X"), | |
325 | oleVariant.vt); | |
326 | return false; | |
327 | } | |
328 | } | |
329 | else // simply type (not array or reference) | |
330 | { | |
331 | switch ( oleVariant.vt & VT_TYPEMASK ) | |
332 | { | |
333 | case VT_BSTR: | |
334 | { | |
335 | wxString str(wxConvertStringFromOle(oleVariant.bstrVal)); | |
336 | variant = str; | |
337 | } | |
338 | break; | |
339 | ||
340 | case VT_DATE: | |
341 | #if wxUSE_DATETIME | |
342 | { | |
343 | SYSTEMTIME st; | |
344 | VariantTimeToSystemTime(oleVariant.date, &st); | |
345 | ||
346 | wxDateTime date; | |
347 | date.SetFromMSWSysTime(st); | |
348 | variant = date; | |
349 | } | |
350 | #endif // wxUSE_DATETIME | |
351 | break; | |
352 | ||
353 | case VT_I4: | |
354 | variant = (long) oleVariant.lVal; | |
355 | break; | |
356 | ||
357 | case VT_I2: | |
358 | variant = (long) oleVariant.iVal; | |
359 | break; | |
360 | ||
361 | case VT_BOOL: | |
362 | variant = oleVariant.boolVal != 0; | |
363 | break; | |
364 | ||
51909b8b VZ |
365 | case VT_R4: |
366 | variant = oleVariant.fltVal; | |
367 | break; | |
368 | ||
a2fd865b VS |
369 | case VT_R8: |
370 | variant = oleVariant.dblVal; | |
371 | break; | |
372 | ||
373 | case VT_DISPATCH: | |
374 | variant = (void*) oleVariant.pdispVal; | |
375 | break; | |
376 | ||
377 | case VT_NULL: | |
378 | variant.MakeNull(); | |
379 | break; | |
380 | ||
381 | case VT_EMPTY: | |
382 | break; // Ignore Empty Variant, used only during destruction of objects | |
383 | ||
384 | default: | |
385 | wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type %X -> %X"), | |
386 | oleVariant.vt,oleVariant.vt&VT_TYPEMASK); | |
387 | return false; | |
388 | } | |
389 | } | |
390 | ||
391 | return ok; | |
392 | } | |
393 | ||
394 | #endif // wxUSE_VARIANT | |
395 | ||
c740f496 | 396 | |
bbf1f0e5 KB |
397 | // ---------------------------------------------------------------------------- |
398 | // Debug support | |
399 | // ---------------------------------------------------------------------------- | |
400 | ||
a2fd865b VS |
401 | #if wxUSE_DATAOBJ |
402 | ||
4b6a582b | 403 | #if wxDEBUG_LEVEL && ( ( defined(__VISUALC__) && (__VISUALC__ > 1000) ) || defined(__MWERKS__) ) |
f6bcfd97 | 404 | static wxString GetIidName(REFIID riid) |
bbf1f0e5 KB |
405 | { |
406 | // an association between symbolic name and numeric value of an IID | |
407 | struct KNOWN_IID { | |
408 | const IID *pIid; | |
f6bcfd97 | 409 | const wxChar *szName; |
bbf1f0e5 KB |
410 | }; |
411 | ||
412 | // construct the table containing all known interfaces | |
9a83f860 | 413 | #define ADD_KNOWN_IID(name) { &IID_I##name, wxT(#name) } |
bbf1f0e5 KB |
414 | |
415 | static const KNOWN_IID aKnownIids[] = { | |
416 | ADD_KNOWN_IID(AdviseSink), | |
417 | ADD_KNOWN_IID(AdviseSink2), | |
418 | ADD_KNOWN_IID(BindCtx), | |
419 | ADD_KNOWN_IID(ClassFactory), | |
ba14d986 | 420 | #if ( !defined( __VISUALC__) || (__VISUALC__!=1010) ) && !defined(__MWERKS__) |
bbf1f0e5 KB |
421 | ADD_KNOWN_IID(ContinueCallback), |
422 | ADD_KNOWN_IID(EnumOleDocumentViews), | |
423 | ADD_KNOWN_IID(OleCommandTarget), | |
424 | ADD_KNOWN_IID(OleDocument), | |
425 | ADD_KNOWN_IID(OleDocumentSite), | |
426 | ADD_KNOWN_IID(OleDocumentView), | |
427 | ADD_KNOWN_IID(Print), | |
27a9bd48 | 428 | #endif |
bbf1f0e5 KB |
429 | ADD_KNOWN_IID(DataAdviseHolder), |
430 | ADD_KNOWN_IID(DataObject), | |
431 | ADD_KNOWN_IID(Debug), | |
432 | ADD_KNOWN_IID(DebugStream), | |
433 | ADD_KNOWN_IID(DfReserved1), | |
434 | ADD_KNOWN_IID(DfReserved2), | |
435 | ADD_KNOWN_IID(DfReserved3), | |
436 | ADD_KNOWN_IID(Dispatch), | |
437 | ADD_KNOWN_IID(DropSource), | |
438 | ADD_KNOWN_IID(DropTarget), | |
439 | ADD_KNOWN_IID(EnumCallback), | |
440 | ADD_KNOWN_IID(EnumFORMATETC), | |
441 | ADD_KNOWN_IID(EnumGeneric), | |
442 | ADD_KNOWN_IID(EnumHolder), | |
443 | ADD_KNOWN_IID(EnumMoniker), | |
444 | ADD_KNOWN_IID(EnumOLEVERB), | |
445 | ADD_KNOWN_IID(EnumSTATDATA), | |
446 | ADD_KNOWN_IID(EnumSTATSTG), | |
447 | ADD_KNOWN_IID(EnumString), | |
448 | ADD_KNOWN_IID(EnumUnknown), | |
449 | ADD_KNOWN_IID(EnumVARIANT), | |
450 | ADD_KNOWN_IID(ExternalConnection), | |
451 | ADD_KNOWN_IID(InternalMoniker), | |
452 | ADD_KNOWN_IID(LockBytes), | |
453 | ADD_KNOWN_IID(Malloc), | |
454 | ADD_KNOWN_IID(Marshal), | |
455 | ADD_KNOWN_IID(MessageFilter), | |
456 | ADD_KNOWN_IID(Moniker), | |
457 | ADD_KNOWN_IID(OleAdviseHolder), | |
458 | ADD_KNOWN_IID(OleCache), | |
459 | ADD_KNOWN_IID(OleCache2), | |
460 | ADD_KNOWN_IID(OleCacheControl), | |
461 | ADD_KNOWN_IID(OleClientSite), | |
462 | ADD_KNOWN_IID(OleContainer), | |
463 | ADD_KNOWN_IID(OleInPlaceActiveObject), | |
464 | ADD_KNOWN_IID(OleInPlaceFrame), | |
465 | ADD_KNOWN_IID(OleInPlaceObject), | |
466 | ADD_KNOWN_IID(OleInPlaceSite), | |
467 | ADD_KNOWN_IID(OleInPlaceUIWindow), | |
468 | ADD_KNOWN_IID(OleItemContainer), | |
469 | ADD_KNOWN_IID(OleLink), | |
470 | ADD_KNOWN_IID(OleManager), | |
471 | ADD_KNOWN_IID(OleObject), | |
472 | ADD_KNOWN_IID(OlePresObj), | |
473 | ADD_KNOWN_IID(OleWindow), | |
474 | ADD_KNOWN_IID(PSFactory), | |
475 | ADD_KNOWN_IID(ParseDisplayName), | |
476 | ADD_KNOWN_IID(Persist), | |
477 | ADD_KNOWN_IID(PersistFile), | |
478 | ADD_KNOWN_IID(PersistStorage), | |
479 | ADD_KNOWN_IID(PersistStream), | |
480 | ADD_KNOWN_IID(ProxyManager), | |
481 | ADD_KNOWN_IID(RootStorage), | |
482 | ADD_KNOWN_IID(RpcChannel), | |
483 | ADD_KNOWN_IID(RpcProxy), | |
484 | ADD_KNOWN_IID(RpcStub), | |
485 | ADD_KNOWN_IID(RunnableObject), | |
486 | ADD_KNOWN_IID(RunningObjectTable), | |
487 | ADD_KNOWN_IID(StdMarshalInfo), | |
488 | ADD_KNOWN_IID(Storage), | |
489 | ADD_KNOWN_IID(Stream), | |
490 | ADD_KNOWN_IID(StubManager), | |
491 | ADD_KNOWN_IID(Unknown), | |
492 | ADD_KNOWN_IID(ViewObject), | |
493 | ADD_KNOWN_IID(ViewObject2), | |
494 | }; | |
495 | ||
496 | // don't clobber preprocessor name space | |
497 | #undef ADD_KNOWN_IID | |
498 | ||
499 | // try to find the interface in the table | |
c86f1403 | 500 | for ( size_t ui = 0; ui < WXSIZEOF(aKnownIids); ui++ ) { |
bbf1f0e5 KB |
501 | if ( riid == *aKnownIids[ui].pIid ) { |
502 | return aKnownIids[ui].szName; | |
503 | } | |
504 | } | |
505 | ||
4676948b | 506 | #ifndef __WXWINCE__ |
bbf1f0e5 | 507 | // unknown IID, just transform to string |
f6bcfd97 BP |
508 | Uuid uuid(riid); |
509 | return wxString((const wxChar *)uuid); | |
4676948b JS |
510 | #else |
511 | return wxEmptyString; | |
512 | #endif | |
bbf1f0e5 KB |
513 | } |
514 | ||
f6bcfd97 | 515 | void wxLogQueryInterface(const wxChar *szInterface, REFIID riid) |
bbf1f0e5 | 516 | { |
ba14d986 | 517 | wxLogTrace(wxTRACE_OleCalls, wxT("%s::QueryInterface (iid = %s)"), |
f6bcfd97 | 518 | szInterface, GetIidName(riid).c_str()); |
bbf1f0e5 KB |
519 | } |
520 | ||
f6bcfd97 | 521 | void wxLogAddRef(const wxChar *szInterface, ULONG cRef) |
bbf1f0e5 | 522 | { |
ba14d986 | 523 | wxLogTrace(wxTRACE_OleCalls, wxT("After %s::AddRef: m_cRef = %d"), szInterface, cRef + 1); |
bbf1f0e5 KB |
524 | } |
525 | ||
f6bcfd97 | 526 | void wxLogRelease(const wxChar *szInterface, ULONG cRef) |
bbf1f0e5 | 527 | { |
ba14d986 | 528 | wxLogTrace(wxTRACE_OleCalls, wxT("After %s::Release: m_cRef = %d"), szInterface, cRef - 1); |
bbf1f0e5 KB |
529 | } |
530 | ||
4b6a582b | 531 | #endif // wxDEBUG_LEVEL |
aeab10d0 | 532 | |
a2fd865b | 533 | #endif // wxUSE_DATAOBJ |
aeab10d0 | 534 | |
4b6a582b | 535 | #endif // __CYGWIN10__ |
aeab10d0 | 536 | |
4b6a582b | 537 | #endif // wxUSE_OLE |