1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/msw/ole/automtn.cpp
3 // Purpose: OLE automation utilities
4 // Author: Julian Smart
8 // Copyright: (c) 1998, Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx.h".
13 #include "wx/wxprec.h"
15 #if defined(__BORLANDC__)
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
30 #define _FORCENAMELESSUNION
31 #include "wx/msw/private.h"
32 #include "wx/msw/ole/oleutils.h"
33 #include "wx/msw/ole/automtn.h"
36 #include "wx/msw/wince/time.h"
54 #include "wx/datetime.h"
55 #endif // wxUSE_DATETIME
57 #if wxUSE_OLE_AUTOMATION
59 #include <wx/vector.h>
61 // Report an OLE error when calling the specified method to the user via wxLog.
63 ShowException(const wxString
& member
,
65 EXCEPINFO
*pexcep
= NULL
,
66 unsigned int uiArgErr
= 0);
70 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
72 m_dispatchPtr
= dispatchPtr
;
75 wxAutomationObject::~wxAutomationObject()
79 ((IDispatch
*)m_dispatchPtr
)->Release();
87 // A simple helper that ensures that VARIANT is destroyed on scope exit.
88 struct wxOleVariantArg
: VARIANTARG
90 wxOleVariantArg() { VariantInit(this); }
91 ~wxOleVariantArg() { VariantClear(this); }
94 } // anonymous namespace
97 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
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
108 int ch
= member
.Find('.');
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
))
117 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
120 wxOleVariantArg vReturn
;
121 wxOleVariantArg
* vReturnPtr
= & vReturn
;
123 // Find number of names args
124 int namedArgCount
= 0;
126 for (i
= 0; i
< noArgs
; i
++)
128 if ( !INVOKEARG(i
).GetName().empty() )
134 int namedArgStringCount
= namedArgCount
+ 1;
135 wxVector
<wxBasicString
> argNames(namedArgStringCount
, wxString());
136 argNames
[0] = member
;
138 // Note that arguments are specified in reverse order
139 // (all totally logical; hey, we're dealing with OLE here.)
142 for (i
= 0; i
< namedArgCount
; i
++)
144 if ( !INVOKEARG(i
).GetName().empty() )
146 argNames
[(namedArgCount
-j
)] = INVOKEARG(i
).GetName();
151 // + 1 for the member name, + 1 again in case we're a 'put'
152 wxVector
<DISPID
> dispIds(namedArgCount
+ 2);
155 DISPPARAMS dispparams
;
156 unsigned int uiArgErr
;
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
, LOCALE_SYSTEM_DEFAULT
, &dispIds
[0]);
167 ShowException(member
, hr
);
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
))
176 dispIds
[1] = DISPID_PROPERTYPUT
;
180 // Convert the wxVariants to VARIANTARGs
181 wxVector
<wxOleVariantArg
> oleArgs(noArgs
);
182 for (i
= 0; i
< noArgs
; i
++)
184 // Again, reverse args
185 if (!wxConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
189 dispparams
.rgdispidNamedArgs
= &dispIds
[0] + 1;
190 dispparams
.rgvarg
= &oleArgs
[0];
191 dispparams
.cArgs
= noArgs
;
192 dispparams
.cNamedArgs
= namedArgCount
;
197 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
198 (WORD
)action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
202 // display the exception information if appropriate:
203 ShowException(member
, hr
, &excep
, uiArgErr
);
205 // free exception structure information
206 SysFreeString(excep
.bstrSource
);
207 SysFreeString(excep
.bstrDescription
);
208 SysFreeString(excep
.bstrHelpFile
);
216 // Convert result to wxVariant form
217 if (!wxConvertOleToVariant(vReturn
, retValue
))
219 // Mustn't release the dispatch pointer
220 if (vReturn
.vt
== VT_DISPATCH
)
222 vReturn
.pdispVal
= NULL
;
229 // Invoke a member function
230 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
232 wxVariant retVariant
;
233 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
235 retVariant
.MakeNull();
240 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
242 wxVariant retVariant
;
243 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
245 retVariant
.MakeNull();
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
)
255 const wxVariant
** args
= new const wxVariant
*[6];
287 wxVariant retVariant
;
288 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
290 retVariant
.MakeNull();
297 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
299 wxVariant retVariant
;
300 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
302 retVariant
.MakeNull();
306 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
308 wxVariant retVariant
;
309 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
311 retVariant
.MakeNull();
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
)
321 const wxVariant
** args
= new const wxVariant
*[6];
353 wxVariant retVariant
;
354 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
356 retVariant
.MakeNull();
362 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
364 wxVariant retVariant
;
365 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
372 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
374 wxVariant retVariant
;
375 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
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
)
387 const wxVariant
** args
= new const wxVariant
*[6];
419 wxVariant retVariant
;
420 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
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
432 wxVariant retVariant
;
433 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
435 if (retVariant
.GetType() == wxT("void*"))
437 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
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
450 wxVariant retVariant
;
451 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
453 if (retVariant
.GetType() == wxT("void*"))
455 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
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
466 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
469 obj
.SetDispatchPtr(dispatch
);
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
479 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
482 obj
.SetDispatchPtr(dispatch
);
492 HRESULT
wxCLSIDFromProgID(const wxString
& progId
, CLSID
& clsId
)
494 HRESULT hr
= CLSIDFromProgID(wxBasicString(progId
), &clsId
);
497 wxLogSysError(hr
, _("Failed to find CLSID of \"%s\""), progId
);
502 void *DoCreateInstance(const wxString
& progId
, const CLSID
& clsId
)
504 // get the server IDispatch interface
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
);
514 wxLogSysError(hr
, _("Failed to create an instance of \"%s\""), progId
);
521 } // anonymous namespace
523 // Get a dispatch pointer from the current object associated
525 bool wxAutomationObject::GetInstance(const wxString
& progId
, int flags
) const
531 HRESULT hr
= wxCLSIDFromProgID(progId
, clsId
);
535 IUnknown
*pUnk
= NULL
;
536 hr
= GetActiveObject(clsId
, NULL
, &pUnk
);
539 if ( flags
& wxAutomationInstance_CreateIfNeeded
)
541 const_cast<wxAutomationObject
*>(this)->
542 m_dispatchPtr
= DoCreateInstance(progId
, clsId
);
548 // Log an error except if we're supposed to fail silently when the
549 // error is that no current instance exists.
550 if ( hr
!= MK_E_UNAVAILABLE
||
551 !(flags
& wxAutomationInstance_SilentIfNone
) )
554 _("Cannot get an active instance of \"%s\""),
562 hr
= pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
);
566 _("Failed to get OLE automation interface for \"%s\""),
574 // Get a dispatch pointer from a new object associated
575 // with the given ProgID
576 bool wxAutomationObject::CreateInstance(const wxString
& progId
) const
582 HRESULT hr
= wxCLSIDFromProgID(progId
, clsId
);
586 const_cast<wxAutomationObject
*>(this)->
587 m_dispatchPtr
= DoCreateInstance(progId
, clsId
);
589 return m_dispatchPtr
!= NULL
;
593 ShowException(const wxString
& member
,
596 unsigned int uiArgErr
)
599 switch (GetScode(hr
))
601 case DISP_E_UNKNOWNNAME
:
602 message
= _("Unknown name or named argument.");
605 case DISP_E_BADPARAMCOUNT
:
606 message
= _("Incorrect number of arguments.");
609 case DISP_E_EXCEPTION
:
612 if ( pexcep
->bstrDescription
)
613 message
<< pexcep
->bstrDescription
<< wxS(" ");
614 message
+= wxString::Format(wxS("error code %u"), pexcep
->wCode
);
618 message
= _("Unknown exception");
622 case DISP_E_MEMBERNOTFOUND
:
623 message
= _("Method or property not found.");
626 case DISP_E_OVERFLOW
:
627 message
= _("Overflow while coercing argument values.");
630 case DISP_E_NONAMEDARGS
:
631 message
= _("Object implementation does not support named arguments.");
634 case DISP_E_UNKNOWNLCID
:
635 message
= _("The locale ID is unknown.");
638 case DISP_E_PARAMNOTOPTIONAL
:
639 message
= _("Missing a required parameter.");
642 case DISP_E_PARAMNOTFOUND
:
643 message
.Printf(_("Argument %u not found."), uiArgErr
);
646 case DISP_E_TYPEMISMATCH
:
647 message
.Printf(_("Type mismatch in argument %u."), uiArgErr
);
650 case ERROR_FILE_NOT_FOUND
:
651 message
= _("The system cannot find the file specified.");
654 case REGDB_E_CLASSNOTREG
:
655 message
= _("Class not registered.");
659 message
.Printf(_("Unknown error %08x"), hr
);
663 wxLogError(_("OLE Automation error in %s: %s"), member
, message
);
666 #endif // wxUSE_OLE_AUTOMATION