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 // Report an OLE error when calling the specified method to the user via wxLog.
61 ShowException(const wxString
& member
,
63 EXCEPINFO
*pexcep
= NULL
,
64 unsigned int uiArgErr
= 0);
68 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
70 m_dispatchPtr
= dispatchPtr
;
73 wxAutomationObject::~wxAutomationObject()
77 ((IDispatch
*)m_dispatchPtr
)->Release();
82 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
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
91 // nonConstMember is necessary because the wxString class doesn't have enough consts...
92 wxString
nonConstMember(member
);
94 int ch
= nonConstMember
.Find('.');
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
))
103 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
107 VariantInit(& vReturn
);
109 VARIANTARG
* vReturnPtr
= & vReturn
;
111 // Find number of names args
112 int namedArgCount
= 0;
114 for (i
= 0; i
< noArgs
; i
++)
115 if (!INVOKEARG(i
).GetName().IsNull())
120 int namedArgStringCount
= namedArgCount
+ 1;
121 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
122 argNames
[0] = wxConvertStringToOle(member
);
124 // Note that arguments are specified in reverse order
125 // (all totally logical; hey, we're dealing with OLE here.)
128 for (i
= 0; i
< namedArgCount
; i
++)
130 if (!INVOKEARG(i
).GetName().IsNull())
132 argNames
[(namedArgCount
-j
)] = wxConvertStringToOle(INVOKEARG(i
).GetName());
137 // + 1 for the member name, + 1 again in case we're a 'put'
138 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
141 DISPPARAMS dispparams
;
142 unsigned int uiArgErr
;
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
);
150 ShowException(member
, hr
);
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
))
161 dispIds
[1] = DISPID_PROPERTYPUT
;
165 // Convert the wxVariants to VARIANTARGs
166 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
167 for (i
= 0; i
< noArgs
; i
++)
169 // Again, reverse args
170 if (!wxConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
179 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
180 dispparams
.rgvarg
= oleArgs
;
181 dispparams
.cArgs
= noArgs
;
182 dispparams
.cNamedArgs
= namedArgCount
;
187 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
188 (WORD
)action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
190 for (i
= 0; i
< namedArgStringCount
; i
++)
192 SysFreeString(argNames
[i
]);
197 for (i
= 0; i
< noArgs
; i
++)
198 VariantClear(& oleArgs
[i
]) ;
203 // display the exception information if appropriate:
204 ShowException(member
, hr
, &excep
, uiArgErr
);
206 // free exception structure information
207 SysFreeString(excep
.bstrSource
);
208 SysFreeString(excep
.bstrDescription
);
209 SysFreeString(excep
.bstrHelpFile
);
212 VariantClear(vReturnPtr
);
219 // Convert result to wxVariant form
220 wxConvertOleToVariant(vReturn
, retValue
);
221 // Mustn't release the dispatch pointer
222 if (vReturn
.vt
== VT_DISPATCH
)
224 vReturn
.pdispVal
= NULL
;
226 VariantClear(& vReturn
);
232 // Invoke a member function
233 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
235 wxVariant retVariant
;
236 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
238 retVariant
.MakeNull();
243 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
245 wxVariant retVariant
;
246 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
248 retVariant
.MakeNull();
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
)
258 const wxVariant
** args
= new const wxVariant
*[6];
290 wxVariant retVariant
;
291 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
293 retVariant
.MakeNull();
300 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
302 wxVariant retVariant
;
303 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
305 retVariant
.MakeNull();
309 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
311 wxVariant retVariant
;
312 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
314 retVariant
.MakeNull();
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
)
324 const wxVariant
** args
= new const wxVariant
*[6];
356 wxVariant retVariant
;
357 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
359 retVariant
.MakeNull();
365 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
367 wxVariant retVariant
;
368 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
375 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
377 wxVariant retVariant
;
378 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
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
)
390 const wxVariant
** args
= new const wxVariant
*[6];
422 wxVariant retVariant
;
423 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
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
435 wxVariant retVariant
;
436 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
438 if (retVariant
.GetType() == wxT("void*"))
440 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
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
453 wxVariant retVariant
;
454 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
456 if (retVariant
.GetType() == wxT("void*"))
458 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
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
469 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
472 obj
.SetDispatchPtr(dispatch
);
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
482 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
485 obj
.SetDispatchPtr(dispatch
);
495 HRESULT
wxCLSIDFromProgID(const wxString
& progId
, CLSID
& clsId
)
497 HRESULT hr
= CLSIDFromProgID(wxBasicString(progId
), &clsId
);
500 wxLogSysError(hr
, _("Failed to find CLSID of \"%s\""), progId
);
505 void *DoCreateInstance(const wxString
& progId
, const CLSID
& clsId
)
507 // get the server IDispatch interface
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
);
517 wxLogSysError(hr
, _("Failed to create an instance of \"%s\""), progId
);
524 } // anonymous namespace
526 // Get a dispatch pointer from the current object associated
528 bool wxAutomationObject::GetInstance(const wxString
& progId
, int flags
) const
534 HRESULT hr
= wxCLSIDFromProgID(progId
, clsId
);
538 IUnknown
*pUnk
= NULL
;
539 hr
= GetActiveObject(clsId
, NULL
, &pUnk
);
542 if ( flags
& wxAutomationInstance_CreateIfNeeded
)
544 const_cast<wxAutomationObject
*>(this)->
545 m_dispatchPtr
= DoCreateInstance(progId
, clsId
);
552 _("Cannot get an active instance of \"%s\""), progId
);
558 hr
= pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
);
562 _("Failed to get OLE automation interface for \"%s\""),
570 // Get a dispatch pointer from a new object associated
571 // with the given ProgID
572 bool wxAutomationObject::CreateInstance(const wxString
& progId
) const
578 HRESULT hr
= wxCLSIDFromProgID(progId
, clsId
);
582 const_cast<wxAutomationObject
*>(this)->
583 m_dispatchPtr
= DoCreateInstance(progId
, clsId
);
585 return m_dispatchPtr
!= NULL
;
589 ShowException(const wxString
& member
,
592 unsigned int uiArgErr
)
595 switch (GetScode(hr
))
597 case DISP_E_UNKNOWNNAME
:
598 message
= _("Unknown name or named argument.");
601 case DISP_E_BADPARAMCOUNT
:
602 message
= _("Incorrect number of arguments.");
605 case DISP_E_EXCEPTION
:
608 if ( pexcep
->bstrDescription
)
609 message
<< pexcep
->bstrDescription
<< wxS(" ");
610 message
+= wxString::Format(wxS("error code %u"), pexcep
->wCode
);
614 message
= _("Unknown exception");
618 case DISP_E_MEMBERNOTFOUND
:
619 message
= _("Method or property not found.");
622 case DISP_E_OVERFLOW
:
623 message
= _("Overflow while coercing argument values.");
626 case DISP_E_NONAMEDARGS
:
627 message
= _("Object implementation does not support named arguments.");
630 case DISP_E_UNKNOWNLCID
:
631 message
= _("The locale ID is unknown.");
634 case DISP_E_PARAMNOTOPTIONAL
:
635 message
= _("Missing a required parameter.");
638 case DISP_E_PARAMNOTFOUND
:
639 message
.Printf(_("Argument %u not found."), uiArgErr
);
642 case DISP_E_TYPEMISMATCH
:
643 message
.Printf(_("Type mismatch in argument %u."), uiArgErr
);
646 case ERROR_FILE_NOT_FOUND
:
647 message
= _("The system cannot find the file specified.");
650 case REGDB_E_CLASSNOTREG
:
651 message
= _("Class not registered.");
655 message
.Printf(_("Unknown error %08x"), hr
);
659 wxLogError(_("OLE Automation error in %s: %s"), member
, message
);
662 #endif // wxUSE_OLE_AUTOMATION