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
;
73 m_lcid
= LOCALE_SYSTEM_DEFAULT
;
76 wxAutomationObject::~wxAutomationObject()
80 ((IDispatch
*)m_dispatchPtr
)->Release();
88 // A simple helper that ensures that VARIANT is destroyed on scope exit.
89 struct wxOleVariantArg
: VARIANTARG
91 wxOleVariantArg() { VariantInit(this); }
92 ~wxOleVariantArg() { VariantClear(this); }
95 } // anonymous namespace
98 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
100 // For Put/Get, no named arguments are allowed.
101 // WARNING: if args contain IDispatches, their reference count will be decreased
102 // by one after Invoke() returns!
103 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
104 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
109 int ch
= member
.Find('.');
112 // Use dot notation to get the next object
113 wxString
member2(member
.Left((size_t) ch
));
114 wxString
rest(member
.Right(member
.length() - ch
- 1));
115 wxAutomationObject obj
;
116 if (!GetObject(obj
, member2
))
118 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
121 wxOleVariantArg vReturn
;
122 wxOleVariantArg
* vReturnPtr
= & vReturn
;
124 // Find number of names args
125 int namedArgCount
= 0;
127 for (i
= 0; i
< noArgs
; i
++)
129 if ( !INVOKEARG(i
).GetName().empty() )
135 int namedArgStringCount
= namedArgCount
+ 1;
136 wxVector
<wxBasicString
> argNames(namedArgStringCount
, wxString());
137 argNames
[0] = member
;
139 // Note that arguments are specified in reverse order
140 // (all totally logical; hey, we're dealing with OLE here.)
143 for (i
= 0; i
< namedArgCount
; i
++)
145 if ( !INVOKEARG(i
).GetName().empty() )
147 argNames
[(namedArgCount
-j
)] = INVOKEARG(i
).GetName();
152 // + 1 for the member name, + 1 again in case we're a 'put'
153 wxVector
<DISPID
> dispIds(namedArgCount
+ 2);
156 DISPPARAMS dispparams
;
157 unsigned int uiArgErr
;
159 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
160 // member name as the first name, followed by argument names (if any).
161 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
,
162 // We rely on the fact that wxBasicString is
163 // just BSTR with some methods here.
164 reinterpret_cast<BSTR
*>(&argNames
[0]),
165 1 + namedArgCount
, m_lcid
, &dispIds
[0]);
168 ShowException(member
, hr
);
172 // if doing a property put(ref), we need to adjust the first argument to have a
173 // named arg of DISPID_PROPERTYPUT.
174 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
177 dispIds
[1] = DISPID_PROPERTYPUT
;
181 // Convert the wxVariants to VARIANTARGs
182 wxVector
<wxOleVariantArg
> oleArgs(noArgs
);
183 for (i
= 0; i
< noArgs
; i
++)
185 // Again, reverse args
186 if (!wxConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
190 dispparams
.rgdispidNamedArgs
= &dispIds
[0] + 1;
191 dispparams
.rgvarg
= oleArgs
.empty() ? NULL
: &oleArgs
[0];
192 dispparams
.cArgs
= noArgs
;
193 dispparams
.cNamedArgs
= namedArgCount
;
198 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, m_lcid
,
199 (WORD
)action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
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
);
217 // Convert result to wxVariant form
218 if (!wxConvertOleToVariant(vReturn
, retValue
))
220 // Mustn't release the dispatch pointer
221 if (vReturn
.vt
== VT_DISPATCH
)
223 vReturn
.pdispVal
= NULL
;
230 // Invoke a member function
231 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
233 wxVariant retVariant
;
234 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
236 retVariant
.MakeNull();
241 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
243 wxVariant retVariant
;
244 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
246 retVariant
.MakeNull();
251 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
252 const wxVariant
& arg1
, const wxVariant
& arg2
,
253 const wxVariant
& arg3
, const wxVariant
& arg4
,
254 const wxVariant
& arg5
, const wxVariant
& arg6
)
256 const wxVariant
** args
= new const wxVariant
*[6];
288 wxVariant retVariant
;
289 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
291 retVariant
.MakeNull();
298 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
300 wxVariant retVariant
;
301 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
303 retVariant
.MakeNull();
307 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
309 wxVariant retVariant
;
310 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
312 retVariant
.MakeNull();
317 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
318 const wxVariant
& arg1
, const wxVariant
& arg2
,
319 const wxVariant
& arg3
, const wxVariant
& arg4
,
320 const wxVariant
& arg5
, const wxVariant
& arg6
)
322 const wxVariant
** args
= new const wxVariant
*[6];
354 wxVariant retVariant
;
355 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
357 retVariant
.MakeNull();
363 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
365 wxVariant retVariant
;
366 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
373 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
375 wxVariant retVariant
;
376 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
383 bool wxAutomationObject::PutProperty(const wxString
& property
,
384 const wxVariant
& arg1
, const wxVariant
& arg2
,
385 const wxVariant
& arg3
, const wxVariant
& arg4
,
386 const wxVariant
& arg5
, const wxVariant
& arg6
)
388 const wxVariant
** args
= new const wxVariant
*[6];
420 wxVariant retVariant
;
421 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
427 // Uses DISPATCH_PROPERTYGET
428 // and returns a dispatch pointer. The calling code should call Release
429 // on the pointer, though this could be implicit by constructing an wxAutomationObject
430 // with it and letting the destructor call Release.
431 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
433 wxVariant retVariant
;
434 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
436 if (retVariant
.GetType() == wxT("void*"))
438 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
445 // Uses DISPATCH_PROPERTYGET
446 // and returns a dispatch pointer. The calling code should call Release
447 // on the pointer, though this could be implicit by constructing an wxAutomationObject
448 // with it and letting the destructor call Release.
449 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
451 wxVariant retVariant
;
452 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
454 if (retVariant
.GetType() == wxT("void*"))
456 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
464 // A way of initialising another wxAutomationObject with a dispatch object
465 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
467 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
470 obj
.SetDispatchPtr(dispatch
);
471 obj
.SetLCID(GetLCID());
478 // A way of initialising another wxAutomationObject with a dispatch object
479 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, const wxVariant
**args
) const
481 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
484 obj
.SetDispatchPtr(dispatch
);
485 obj
.SetLCID(GetLCID());
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
);
551 // Log an error except if we're supposed to fail silently when the
552 // error is that no current instance exists.
553 if ( hr
!= MK_E_UNAVAILABLE
||
554 !(flags
& wxAutomationInstance_SilentIfNone
) )
557 _("Cannot get an active instance of \"%s\""),
565 hr
= pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
);
569 _("Failed to get OLE automation interface for \"%s\""),
577 // Get a dispatch pointer from a new object associated
578 // with the given ProgID
579 bool wxAutomationObject::CreateInstance(const wxString
& progId
) const
585 HRESULT hr
= wxCLSIDFromProgID(progId
, clsId
);
589 const_cast<wxAutomationObject
*>(this)->
590 m_dispatchPtr
= DoCreateInstance(progId
, clsId
);
592 return m_dispatchPtr
!= NULL
;
595 LCID
wxAutomationObject::GetLCID() const
600 void wxAutomationObject::SetLCID(LCID lcid
)
606 ShowException(const wxString
& member
,
609 unsigned int uiArgErr
)
612 switch (GetScode(hr
))
614 case DISP_E_UNKNOWNNAME
:
615 message
= _("Unknown name or named argument.");
618 case DISP_E_BADPARAMCOUNT
:
619 message
= _("Incorrect number of arguments.");
622 case DISP_E_EXCEPTION
:
625 if ( pexcep
->bstrDescription
)
626 message
<< pexcep
->bstrDescription
<< wxS(" ");
627 message
+= wxString::Format(wxS("error code %u"), pexcep
->wCode
);
631 message
= _("Unknown exception");
635 case DISP_E_MEMBERNOTFOUND
:
636 message
= _("Method or property not found.");
639 case DISP_E_OVERFLOW
:
640 message
= _("Overflow while coercing argument values.");
643 case DISP_E_NONAMEDARGS
:
644 message
= _("Object implementation does not support named arguments.");
647 case DISP_E_UNKNOWNLCID
:
648 message
= _("The locale ID is unknown.");
651 case DISP_E_PARAMNOTOPTIONAL
:
652 message
= _("Missing a required parameter.");
655 case DISP_E_PARAMNOTFOUND
:
656 message
.Printf(_("Argument %u not found."), uiArgErr
);
659 case DISP_E_TYPEMISMATCH
:
660 message
.Printf(_("Type mismatch in argument %u."), uiArgErr
);
663 case ERROR_FILE_NOT_FOUND
:
664 message
= _("The system cannot find the file specified.");
667 case REGDB_E_CLASSNOTREG
:
668 message
= _("Class not registered.");
672 message
.Printf(_("Unknown error %08x"), hr
);
676 wxLogError(_("OLE Automation error in %s: %s"), member
, message
);
679 #endif // wxUSE_OLE_AUTOMATION