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