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