]>
git.saurik.com Git - wxWidgets.git/blob - src/msw/ole/automtn.cpp
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 // Watcom C++ gives a linker error if this is compiled in.
20 // With Borland C++, all samples crash if this is compiled in.
21 #if wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)
23 #define _FORCENAMELESSUNION
25 #include "wx/msw/private.h"
26 #include "wx/msw/ole/oleutils.h"
27 #include "wx/msw/ole/automtn.h"
31 #include "wx/msw/wince/time.h"
49 #include "wx/datetime.h"
50 #endif // wxUSE_TIMEDATE
52 static void ClearVariant(VARIANTARG
*pvarg
) ;
53 static void ReleaseVariant(VARIANTARG
*pvarg
) ;
54 // static void ShowException(LPOLESTR szMember, HRESULT hr, EXCEPINFO *pexcep, unsigned int uiArgErr);
60 wxAutomationObject::wxAutomationObject(WXIDISPATCH
* dispatchPtr
)
62 m_dispatchPtr
= dispatchPtr
;
65 wxAutomationObject::~wxAutomationObject()
69 ((IDispatch
*)m_dispatchPtr
)->Release();
74 #define INVOKEARG(i) (args ? args[i] : *(ptrArgs[i]))
76 // For Put/Get, no named arguments are allowed.
77 bool wxAutomationObject::Invoke(const wxString
& member
, int action
,
78 wxVariant
& retValue
, int noArgs
, wxVariant args
[], const wxVariant
* ptrArgs
[]) const
83 // nonConstMember is necessary because the wxString class doesn't have enough consts...
84 wxString
nonConstMember(member
);
86 int ch
= nonConstMember
.Find('.');
89 // Use dot notation to get the next object
90 wxString
member2(nonConstMember
.Left((size_t) ch
));
91 wxString
rest(nonConstMember
.Right(nonConstMember
.Length() - ch
- 1));
92 wxAutomationObject obj
;
93 if (!GetObject(obj
, member2
))
95 return obj
.Invoke(rest
, action
, retValue
, noArgs
, args
, ptrArgs
);
99 ClearVariant(& vReturn
);
101 VARIANTARG
* vReturnPtr
= & vReturn
;
103 // Find number of names args
104 int namedArgCount
= 0;
106 for (i
= 0; i
< noArgs
; i
++)
107 if (!INVOKEARG(i
).GetName().IsNull())
112 int namedArgStringCount
= namedArgCount
+ 1;
113 BSTR
* argNames
= new BSTR
[namedArgStringCount
];
114 argNames
[0] = wxConvertStringToOle(member
);
116 // Note that arguments are specified in reverse order
117 // (all totally logical; hey, we're dealing with OLE here.)
120 for (i
= 0; i
< namedArgCount
; i
++)
122 if (!INVOKEARG(i
).GetName().IsNull())
124 argNames
[(namedArgCount
-j
)] = wxConvertStringToOle(INVOKEARG(i
).GetName());
129 // + 1 for the member name, + 1 again in case we're a 'put'
130 DISPID
* dispIds
= new DISPID
[namedArgCount
+ 2];
133 DISPPARAMS dispparams
;
134 unsigned int uiArgErr
;
137 // Get the IDs for the member and its arguments. GetIDsOfNames expects the
138 // member name as the first name, followed by argument names (if any).
139 hr
= ((IDispatch
*)m_dispatchPtr
)->GetIDsOfNames(IID_NULL
, argNames
,
140 1 + namedArgCount
, LOCALE_SYSTEM_DEFAULT
, dispIds
);
143 // ShowException(szMember, hr, NULL, 0);
149 // if doing a property put(ref), we need to adjust the first argument to have a
150 // named arg of DISPID_PROPERTYPUT.
151 if (action
& (DISPATCH_PROPERTYPUT
| DISPATCH_PROPERTYPUTREF
))
154 dispIds
[1] = DISPID_PROPERTYPUT
;
155 vReturnPtr
= (VARIANTARG
*) NULL
;
158 // Convert the wxVariants to VARIANTARGs
159 VARIANTARG
* oleArgs
= new VARIANTARG
[noArgs
];
160 for (i
= 0; i
< noArgs
; i
++)
162 // Again, reverse args
163 if (!wxConvertVariantToOle(INVOKEARG((noArgs
-1) - i
), oleArgs
[i
]))
172 dispparams
.rgdispidNamedArgs
= dispIds
+ 1;
173 dispparams
.rgvarg
= oleArgs
;
174 dispparams
.cArgs
= noArgs
;
175 dispparams
.cNamedArgs
= namedArgCount
;
177 excep
.pfnDeferredFillIn
= NULL
;
179 hr
= ((IDispatch
*)m_dispatchPtr
)->Invoke(dispIds
[0], IID_NULL
, LOCALE_SYSTEM_DEFAULT
,
180 (WORD
)action
, &dispparams
, vReturnPtr
, &excep
, &uiArgErr
);
182 for (i
= 0; i
< namedArgStringCount
; i
++)
184 SysFreeString(argNames
[i
]);
189 for (i
= 0; i
< noArgs
; i
++)
190 ReleaseVariant(& oleArgs
[i
]) ;
195 // display the exception information if appropriate:
196 // ShowException((const char*) member, hr, &excep, uiArgErr);
198 // free exception structure information
199 SysFreeString(excep
.bstrSource
);
200 SysFreeString(excep
.bstrDescription
);
201 SysFreeString(excep
.bstrHelpFile
);
204 ReleaseVariant(vReturnPtr
);
211 // Convert result to wxVariant form
212 wxConvertOleToVariant(vReturn
, retValue
);
213 // Mustn't release the dispatch pointer
214 if (vReturn
.vt
== VT_DISPATCH
)
216 vReturn
.pdispVal
= (IDispatch
*) NULL
;
218 ReleaseVariant(& vReturn
);
224 // Invoke a member function
225 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
, int noArgs
, wxVariant args
[])
227 wxVariant retVariant
;
228 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, args
))
230 retVariant
.MakeNull();
235 wxVariant
wxAutomationObject::CallMethodArray(const wxString
& member
, int noArgs
, const wxVariant
**args
)
237 wxVariant retVariant
;
238 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, noArgs
, NULL
, args
))
240 retVariant
.MakeNull();
245 wxVariant
wxAutomationObject::CallMethod(const wxString
& member
,
246 const wxVariant
& arg1
, const wxVariant
& arg2
,
247 const wxVariant
& arg3
, const wxVariant
& arg4
,
248 const wxVariant
& arg5
, const wxVariant
& arg6
)
250 const wxVariant
** args
= new const wxVariant
*[6];
282 wxVariant retVariant
;
283 if (!Invoke(member
, DISPATCH_METHOD
, retVariant
, i
, NULL
, args
))
285 retVariant
.MakeNull();
292 wxVariant
wxAutomationObject::GetPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
294 wxVariant retVariant
;
295 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
297 retVariant
.MakeNull();
301 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
303 wxVariant retVariant
;
304 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
306 retVariant
.MakeNull();
311 wxVariant
wxAutomationObject::GetProperty(const wxString
& property
,
312 const wxVariant
& arg1
, const wxVariant
& arg2
,
313 const wxVariant
& arg3
, const wxVariant
& arg4
,
314 const wxVariant
& arg5
, const wxVariant
& arg6
)
316 const wxVariant
** args
= new const wxVariant
*[6];
348 wxVariant retVariant
;
349 if (!Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, i
, NULL
, args
))
351 retVariant
.MakeNull();
357 bool wxAutomationObject::PutProperty(const wxString
& property
, int noArgs
, wxVariant args
[])
359 wxVariant retVariant
;
360 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, args
))
367 bool wxAutomationObject::PutPropertyArray(const wxString
& property
, int noArgs
, const wxVariant
**args
)
369 wxVariant retVariant
;
370 if (!Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, noArgs
, NULL
, args
))
377 bool wxAutomationObject::PutProperty(const wxString
& property
,
378 const wxVariant
& arg1
, const wxVariant
& arg2
,
379 const wxVariant
& arg3
, const wxVariant
& arg4
,
380 const wxVariant
& arg5
, const wxVariant
& arg6
)
382 const wxVariant
** args
= new const wxVariant
*[6];
414 wxVariant retVariant
;
415 bool ret
= Invoke(property
, DISPATCH_PROPERTYPUT
, retVariant
, i
, NULL
, args
);
421 // Uses DISPATCH_PROPERTYGET
422 // and returns a dispatch pointer. The calling code should call Release
423 // on the pointer, though this could be implicit by constructing an wxAutomationObject
424 // with it and letting the destructor call Release.
425 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, wxVariant args
[]) const
427 wxVariant retVariant
;
428 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, args
))
430 if (retVariant
.GetType() == wxT("void*"))
432 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
436 return (WXIDISPATCH
*) NULL
;
439 // Uses DISPATCH_PROPERTYGET
440 // and returns a dispatch pointer. The calling code should call Release
441 // on the pointer, though this could be implicit by constructing an wxAutomationObject
442 // with it and letting the destructor call Release.
443 WXIDISPATCH
* wxAutomationObject::GetDispatchProperty(const wxString
& property
, int noArgs
, const wxVariant
**args
) const
445 wxVariant retVariant
;
446 if (Invoke(property
, DISPATCH_PROPERTYGET
, retVariant
, noArgs
, NULL
, args
))
448 if (retVariant
.GetType() == wxT("void*"))
450 return (WXIDISPATCH
*) retVariant
.GetVoidPtr();
454 return (WXIDISPATCH
*) NULL
;
458 // A way of initialising another wxAutomationObject with a dispatch object
459 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, wxVariant args
[]) const
461 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
464 obj
.SetDispatchPtr(dispatch
);
471 // A way of initialising another wxAutomationObject with a dispatch object
472 bool wxAutomationObject::GetObject(wxAutomationObject
& obj
, const wxString
& property
, int noArgs
, const wxVariant
**args
) const
474 WXIDISPATCH
* dispatch
= GetDispatchProperty(property
, noArgs
, args
);
477 obj
.SetDispatchPtr(dispatch
);
484 // Get a dispatch pointer from the current object associated
486 bool wxAutomationObject::GetInstance(const wxString
& classId
) const
492 IUnknown
* pUnk
= NULL
;
494 wxBasicString
unicodeName(classId
.mb_str());
496 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
498 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
502 if (FAILED(GetActiveObject(clsId
, NULL
, &pUnk
)))
504 wxLogWarning(wxT("Cannot find an active object"));
508 if (pUnk
->QueryInterface(IID_IDispatch
, (LPVOID
*) &m_dispatchPtr
) != S_OK
)
510 wxLogWarning(wxT("Cannot find IDispatch interface"));
517 // Get a dispatch pointer from a new object associated
518 // with the given class id
519 bool wxAutomationObject::CreateInstance(const wxString
& classId
) const
526 wxBasicString
unicodeName(classId
.mb_str());
528 if (FAILED(CLSIDFromProgID((BSTR
) unicodeName
, &clsId
)))
530 wxLogWarning(wxT("Cannot obtain CLSID from ProgID"));
534 // start a new copy of Excel, grab the IDispatch interface
535 if (FAILED(CoCreateInstance(clsId
, NULL
, CLSCTX_LOCAL_SERVER
, IID_IDispatch
, (void**)&m_dispatchPtr
)))
537 wxLogWarning(wxT("Cannot start an instance of this class."));
545 WXDLLEXPORT
bool wxConvertVariantToOle(const wxVariant
& variant
, VARIANTARG
& oleVariant
)
547 ClearVariant(&oleVariant
);
548 if (variant
.IsNull())
550 oleVariant
.vt
= VT_NULL
;
554 wxString
type(variant
.GetType());
557 if (type
== wxT("long"))
559 oleVariant
.vt
= VT_I4
;
560 oleVariant
.lVal
= variant
.GetLong() ;
562 // cVal not always present
564 else if (type
== wxT("char"))
566 oleVariant
.vt
=VT_I1
; // Signed Char
567 oleVariant
.cVal
=variant
.GetChar();
570 else if (type
== wxT("double"))
572 oleVariant
.vt
= VT_R8
;
573 oleVariant
.dblVal
= variant
.GetDouble();
575 else if (type
== wxT("bool"))
577 oleVariant
.vt
= VT_BOOL
;
578 // 'bool' required for VC++ 4 apparently
579 #if (defined(__VISUALC__) && (__VISUALC__ <= 1000))
580 oleVariant
.bool = variant
.GetBool();
582 oleVariant
.boolVal
= variant
.GetBool();
585 else if (type
== wxT("string"))
587 wxString
str( variant
.GetString() );
588 oleVariant
.vt
= VT_BSTR
;
589 oleVariant
.bstrVal
= wxConvertStringToOle(str
);
592 else if (type
== wxT("datetime"))
594 wxDateTime
date( variant
.GetDateTime() );
595 oleVariant
.vt
= VT_DATE
;
597 long dosDateTime
= date
.GetAsDOS();
598 short dosDate
= short((dosDateTime
& 0xFFFF0000) >> 16);
599 short dosTime
= short(dosDateTime
& 0xFFFF);
601 DosDateTimeToVariantTime(dosDate
, dosTime
, & oleVariant
.date
);
604 else if (type
== wxT("void*"))
606 oleVariant
.vt
= VT_DISPATCH
;
607 oleVariant
.pdispVal
= (IDispatch
*) variant
.GetVoidPtr();
609 else if (type
== wxT("list") || type
== wxT("stringlist"))
611 oleVariant
.vt
= VT_VARIANT
| VT_ARRAY
;
614 SAFEARRAYBOUND saBound
;
615 VARIANTARG
*pvargBase
;
619 int iCount
= variant
.GetCount();
622 saBound
.cElements
= iCount
;
624 psa
= SafeArrayCreate(VT_VARIANT
, 1, &saBound
);
628 SafeArrayAccessData(psa
, (void**)&pvargBase
);
631 for (i
= 0; i
< iCount
; i
++)
633 // copy each string in the list of strings
634 wxVariant
eachVariant(variant
[i
]);
635 if (!wxConvertVariantToOle(eachVariant
, * pvarg
))
637 // memory failure: back out and free strings alloc'ed up to
638 // now, and then the array itself.
640 for (j
= 0; j
< i
; j
++)
642 SysFreeString(pvarg
->bstrVal
);
645 SafeArrayDestroy(psa
);
651 SafeArrayUnaccessData(psa
);
653 oleVariant
.parray
= psa
;
657 oleVariant
.vt
= VT_NULL
;
664 #define VT_TYPEMASK 0xfff
667 WXDLLEXPORT
bool wxConvertOleToVariant(const VARIANTARG
& oleVariant
, wxVariant
& variant
)
669 switch (oleVariant
.vt
& VT_TYPEMASK
)
673 wxString
str(wxConvertStringFromOle(oleVariant
.bstrVal
));
680 unsigned short dosDate
= 0;
681 unsigned short dosTime
= 0;
682 VariantTimeToDosDateTime(oleVariant
.date
, & dosDate
, & dosTime
);
684 long dosDateTime
= (dosDate
<< 16) || dosTime
;
686 date
.SetFromDOS(dosDateTime
);
693 variant
= (long) oleVariant
.lVal
;
698 variant
= (long) oleVariant
.iVal
;
704 #if (defined(_MSC_VER) && (_MSC_VER <= 1000) && !defined(__MWERKS__) ) //GC
705 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
706 variant
= (long) (oleVariant
.bool != 0);
708 variant
= (bool) (oleVariant
.bool != 0);
711 #ifndef HAVE_BOOL // Can't use bool operator if no native bool type
712 variant
= (long) (oleVariant
.boolVal
!= 0);
714 variant
= (bool) (oleVariant
.boolVal
!= 0);
721 variant
= oleVariant
.dblVal
;
728 int cDims
, cElements
, i
;
731 // Iterate the dimensions: number of elements is x*y*z
732 for (cDims
= 0, cElements
= 1;
733 cDims
< oleVariant
.parray
->cDims
; cDims
++)
734 cElements
*= oleVariant
.parray
->rgsabound
[cDims
].cElements
;
736 // Get a pointer to the data
737 HRESULT hr
= SafeArrayAccessData(oleVariant
.parray
, (void HUGEP
* FAR
*) & pvdata
);
741 for (i
= 0; i
< cElements
; i
++)
743 VARIANTARG
& oleElement
= pvdata
[i
];
745 if (!wxConvertOleToVariant(oleElement
, vElement
))
748 variant
.Append(vElement
);
750 SafeArrayUnaccessData(oleVariant
.parray
);
755 variant
= (void*) oleVariant
.pdispVal
;
765 break; // Ignore Empty Variant, used only during destruction of objects
769 wxLogError(wxT("wxAutomationObject::ConvertOleToVariant: Unknown variant value type"));
779 * Zeros a variant structure without regard to current contents
781 static void ClearVariant(VARIANTARG
*pvarg
)
783 pvarg
->vt
= VT_EMPTY
;
784 pvarg
->wReserved1
= 0;
785 pvarg
->wReserved2
= 0;
786 pvarg
->wReserved3
= 0;
793 * Clears a particular variant structure and releases any external objects
794 * or memory contained in the variant. Supports the data types listed above.
796 static void ReleaseVariant(VARIANTARG
*pvarg
)
799 VARIANTARG _huge
*pvargArray
;
800 long lLBound
, lUBound
, l
;
802 vt
= (VARTYPE
)(pvarg
->vt
& 0xfff); // mask off flags
804 // check if an array. If so, free its contents, then the array itself.
805 if (V_ISARRAY(pvarg
))
807 // variant arrays are all this routine currently knows about. Since a
808 // variant can contain anything (even other arrays), call ourselves
810 if (vt
== VT_VARIANT
)
812 SafeArrayGetLBound(pvarg
->parray
, 1, &lLBound
);
813 SafeArrayGetUBound(pvarg
->parray
, 1, &lUBound
);
815 if (lUBound
> lLBound
)
819 SafeArrayAccessData(pvarg
->parray
, (void**)&pvargArray
);
821 for (l
= 0; l
< lUBound
; l
++)
823 ReleaseVariant(pvargArray
);
827 SafeArrayUnaccessData(pvarg
->parray
);
832 wxLogWarning(wxT("ReleaseVariant: Array contains non-variant type"));
835 // Free the array itself.
836 SafeArrayDestroy(pvarg
->parray
);
844 pvarg
->pdispVal
->Release();
848 SysFreeString(pvarg
->bstrVal
);
855 case VT_ERROR
: // to avoid erroring on an error return from Excel
857 // no work for these types
861 wxLogWarning(wxT("ReleaseVariant: Unknown type"));
871 void ShowException(LPOLESTR szMember
, HRESULT hr
, EXCEPINFO
*pexcep
, unsigned int uiArgErr
)
875 switch (GetScode(hr
))
877 case DISP_E_UNKNOWNNAME
:
878 wsprintf(szBuf
, L
"%s: Unknown name or named argument.", szMember
);
881 case DISP_E_BADPARAMCOUNT
:
882 wsprintf(szBuf
, L
"%s: Incorrect number of arguments.", szMember
);
885 case DISP_E_EXCEPTION
:
886 wsprintf(szBuf
, L
"%s: Error %d: ", szMember
, pexcep
->wCode
);
887 if (pexcep
->bstrDescription
!= NULL
)
888 lstrcat(szBuf
, pexcep
->bstrDescription
);
890 lstrcat(szBuf
, L
"<<No Description>>");
893 case DISP_E_MEMBERNOTFOUND
:
894 wsprintf(szBuf
, L
"%s: method or property not found.", szMember
);
897 case DISP_E_OVERFLOW
:
898 wsprintf(szBuf
, L
"%s: Overflow while coercing argument values.", szMember
);
901 case DISP_E_NONAMEDARGS
:
902 wsprintf(szBuf
, L
"%s: Object implementation does not support named arguments.",
906 case DISP_E_UNKNOWNLCID
:
907 wsprintf(szBuf
, L
"%s: The locale ID is unknown.", szMember
);
910 case DISP_E_PARAMNOTOPTIONAL
:
911 wsprintf(szBuf
, L
"%s: Missing a required parameter.", szMember
);
914 case DISP_E_PARAMNOTFOUND
:
915 wsprintf(szBuf
, L
"%s: Argument not found, argument %d.", szMember
, uiArgErr
);
918 case DISP_E_TYPEMISMATCH
:
919 wsprintf(szBuf
, L
"%s: Type mismatch, argument %d.", szMember
, uiArgErr
);
923 wsprintf(szBuf
, L
"%s: Unknown error occurred.", szMember
);
932 #endif // wxUSE_OLE && !(defined(__BORLANDC__) && (__BORLANDC__ < 0x520)) && !defined(__CYGWIN10__)