]> git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/automtn.cpp
supporting a null cgimage so that we don't get exceptions, only an wxbitmap that...
[wxWidgets.git] / src / msw / ole / automtn.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ole/automtn.cpp
3 // Purpose: OLE automation utilities
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 11/6/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998, Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
14
15 #if defined(__BORLANDC__)
16 #pragma hdrstop
17 #endif
18
19 // With Borland C++, all samples crash if this is compiled in.
20 #if (defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) || defined(__CYGWIN10__)
21 #undef wxUSE_OLE_AUTOMATION
22 #define wxUSE_OLE_AUTOMATION 0
23 #endif
24
25 #ifndef WX_PRECOMP
26 #include "wx/log.h"
27 #include "wx/math.h"
28 #endif
29
30 #define _FORCENAMELESSUNION
31 #include "wx/msw/private.h"
32 #include "wx/msw/ole/oleutils.h"
33 #include "wx/msw/ole/automtn.h"
34
35 #ifdef __WXWINCE__
36 #include "wx/msw/wince/time.h"
37 #else
38 #include <time.h>
39 #endif
40
41 #include <wtypes.h>
42 #include <unknwn.h>
43
44 #include <ole2.h>
45 #define _huge
46
47 #ifndef __WXWINCE__
48 #include <ole2ver.h>
49 #endif
50
51 #include <oleauto.h>
52
53 #if wxUSE_DATETIME
54 #include "wx/datetime.h"
55 #endif // wxUSE_DATETIME
56
57 #if wxUSE_OLE_AUTOMATION
58
59 // Report an OLE error when calling the specified method to the user via wxLog.
60 static void
61 ShowException(const wxString& member,
62 HRESULT hr,
63 EXCEPINFO *pexcep = NULL,
64 unsigned int uiArgErr = 0);
65
66 // wxAutomationObject
67
68 wxAutomationObject::wxAutomationObject(WXIDISPATCH* dispatchPtr)
69 {
70 m_dispatchPtr = dispatchPtr;
71 }
72
73 wxAutomationObject::~wxAutomationObject()
74 {
75 if (m_dispatchPtr)
76 {
77 ((IDispatch*)m_dispatchPtr)->Release();
78 m_dispatchPtr = NULL;
79 }
80 }
81
82 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
83
84 // For Put/Get, no named arguments are allowed.
85 bool wxAutomationObject::Invoke(const wxString& member, int action,
86 wxVariant& retValue, int noArgs, wxVariant args[], const wxVariant* ptrArgs[]) const
87 {
88 if (!m_dispatchPtr)
89 return false;
90
91 int ch = member.Find('.');
92 if (ch != -1)
93 {
94 // Use dot notation to get the next object
95 wxString member2(member.Left((size_t) ch));
96 wxString rest(member.Right(member.length() - ch - 1));
97 wxAutomationObject obj;
98 if (!GetObject(obj, member2))
99 return false;
100 return obj.Invoke(rest, action, retValue, noArgs, args, ptrArgs);
101 }
102
103 VARIANTARG vReturn;
104 VariantInit(& vReturn);
105
106 VARIANTARG* vReturnPtr = & vReturn;
107
108 // Find number of names args
109 int namedArgCount = 0;
110 int i;
111 for (i = 0; i < noArgs; i++)
112 if ( !INVOKEARG(i).GetName().empty() )
113 {
114 namedArgCount ++;
115 }
116
117 int namedArgStringCount = namedArgCount + 1;
118 BSTR* argNames = new BSTR[namedArgStringCount];
119 argNames[0] = wxConvertStringToOle(member);
120
121 // Note that arguments are specified in reverse order
122 // (all totally logical; hey, we're dealing with OLE here.)
123
124 int j = 0;
125 for (i = 0; i < namedArgCount; i++)
126 {
127 if ( !INVOKEARG(i).GetName().empty() )
128 {
129 argNames[(namedArgCount-j)] = wxConvertStringToOle(INVOKEARG(i).GetName());
130 j ++;
131 }
132 }
133
134 // + 1 for the member name, + 1 again in case we're a 'put'
135 DISPID* dispIds = new DISPID[namedArgCount + 2];
136
137 HRESULT hr;
138 DISPPARAMS dispparams;
139 unsigned int uiArgErr;
140
141 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
142 // member name as the first name, followed by argument names (if any).
143 hr = ((IDispatch*)m_dispatchPtr)->GetIDsOfNames(IID_NULL, argNames,
144 1 + namedArgCount, LOCALE_SYSTEM_DEFAULT, dispIds);
145 if (FAILED(hr))
146 {
147 ShowException(member, hr);
148 delete[] argNames;
149 delete[] dispIds;
150 return false;
151 }
152
153 // if doing a property put(ref), we need to adjust the first argument to have a
154 // named arg of DISPID_PROPERTYPUT.
155 if (action & (DISPATCH_PROPERTYPUT | DISPATCH_PROPERTYPUTREF))
156 {
157 namedArgCount = 1;
158 dispIds[1] = DISPID_PROPERTYPUT;
159 vReturnPtr = NULL;
160 }
161
162 // Convert the wxVariants to VARIANTARGs
163 VARIANTARG* oleArgs = new VARIANTARG[noArgs];
164 for (i = 0; i < noArgs; i++)
165 {
166 // Again, reverse args
167 if (!wxConvertVariantToOle(INVOKEARG((noArgs-1) - i), oleArgs[i]))
168 {
169 delete[] argNames;
170 delete[] dispIds;
171 delete[] oleArgs;
172 return false;
173 }
174 }
175
176 dispparams.rgdispidNamedArgs = dispIds + 1;
177 dispparams.rgvarg = oleArgs;
178 dispparams.cArgs = noArgs;
179 dispparams.cNamedArgs = namedArgCount;
180
181 EXCEPINFO excep;
182 wxZeroMemory(excep);
183
184 hr = ((IDispatch*)m_dispatchPtr)->Invoke(dispIds[0], IID_NULL, LOCALE_SYSTEM_DEFAULT,
185 (WORD)action, &dispparams, vReturnPtr, &excep, &uiArgErr);
186
187 for (i = 0; i < namedArgStringCount; i++)
188 {
189 SysFreeString(argNames[i]);
190 }
191 delete[] argNames;
192 delete[] dispIds;
193
194 for (i = 0; i < noArgs; i++)
195 VariantClear(& oleArgs[i]) ;
196 delete[] oleArgs;
197
198 if (FAILED(hr))
199 {
200 // display the exception information if appropriate:
201 ShowException(member, hr, &excep, uiArgErr);
202
203 // free exception structure information
204 SysFreeString(excep.bstrSource);
205 SysFreeString(excep.bstrDescription);
206 SysFreeString(excep.bstrHelpFile);
207
208 if (vReturnPtr)
209 VariantClear(vReturnPtr);
210 return false;
211 }
212 else
213 {
214 if (vReturnPtr)
215 {
216 // Convert result to wxVariant form
217 wxConvertOleToVariant(vReturn, retValue);
218 // Mustn't release the dispatch pointer
219 if (vReturn.vt == VT_DISPATCH)
220 {
221 vReturn.pdispVal = NULL;
222 }
223 VariantClear(& vReturn);
224 }
225 }
226 return true;
227 }
228
229 // Invoke a member function
230 wxVariant wxAutomationObject::CallMethod(const wxString& member, int noArgs, wxVariant args[])
231 {
232 wxVariant retVariant;
233 if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, args))
234 {
235 retVariant.MakeNull();
236 }
237 return retVariant;
238 }
239
240 wxVariant wxAutomationObject::CallMethodArray(const wxString& member, int noArgs, const wxVariant **args)
241 {
242 wxVariant retVariant;
243 if (!Invoke(member, DISPATCH_METHOD, retVariant, noArgs, NULL, args))
244 {
245 retVariant.MakeNull();
246 }
247 return retVariant;
248 }
249
250 wxVariant wxAutomationObject::CallMethod(const wxString& member,
251 const wxVariant& arg1, const wxVariant& arg2,
252 const wxVariant& arg3, const wxVariant& arg4,
253 const wxVariant& arg5, const wxVariant& arg6)
254 {
255 const wxVariant** args = new const wxVariant*[6];
256 int i = 0;
257 if (!arg1.IsNull())
258 {
259 args[i] = & arg1;
260 i ++;
261 }
262 if (!arg2.IsNull())
263 {
264 args[i] = & arg2;
265 i ++;
266 }
267 if (!arg3.IsNull())
268 {
269 args[i] = & arg3;
270 i ++;
271 }
272 if (!arg4.IsNull())
273 {
274 args[i] = & arg4;
275 i ++;
276 }
277 if (!arg5.IsNull())
278 {
279 args[i] = & arg5;
280 i ++;
281 }
282 if (!arg6.IsNull())
283 {
284 args[i] = & arg6;
285 i ++;
286 }
287 wxVariant retVariant;
288 if (!Invoke(member, DISPATCH_METHOD, retVariant, i, NULL, args))
289 {
290 retVariant.MakeNull();
291 }
292 delete[] args;
293 return retVariant;
294 }
295
296 // Get/Set property
297 wxVariant wxAutomationObject::GetPropertyArray(const wxString& property, int noArgs, const wxVariant **args) const
298 {
299 wxVariant retVariant;
300 if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args))
301 {
302 retVariant.MakeNull();
303 }
304 return retVariant;
305 }
306 wxVariant wxAutomationObject::GetProperty(const wxString& property, int noArgs, wxVariant args[]) const
307 {
308 wxVariant retVariant;
309 if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args))
310 {
311 retVariant.MakeNull();
312 }
313 return retVariant;
314 }
315
316 wxVariant wxAutomationObject::GetProperty(const wxString& property,
317 const wxVariant& arg1, const wxVariant& arg2,
318 const wxVariant& arg3, const wxVariant& arg4,
319 const wxVariant& arg5, const wxVariant& arg6)
320 {
321 const wxVariant** args = new const wxVariant*[6];
322 int i = 0;
323 if (!arg1.IsNull())
324 {
325 args[i] = & arg1;
326 i ++;
327 }
328 if (!arg2.IsNull())
329 {
330 args[i] = & arg2;
331 i ++;
332 }
333 if (!arg3.IsNull())
334 {
335 args[i] = & arg3;
336 i ++;
337 }
338 if (!arg4.IsNull())
339 {
340 args[i] = & arg4;
341 i ++;
342 }
343 if (!arg5.IsNull())
344 {
345 args[i] = & arg5;
346 i ++;
347 }
348 if (!arg6.IsNull())
349 {
350 args[i] = & arg6;
351 i ++;
352 }
353 wxVariant retVariant;
354 if (!Invoke(property, DISPATCH_PROPERTYGET, retVariant, i, NULL, args))
355 {
356 retVariant.MakeNull();
357 }
358 delete[] args;
359 return retVariant;
360 }
361
362 bool wxAutomationObject::PutProperty(const wxString& property, int noArgs, wxVariant args[])
363 {
364 wxVariant retVariant;
365 if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, args))
366 {
367 return false;
368 }
369 return true;
370 }
371
372 bool wxAutomationObject::PutPropertyArray(const wxString& property, int noArgs, const wxVariant **args)
373 {
374 wxVariant retVariant;
375 if (!Invoke(property, DISPATCH_PROPERTYPUT, retVariant, noArgs, NULL, args))
376 {
377 return false;
378 }
379 return true;
380 }
381
382 bool wxAutomationObject::PutProperty(const wxString& property,
383 const wxVariant& arg1, const wxVariant& arg2,
384 const wxVariant& arg3, const wxVariant& arg4,
385 const wxVariant& arg5, const wxVariant& arg6)
386 {
387 const wxVariant** args = new const wxVariant*[6];
388 int i = 0;
389 if (!arg1.IsNull())
390 {
391 args[i] = & arg1;
392 i ++;
393 }
394 if (!arg2.IsNull())
395 {
396 args[i] = & arg2;
397 i ++;
398 }
399 if (!arg3.IsNull())
400 {
401 args[i] = & arg3;
402 i ++;
403 }
404 if (!arg4.IsNull())
405 {
406 args[i] = & arg4;
407 i ++;
408 }
409 if (!arg5.IsNull())
410 {
411 args[i] = & arg5;
412 i ++;
413 }
414 if (!arg6.IsNull())
415 {
416 args[i] = & arg6;
417 i ++;
418 }
419 wxVariant retVariant;
420 bool ret = Invoke(property, DISPATCH_PROPERTYPUT, retVariant, i, NULL, args);
421 delete[] args;
422 return ret;
423 }
424
425
426 // Uses DISPATCH_PROPERTYGET
427 // and returns a dispatch pointer. The calling code should call Release
428 // on the pointer, though this could be implicit by constructing an wxAutomationObject
429 // with it and letting the destructor call Release.
430 WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, wxVariant args[]) const
431 {
432 wxVariant retVariant;
433 if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, args))
434 {
435 if (retVariant.GetType() == wxT("void*"))
436 {
437 return (WXIDISPATCH*) retVariant.GetVoidPtr();
438 }
439 }
440
441 return NULL;
442 }
443
444 // Uses DISPATCH_PROPERTYGET
445 // and returns a dispatch pointer. The calling code should call Release
446 // on the pointer, though this could be implicit by constructing an wxAutomationObject
447 // with it and letting the destructor call Release.
448 WXIDISPATCH* wxAutomationObject::GetDispatchProperty(const wxString& property, int noArgs, const wxVariant **args) const
449 {
450 wxVariant retVariant;
451 if (Invoke(property, DISPATCH_PROPERTYGET, retVariant, noArgs, NULL, args))
452 {
453 if (retVariant.GetType() == wxT("void*"))
454 {
455 return (WXIDISPATCH*) retVariant.GetVoidPtr();
456 }
457 }
458
459 return NULL;
460 }
461
462
463 // A way of initialising another wxAutomationObject with a dispatch object
464 bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, wxVariant args[]) const
465 {
466 WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args);
467 if (dispatch)
468 {
469 obj.SetDispatchPtr(dispatch);
470 return true;
471 }
472 else
473 return false;
474 }
475
476 // A way of initialising another wxAutomationObject with a dispatch object
477 bool wxAutomationObject::GetObject(wxAutomationObject& obj, const wxString& property, int noArgs, const wxVariant **args) const
478 {
479 WXIDISPATCH* dispatch = GetDispatchProperty(property, noArgs, args);
480 if (dispatch)
481 {
482 obj.SetDispatchPtr(dispatch);
483 return true;
484 }
485 else
486 return false;
487 }
488
489 namespace
490 {
491
492 HRESULT wxCLSIDFromProgID(const wxString& progId, CLSID& clsId)
493 {
494 HRESULT hr = CLSIDFromProgID(wxBasicString(progId), &clsId);
495 if ( FAILED(hr) )
496 {
497 wxLogSysError(hr, _("Failed to find CLSID of \"%s\""), progId);
498 }
499 return hr;
500 }
501
502 void *DoCreateInstance(const wxString& progId, const CLSID& clsId)
503 {
504 // get the server IDispatch interface
505 //
506 // NB: using CLSCTX_INPROC_HANDLER results in failure when getting
507 // Automation interface for Microsoft Office applications so don't use
508 // CLSCTX_ALL which includes it
509 void *pDispatch = NULL;
510 HRESULT hr = CoCreateInstance(clsId, NULL, CLSCTX_SERVER,
511 IID_IDispatch, &pDispatch);
512 if (FAILED(hr))
513 {
514 wxLogSysError(hr, _("Failed to create an instance of \"%s\""), progId);
515 return NULL;
516 }
517
518 return pDispatch;
519 }
520
521 } // anonymous namespace
522
523 // Get a dispatch pointer from the current object associated
524 // with a ProgID
525 bool wxAutomationObject::GetInstance(const wxString& progId, int flags) const
526 {
527 if (m_dispatchPtr)
528 return false;
529
530 CLSID clsId;
531 HRESULT hr = wxCLSIDFromProgID(progId, clsId);
532 if (FAILED(hr))
533 return false;
534
535 IUnknown *pUnk = NULL;
536 hr = GetActiveObject(clsId, NULL, &pUnk);
537 if (FAILED(hr))
538 {
539 if ( flags & wxAutomationInstance_CreateIfNeeded )
540 {
541 const_cast<wxAutomationObject *>(this)->
542 m_dispatchPtr = DoCreateInstance(progId, clsId);
543 if ( m_dispatchPtr )
544 return true;
545 }
546 else
547 {
548 wxLogSysError(hr,
549 _("Cannot get an active instance of \"%s\""), progId);
550 }
551
552 return false;
553 }
554
555 hr = pUnk->QueryInterface(IID_IDispatch, (LPVOID*) &m_dispatchPtr);
556 if (FAILED(hr))
557 {
558 wxLogSysError(hr,
559 _("Failed to get OLE automation interface for \"%s\""),
560 progId);
561 return false;
562 }
563
564 return true;
565 }
566
567 // Get a dispatch pointer from a new object associated
568 // with the given ProgID
569 bool wxAutomationObject::CreateInstance(const wxString& progId) const
570 {
571 if (m_dispatchPtr)
572 return false;
573
574 CLSID clsId;
575 HRESULT hr = wxCLSIDFromProgID(progId, clsId);
576 if (FAILED(hr))
577 return false;
578
579 const_cast<wxAutomationObject *>(this)->
580 m_dispatchPtr = DoCreateInstance(progId, clsId);
581
582 return m_dispatchPtr != NULL;
583 }
584
585 static void
586 ShowException(const wxString& member,
587 HRESULT hr,
588 EXCEPINFO *pexcep,
589 unsigned int uiArgErr)
590 {
591 wxString message;
592 switch (GetScode(hr))
593 {
594 case DISP_E_UNKNOWNNAME:
595 message = _("Unknown name or named argument.");
596 break;
597
598 case DISP_E_BADPARAMCOUNT:
599 message = _("Incorrect number of arguments.");
600 break;
601
602 case DISP_E_EXCEPTION:
603 if ( pexcep )
604 {
605 if ( pexcep->bstrDescription )
606 message << pexcep->bstrDescription << wxS(" ");
607 message += wxString::Format(wxS("error code %u"), pexcep->wCode);
608 }
609 else
610 {
611 message = _("Unknown exception");
612 }
613 break;
614
615 case DISP_E_MEMBERNOTFOUND:
616 message = _("Method or property not found.");
617 break;
618
619 case DISP_E_OVERFLOW:
620 message = _("Overflow while coercing argument values.");
621 break;
622
623 case DISP_E_NONAMEDARGS:
624 message = _("Object implementation does not support named arguments.");
625 break;
626
627 case DISP_E_UNKNOWNLCID:
628 message = _("The locale ID is unknown.");
629 break;
630
631 case DISP_E_PARAMNOTOPTIONAL:
632 message = _("Missing a required parameter.");
633 break;
634
635 case DISP_E_PARAMNOTFOUND:
636 message.Printf(_("Argument %u not found."), uiArgErr);
637 break;
638
639 case DISP_E_TYPEMISMATCH:
640 message.Printf(_("Type mismatch in argument %u."), uiArgErr);
641 break;
642
643 case ERROR_FILE_NOT_FOUND:
644 message = _("The system cannot find the file specified.");
645 break;
646
647 case REGDB_E_CLASSNOTREG:
648 message = _("Class not registered.");
649 break;
650
651 default:
652 message.Printf(_("Unknown error %08x"), hr);
653 break;
654 }
655
656 wxLogError(_("OLE Automation error in %s: %s"), member, message);
657 }
658
659 #endif // wxUSE_OLE_AUTOMATION