xref: /aoo41x/main/extensions/source/ole/oleobjw.hxx (revision cdf0e10c)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef __OLEOBJW_HXX
29*cdf0e10cSrcweir #define __OLEOBJW_HXX
30*cdf0e10cSrcweir #include "ole2uno.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #ifdef _MSC_VER
33*cdf0e10cSrcweir #pragma warning (push,1)
34*cdf0e10cSrcweir #pragma warning (disable:4548)
35*cdf0e10cSrcweir #endif
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <tools/presys.h>
38*cdf0e10cSrcweir #define _WIN32_WINNT 0x0400
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #if defined(_MSC_VER) && (_MSC_VER >= 1300)
41*cdf0e10cSrcweir #undef _DEBUG
42*cdf0e10cSrcweir #endif
43*cdf0e10cSrcweir #include <atlbase.h>
44*cdf0e10cSrcweir #include <vector>
45*cdf0e10cSrcweir #include <hash_map>
46*cdf0e10cSrcweir #include <tools/postsys.h>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir #ifdef _MSC_VER
49*cdf0e10cSrcweir #pragma warning (pop)
50*cdf0e10cSrcweir #endif
51*cdf0e10cSrcweir #include <cppuhelper/implbase3.hxx>
52*cdf0e10cSrcweir #include <cppuhelper/implbase4.hxx>
53*cdf0e10cSrcweir #include <cppuhelper/implbase7.hxx>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
56*cdf0e10cSrcweir #include <com/sun/star/bridge/oleautomation/XAutomationObject.hpp>
57*cdf0e10cSrcweir #include <rtl/ustring.hxx>
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir #include <com/sun/star/script/XDefaultProperty.hpp>
60*cdf0e10cSrcweir #include <com/sun/star/script/XDefaultMethod.hpp>
61*cdf0e10cSrcweir #include <com/sun/star/script/XDirectInvocation.hpp>
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir #include <typelib/typedescription.hxx>
64*cdf0e10cSrcweir #include "unoconversionutilities.hxx"
65*cdf0e10cSrcweir #include "windata.hxx"
66*cdf0e10cSrcweir using namespace cppu;
67*cdf0e10cSrcweir using namespace rtl;
68*cdf0e10cSrcweir using namespace std;
69*cdf0e10cSrcweir using namespace com::sun::star::lang;
70*cdf0e10cSrcweir using namespace com::sun::star::bridge;
71*cdf0e10cSrcweir using namespace com::sun::star::bridge::oleautomation;
72*cdf0e10cSrcweir 
73*cdf0e10cSrcweir namespace ole_adapter
74*cdf0e10cSrcweir {
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir typedef hash_map<OUString, pair<DISPID, unsigned short>, hashOUString_Impl, equalOUString_Impl> DispIdMap;
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir typedef hash_multimap<OUString, unsigned int, hashOUString_Impl, equalOUString_Impl> TLBFuncIndexMap;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir // This class wraps an IDispatch and maps XInvocation calls to IDispatch calls on the wrapped object.
83*cdf0e10cSrcweir // If m_TypeDescription is set then this class represents an UNO interface implemented in a COM component.
84*cdf0e10cSrcweir // The interface is not a real interface in terms of an abstract class but is realized through IDispatch.
85*cdf0e10cSrcweir class IUnknownWrapper_Impl : public WeakImplHelper7< XInvocation, XBridgeSupplier2, XInitialization, XAutomationObject, XDefaultProperty, XDefaultMethod, XDirectInvocation >,
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 							 public UnoConversionUtilities<IUnknownWrapper_Impl>
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir {
90*cdf0e10cSrcweir public:
91*cdf0e10cSrcweir 	IUnknownWrapper_Impl(Reference<XMultiServiceFactory> &xFactory,
92*cdf0e10cSrcweir                          sal_uInt8 unoWrapperClass, sal_uInt8 comWrapperClass);
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	~IUnknownWrapper_Impl();
95*cdf0e10cSrcweir 
96*cdf0e10cSrcweir     //XInterface
97*cdf0e10cSrcweir 	Any SAL_CALL queryInterface(const Type& t)
98*cdf0e10cSrcweir         throw (RuntimeException);
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 	// XInvokation
101*cdf0e10cSrcweir 	virtual Reference< XIntrospectionAccess > SAL_CALL getIntrospection(  )
102*cdf0e10cSrcweir         throw(RuntimeException);
103*cdf0e10cSrcweir     virtual Any SAL_CALL invoke( const OUString& aFunctionName,
104*cdf0e10cSrcweir                                  const Sequence< Any >& aParams,
105*cdf0e10cSrcweir                                  Sequence< sal_Int16 >& aOutParamIndex,
106*cdf0e10cSrcweir                                  Sequence< Any >& aOutParam )
107*cdf0e10cSrcweir         throw(IllegalArgumentException, CannotConvertException,
108*cdf0e10cSrcweir               InvocationTargetException, RuntimeException);
109*cdf0e10cSrcweir     virtual void SAL_CALL setValue( const OUString& aPropertyName,
110*cdf0e10cSrcweir                                     const Any& aValue )
111*cdf0e10cSrcweir         throw(UnknownPropertyException, CannotConvertException,
112*cdf0e10cSrcweir               InvocationTargetException, RuntimeException);
113*cdf0e10cSrcweir     virtual Any SAL_CALL getValue( const OUString& aPropertyName )
114*cdf0e10cSrcweir         throw(UnknownPropertyException, RuntimeException);
115*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasMethod( const OUString& aName )
116*cdf0e10cSrcweir         throw(RuntimeException);
117*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL hasProperty( const OUString& aName )
118*cdf0e10cSrcweir         throw(RuntimeException);
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir 	// XBridgeSupplier2
121*cdf0e10cSrcweir 	// This interface is implemented to provide a safe way to obtain the original
122*cdf0e10cSrcweir 	// IUnknown or IDispatch within the function anyToVariant. The function asks
123*cdf0e10cSrcweir 	// every UNO object for its XBridgeSupplier2 and if it is available uses it to convert
124*cdf0e10cSrcweir 	// the object with its own supplier.
125*cdf0e10cSrcweir 	virtual Any SAL_CALL createBridge( const Any& modelDepObject,
126*cdf0e10cSrcweir                                        const Sequence< sal_Int8 >& aProcessId,
127*cdf0e10cSrcweir                                        sal_Int16 sourceModelType,
128*cdf0e10cSrcweir                                        sal_Int16 destModelType )
129*cdf0e10cSrcweir         throw(IllegalArgumentException, RuntimeException);
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir 	// XInitialization
132*cdf0e10cSrcweir     virtual void SAL_CALL initialize( const Sequence< Any >& aArguments )
133*cdf0e10cSrcweir         throw(Exception, RuntimeException);
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     // XDefaultProperty
136*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getDefaultPropertyName(  ) throw (::com::sun::star::uno::RuntimeException) { return m_sDefaultMember; }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir     // XDefaultMethod
139*cdf0e10cSrcweir     virtual ::rtl::OUString SAL_CALL getDefaultMethodName(  ) throw (::com::sun::star::uno::RuntimeException) { return m_sDefaultMember; }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir     // XDirectInvocation
142*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Any SAL_CALL directInvoke( const ::rtl::OUString& aName, const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aParams ) throw (::com::sun::star::lang::IllegalArgumentException, ::com::sun::star::script::CannotConvertException, ::com::sun::star::reflection::InvocationTargetException, ::com::sun::star::uno::RuntimeException);
143*cdf0e10cSrcweir     virtual ::sal_Bool SAL_CALL hasMember( const ::rtl::OUString& aName ) throw (::com::sun::star::uno::RuntimeException);
144*cdf0e10cSrcweir 
145*cdf0e10cSrcweir protected:
146*cdf0e10cSrcweir 	// ----------------------------------------------------------------------------
147*cdf0e10cSrcweir     virtual Any invokeWithDispIdUnoTlb(const OUString& sFunctionName,
148*cdf0e10cSrcweir                                        const Sequence< Any >& Params,
149*cdf0e10cSrcweir                                        Sequence<sal_Int16 >& OutParamIndex,
150*cdf0e10cSrcweir                                        Sequence< Any >& OutParam);
151*cdf0e10cSrcweir 	// Is used for OleObjectFactory service
152*cdf0e10cSrcweir 	virtual Any invokeWithDispIdComTlb(const OUString& sFuncName,
153*cdf0e10cSrcweir                                        const Sequence< Any >& Params,
154*cdf0e10cSrcweir                                        Sequence< sal_Int16 >& OutParamIndex,
155*cdf0e10cSrcweir                                        Sequence< Any >& OutParam);
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir //    virtual void setValueWithDispId(DISPID dispID, const Any& Value);
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir //    virtual Any getValueWithDispId(const OUString& sName, DISPID dispID);
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 
162*cdf0e10cSrcweir 	// UnoConversionUtilities -------------------------------------------------------------------------------
163*cdf0e10cSrcweir 	virtual	Reference<XInterface> createUnoWrapperInstance();
164*cdf0e10cSrcweir 	virtual Reference<XInterface> createComWrapperInstance();
165*cdf0e10cSrcweir 
166*cdf0e10cSrcweir     /**Obtains a FUNCDESC structure for a function.
167*cdf0e10cSrcweir        Fills the FUNCDESC structure if ITypeInfo provides information for
168*cdf0e10cSrcweir        the function of name sFuncName or pFuncDesc will not be filled in.
169*cdf0e10cSrcweir        May throw a BridgeRuntimeError.
170*cdf0e10cSrcweir      */
171*cdf0e10cSrcweir     void getFuncDesc(const OUString & sFuncName, FUNCDESC ** pFuncDesc);
172*cdf0e10cSrcweir     /**Obtains a FUNCDESC structures or a VARDESC structure
173*cdf0e10cSrcweir        for a property. pFuncDescPut may also contain
174*cdf0e10cSrcweir        a structure for a "propertyputref" operation. If pFuncDesc contains a
175*cdf0e10cSrcweir        "put ref" or "put" FUNCDESC depends on what was found first in the type
176*cdf0e10cSrcweir        description.
177*cdf0e10cSrcweir        Fills the FUNCDESC structure if ITypeInfo provides information for
178*cdf0e10cSrcweir        the respective property functions or the structures will not be filled in.
179*cdf0e10cSrcweir        May throw a BridgeRuntimeError.
180*cdf0e10cSrcweir      */
181*cdf0e10cSrcweir     void getPropDesc(const OUString & sFuncName, FUNCDESC ** pFuncDescGet,
182*cdf0e10cSrcweir                      FUNCDESC** pFuncDescPut, VARDESC ** pVarDesc);
183*cdf0e10cSrcweir 	// These functions are for the case if an object of this class wraps an IDispatch
184*cdf0e10cSrcweir 	// object that implements UNO interfaces. In that case the member m_seqTypes
185*cdf0e10cSrcweir 	// is set through XInitialization::initialize.
186*cdf0e10cSrcweir 	void getMethodInfo(const OUString& sName, TypeDescription& methodDescription);
187*cdf0e10cSrcweir 	// After return attributInfo contains typelib_InterfaceAttributeTypeDescription::pAttributeTypeRef
188*cdf0e10cSrcweir 	void getAttributeInfo(const OUString& sName, TypeDescription& attributeInfo);
189*cdf0e10cSrcweir 	// used by get MethodInfo
190*cdf0e10cSrcweir 	TypeDescription	 getInterfaceMemberDescOfCurrentCall(const OUString& sName);
191*cdf0e10cSrcweir     /** Returns alway a valid ITypeInfo interface or throws a BridgeRuntimeError.
192*cdf0e10cSrcweir         The returned interface does not need to be AddRef'ed as long as it is locally
193*cdf0e10cSrcweir         used. The interface is kept in the instance of this class.
194*cdf0e10cSrcweir      */
195*cdf0e10cSrcweir 	ITypeInfo*	getTypeInfo();
196*cdf0e10cSrcweir 
197*cdf0e10cSrcweir 	/** Returns the DISPID for a function or property name. If true is returned then
198*cdf0e10cSrcweir 		id contains a valid DISPID.
199*cdf0e10cSrcweir 	*/
200*cdf0e10cSrcweir 	bool getDispid(const OUString& sFuncName, DISPID * id);
201*cdf0e10cSrcweir 
202*cdf0e10cSrcweir 	/** Gets the element type in a VARIANT like style. E.g. if desc->lptdesc contains
203*cdf0e10cSrcweir 		a VT_PTR than it is replaced by VT_BYREF and VT_SAFEARRAY is replaced by VT_ARRAY
204*cdf0e10cSrcweir 		If the TYPEDESC describes an SAFEARRAY then varType is a combination of VT_ARRAY
205*cdf0e10cSrcweir 		and the element type.
206*cdf0e10cSrcweir 		The argument desc must be obtained from FUNCDESC::lprgelemdescParam[i].tdesc where
207*cdf0e10cSrcweir 		FUNCDESC was obtained from the ITypeInfo belonging to wrapped IDispatch.
208*cdf0e10cSrcweir 	*/
209*cdf0e10cSrcweir 	VARTYPE getElementTypeDesc( const TYPEDESC *desc);
210*cdf0e10cSrcweir 	/** Iterates over all functions and put the names and indices into the map
211*cdf0e10cSrcweir         m_mapComFunc of type TLBFuncIndexMap.
212*cdf0e10cSrcweir         Call the function every time before accessing the map.
213*cdf0e10cSrcweir         Throws a BridgeRuntimeError on failure.
214*cdf0e10cSrcweir     */
215*cdf0e10cSrcweir 	void  buildComTlbIndex();
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     /** Returns a FUNCDESC structure which contains type information about the
218*cdf0e10cSrcweir         current XInvocation::invoke call. The FUNCDESC either describes a method,
219*cdf0e10cSrcweir         a property put or a property get operation.
220*cdf0e10cSrcweir         It uses the types  com.sun.star.bridge.oleautomation.PropertyPutArgument
221*cdf0e10cSrcweir         which can be
222*cdf0e10cSrcweir         contained in the sequence of in-arguments of invoke to determine if the call is
223*cdf0e10cSrcweir         a property put or property get operation.
224*cdf0e10cSrcweir         If no adequate FUNCDESC was found, an IllegalArgumentException is thrown.
225*cdf0e10cSrcweir         Therefore it is safe to assume that the returned FUNCDESC* is not NULL.
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir         @exception IllegalArgumentException
228*cdf0e10cSrcweir         Thrown if no adequate FUNCDESC could be found.
229*cdf0e10cSrcweir     */
230*cdf0e10cSrcweir     void getFuncDescForInvoke(const OUString & sFuncName,
231*cdf0e10cSrcweir                               const Sequence<Any> & seqArgs, FUNCDESC** pFuncDesc);
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     // Finds out wheter the wrapped IDispatch is an JScript Object. This is is
234*cdf0e10cSrcweir 	// done by
235*cdf0e10cSrcweir 	// asking for the property "_environment". If it has the value "JScript"
236*cdf0e10cSrcweir 	// (case insensitive) then the IDispatch is considered a JScript object.
237*cdf0e10cSrcweir 	sal_Bool isJScriptObject();
238*cdf0e10cSrcweir 	// -------------------------------------------------------------------------------
239*cdf0e10cSrcweir 
240*cdf0e10cSrcweir 	// If UNO interfaces are implemented in JScript objects, VB or C++ COM objects
241*cdf0e10cSrcweir 	// and those are passed as parameter to a UNO interface function, then
242*cdf0e10cSrcweir 	// the IDispatch* are wrapped by objects of this class. Assuming that the functions
243*cdf0e10cSrcweir 	// implemented by the IDispatch object returns another UNO interface then
244*cdf0e10cSrcweir 	// it has to be wrapped to this type. But this is only possible if an object of this
245*cdf0e10cSrcweir 	// wrapper class knows what type it is represting. The member m_TypeDescription holds this
246*cdf0e10cSrcweir 	// information.
247*cdf0e10cSrcweir 	// m_TypeDescription is only useful when an object wraps an IDispatch object that implements
248*cdf0e10cSrcweir 	// an UNO interface. The value is set during a call to XInitialization::initialize.
249*cdf0e10cSrcweir 	Sequence<Type> m_seqTypes;
250*cdf0e10cSrcweir 	CComPtr<IUnknown> m_spUnknown;
251*cdf0e10cSrcweir 	CComPtr<IDispatch> m_spDispatch;
252*cdf0e10cSrcweir         rtl::OUString m_sTypeName; // is "" ( not initialised ), "IDispatch" ( we have no idea ) or "SomeLibrary.SomeTypeName" if we managed to get a type
253*cdf0e10cSrcweir     /** This value is set dureing XInitialization::initialize. It indicates that the COM interface
254*cdf0e10cSrcweir     was transported as VT_DISPATCH in a VARIANT rather then a VT_UNKNOWN
255*cdf0e10cSrcweir     */
256*cdf0e10cSrcweir     sal_Bool  m_bOriginalDispatch;
257*cdf0e10cSrcweir 	DispIdMap 			m_dispIdMap;
258*cdf0e10cSrcweir 	Reference<XIdlClass>*		m_pxIdlClass;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 
261*cdf0e10cSrcweir 	// used by isJScriptObject
262*cdf0e10cSrcweir 	enum JScriptDetermination{ JScriptUndefined=0, NoJScript, IsJScript};
263*cdf0e10cSrcweir 	JScriptDetermination m_eJScript;
264*cdf0e10cSrcweir 	// The map is filled by buildComTlbIndex
265*cdf0e10cSrcweir     // It maps Uno Function names to an index which is used in ITypeInfo::GetFuncDesc
266*cdf0e10cSrcweir 	TLBFuncIndexMap m_mapComFunc;
267*cdf0e10cSrcweir     // used for synchroizing the computation of the content for m_mapComFunc
268*cdf0e10cSrcweir     bool m_bComTlbIndexInit;
269*cdf0e10cSrcweir 	// Keeps the ITypeInfo obtained from IDispatch::GetTypeInfo
270*cdf0e10cSrcweir 	CComPtr< ITypeInfo > m_spTypeInfo;
271*cdf0e10cSrcweir     rtl::OUString m_sDefaultMember;
272*cdf0e10cSrcweir     bool m_bHasDfltMethod;
273*cdf0e10cSrcweir     bool m_bHasDfltProperty;
274*cdf0e10cSrcweir };
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir } // end namespace
277*cdf0e10cSrcweir #endif
278*cdf0e10cSrcweir 
279