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