1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "ole2uno.hxx"
21 #include <sal/log.hxx>
22 #include <o3tl/char16_t2wchar_t.hxx>
24 #include <osl/diagnose.h>
25 #include <osl/doublecheckedlocking.h>
26 #include <osl/thread.h>
29 #include <com/sun/star/script/CannotConvertException.hpp>
30 #include <com/sun/star/script/FailReason.hpp>
31 #include <com/sun/star/beans/XMaterialHolder.hpp>
32 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
33 #include <com/sun/star/script/XInvocation.hpp>
34 #include <com/sun/star/bridge/ModelDependent.hpp>
36 #include <com/sun/star/bridge/oleautomation/NamedArgument.hpp>
37 #include <com/sun/star/bridge/oleautomation/PropertyPutArgument.hpp>
38 #include <cppuhelper/exc_hlp.hxx>
40 #include <typelib/typedescription.hxx>
42 #include <rtl/ustring.hxx>
44 #include "jscriptclasses.hxx"
46 #include "oleobjw.hxx"
47 #include "unoobjw.hxx"
52 using namespace com::sun::star::script
;
53 using namespace com::sun::star::lang
;
54 using namespace com::sun::star::bridge
;
55 using namespace com::sun::star::bridge::oleautomation
;
56 using namespace com::sun::star::bridge::ModelDependent
;
57 using namespace ::com::sun::star
;
60 #define JSCRIPT_ID_PROPERTY L"_environment"
61 #define JSCRIPT_ID L"jscript"
63 // key: XInterface pointer created by Invocation Adapter Factory
64 // value: XInterface pointer to the wrapper class.
65 // Entries to the map are made within
66 // Any createOleObjectWrapper(IUnknown* pUnknown, const Type& aType);
67 // Entries are being deleted if the wrapper class's destructor has been
69 // Before UNO object is wrapped to COM object this map is checked
70 // to see if the UNO object is already a wrapper.
71 std::unordered_map
<sal_uIntPtr
, sal_uIntPtr
> AdapterToWrapperMap
;
72 // key: XInterface of the wrapper object.
73 // value: XInterface of the Interface created by the Invocation Adapter Factory.
74 // A COM wrapper is responsible for removing the corresponding entry
75 // in AdapterToWrapperMap if it is being destroyed. Because the wrapper does not
76 // know about its adapted interface it uses WrapperToAdapterMap to get the
77 // adapted interface which is then used to locate the entry in AdapterToWrapperMap.
78 std::unordered_map
<sal_uIntPtr
,sal_uIntPtr
> WrapperToAdapterMap
;
80 std::unordered_map
<sal_uIntPtr
, WeakReference
<XInterface
> > ComPtrToWrapperMap
;
82 IUnknownWrapper::IUnknownWrapper( Reference
<XMultiServiceFactory
> const & xFactory
,
83 sal_uInt8 unoWrapperClass
, sal_uInt8 comWrapperClass
):
84 UnoConversionUtilities
<IUnknownWrapper
>( xFactory
, unoWrapperClass
, comWrapperClass
),
85 m_pxIdlClass( nullptr), m_eJScript( JScriptUndefined
),
86 m_bComTlbIndexInit(false), m_bHasDfltMethod(false), m_bHasDfltProperty(false)
91 IUnknownWrapper::~IUnknownWrapper()
93 o2u_attachCurrentThread();
94 MutexGuard
guard(getBridgeMutex());
95 XInterface
* xIntRoot
= static_cast<OWeakObject
*>(this);
96 #if OSL_DEBUG_LEVEL > 0
97 acquire(); // make sure we don't delete us twice because of Reference
98 OSL_ASSERT( Reference
<XInterface
>( static_cast<XWeak
*>(this), UNO_QUERY
).get() == xIntRoot
);
101 // remove entries in global maps
102 auto it
= WrapperToAdapterMap
.find( reinterpret_cast<sal_uIntPtr
>(xIntRoot
));
103 if( it
!= WrapperToAdapterMap
.end())
105 sal_uIntPtr adapter
= it
->second
;
107 AdapterToWrapperMap
.erase( adapter
);
108 WrapperToAdapterMap
.erase( it
);
111 auto it_c
= ComPtrToWrapperMap
.find( reinterpret_cast<sal_uIntPtr
>(m_spUnknown
.p
));
112 if(it_c
!= ComPtrToWrapperMap
.end())
113 ComPtrToWrapperMap
.erase(it_c
);
116 Any
IUnknownWrapper::queryInterface(const Type
& t
)
118 if (t
== cppu::UnoType
<XDefaultMethod
>::get() && !m_bHasDfltMethod
)
120 if (t
== cppu::UnoType
<XDefaultProperty
>::get() && !m_bHasDfltProperty
)
122 if ( ( t
== cppu::UnoType
<XInvocation
>::get() || t
== cppu::UnoType
<XAutomationInvocation
>::get() ) && !m_spDispatch
)
124 // XDirectInvocation seems to be an oracle replacement for XAutomationInvocation, however it is flawed especially wrt. assumptions about whether to invoke a
125 // Put or Get property, the implementation code has no business guessing that, it's up to the caller to decide that. Worse XDirectInvocation duplicates lots of code.
126 // XAutomationInvocation provides separate calls for put& get
127 // properties. Note: Currently the basic runtime doesn't call put properties directly, it should... after all the basic runtime should know whether it is calling a put or get property.
128 // For the moment for ease of merging we will let the XDirectInvoke and XAuthomationInvocation interfaces stay side by side (and for the moment at least I would prefer the basic
129 // runtime to call XAutomationInvocation instead of XDirectInvoke
130 return WeakImplHelper
<XBridgeSupplier2
,
131 XInitialization
, XAutomationObject
, XDefaultProperty
, XDefaultMethod
, XDirectInvocation
, XAutomationInvocation
>::queryInterface(t
);
134 Reference
<XIntrospectionAccess
> SAL_CALL
IUnknownWrapper::getIntrospection()
136 Reference
<XIntrospectionAccess
> ret
;
141 Any SAL_CALL
IUnknownWrapper::invokeGetProperty( const OUString
& aPropertyName
, const Sequence
< Any
>& aParams
, Sequence
< sal_Int16
>& aOutParamIndex
, Sequence
< Any
>& aOutParam
)
146 o2u_attachCurrentThread();
147 ITypeInfo
* pInfo
= getTypeInfo();
148 FuncDesc
aDescGet(pInfo
);
149 FuncDesc
aDescPut(pInfo
);
150 VarDesc
aVarDesc(pInfo
);
151 getPropDesc(aPropertyName
, & aDescGet
, & aDescPut
, & aVarDesc
);
154 OUString
msg("[automation bridge]Property \"" + aPropertyName
+
155 "\" is not supported");
156 throw UnknownPropertyException(msg
);
158 aResult
= invokeWithDispIdComTlb( aDescGet
, aPropertyName
, aParams
, aOutParamIndex
, aOutParam
);
160 catch ( const Exception
& e
)
162 css::uno::Any anyEx
= cppu::getCaughtException();
163 throw css::lang::WrappedTargetRuntimeException("[automation bridge] unexpected exception in "
164 "IUnknownWrapper::invokeGetProperty ! Message : \n " +
171 Any SAL_CALL
IUnknownWrapper::invokePutProperty( const OUString
& aPropertyName
, const Sequence
< Any
>& aParams
, Sequence
< sal_Int16
>& aOutParamIndex
, Sequence
< Any
>& aOutParam
)
176 o2u_attachCurrentThread();
177 ITypeInfo
* pInfo
= getTypeInfo();
178 FuncDesc
aDescGet(pInfo
);
179 FuncDesc
aDescPut(pInfo
);
180 VarDesc
aVarDesc(pInfo
);
181 getPropDesc(aPropertyName
, & aDescGet
, & aDescPut
, & aVarDesc
);
184 OUString
msg("[automation bridge]Property \"" + aPropertyName
+
185 "\" is not supported");
186 throw UnknownPropertyException(msg
);
188 aResult
= invokeWithDispIdComTlb( aDescPut
, aPropertyName
, aParams
, aOutParamIndex
, aOutParam
);
190 catch ( const Exception
& e
)
192 css::uno::Any anyEx
= cppu::getCaughtException();
193 throw css::lang::WrappedTargetRuntimeException("[automation bridge] unexpected exception in "
194 "IUnknownWrapper::invokePutProperty ! Message : \n" +
202 Any SAL_CALL
IUnknownWrapper::invoke( const OUString
& aFunctionName
,
203 const Sequence
< Any
>& aParams
, Sequence
< sal_Int16
>& aOutParamIndex
,
204 Sequence
< Any
>& aOutParam
)
206 if ( ! m_spDispatch
)
208 throw RuntimeException(
209 "[automation bridge] The object does not have an IDispatch interface");
216 o2u_attachCurrentThread();
218 TypeDescription methodDesc
;
219 getMethodInfo(aFunctionName
, methodDesc
);
222 ret
= invokeWithDispIdUnoTlb(aFunctionName
,
229 ret
= invokeWithDispIdComTlb( aFunctionName
,
235 catch (const IllegalArgumentException
&)
239 catch (const CannotConvertException
&)
243 catch (const BridgeRuntimeError
& e
)
245 throw RuntimeException(e
.message
);
247 catch (const Exception
& e
)
249 css::uno::Any anyEx
= cppu::getCaughtException();
250 throw css::lang::WrappedTargetRuntimeException("[automation bridge] unexpected exception in "
251 "IUnknownWrapper::invoke ! Message : \n" +
258 throw RuntimeException("[automation bridge] unexpected exception in "
259 "IUnknownWrapper::Invoke !");
264 void SAL_CALL
IUnknownWrapper::setValue( const OUString
& aPropertyName
,
267 if ( ! m_spDispatch
)
269 throw RuntimeException(
270 "[automation bridge] The object does not have an IDispatch interface");
274 o2u_attachCurrentThread();
276 ITypeInfo
* pInfo
= getTypeInfo();
277 FuncDesc
aDescGet(pInfo
);
278 FuncDesc
aDescPut(pInfo
);
279 VarDesc
aVarDesc(pInfo
);
280 getPropDesc(aPropertyName
, & aDescGet
, & aDescPut
, & aVarDesc
);
281 //check if there is such a property at all or if it is read only
282 if ( ! aDescPut
&& ! aDescGet
&& ! aVarDesc
)
284 OUString
msg("[automation bridge]Property \"" + aPropertyName
+
285 "\" is not supported");
286 throw UnknownPropertyException(msg
);
289 if ( (! aDescPut
&& aDescGet
)
290 || (aVarDesc
&& aVarDesc
->wVarFlags
== VARFLAG_FREADONLY
) )
293 SAL_WARN( "extensions.olebridge", "[automation bridge] Property " << aPropertyName
<< " is read-only");
299 DISPPARAMS dispparams
;
301 CComVariant varRefArg
;
302 CComVariant varResult
;
304 unsigned int uArgErr
;
306 // converting UNO value to OLE variant
307 DISPID dispidPut
= DISPID_PROPERTYPUT
;
308 dispparams
.rgdispidNamedArgs
= &dispidPut
;
309 dispparams
.cArgs
= 1;
310 dispparams
.cNamedArgs
= 1;
311 dispparams
.rgvarg
= & varArg
;
313 OSL_ASSERT(aDescPut
|| aVarDesc
);
317 INVOKEKIND invkind
= INVOKE_PROPERTYPUT
;
318 //determine the expected type, dispid, invoke kind (DISPATCH_PROPERTYPUT,
319 //DISPATCH_PROPERTYPUTREF)
322 vt
= getElementTypeDesc(& aDescPut
->lprgelemdescParam
[0].tdesc
);
323 dispid
= aDescPut
->memid
;
324 invkind
= aDescPut
->invkind
;
328 vt
= getElementTypeDesc( & aVarDesc
->elemdescVar
.tdesc
);
329 dispid
= aVarDesc
->memid
;
330 if (vt
== VT_UNKNOWN
|| vt
== VT_DISPATCH
||
331 (vt
& VT_ARRAY
) || (vt
& VT_BYREF
))
333 invkind
= INVOKE_PROPERTYPUTREF
;
337 // convert the uno argument
340 anyToVariant( & varRefArg
, aValue
, ::sal::static_int_cast
< VARTYPE
, int >( vt
^ VT_BYREF
) );
342 if( (vt
& VT_TYPEMASK
) == VT_VARIANT
)
343 varArg
.byref
= & varRefArg
;
344 else if ((vt
& VT_TYPEMASK
) == VT_DECIMAL
)
345 varArg
.byref
= & varRefArg
.decVal
;
347 varArg
.byref
= & varRefArg
.byref
;
351 anyToVariant(& varArg
, aValue
, vt
);
354 hr
= m_spDispatch
->Invoke(dispid
, IID_NULL
, LOCALE_USER_DEFAULT
, ::sal::static_int_cast
< WORD
, INVOKEKIND
>( invkind
),
355 &dispparams
, & varResult
, & excepinfo
, &uArgErr
);
362 case DISP_E_BADPARAMCOUNT
:
363 throw RuntimeException();
365 case DISP_E_BADVARTYPE
:
366 throw RuntimeException();
368 case DISP_E_EXCEPTION
:
369 throw InvocationTargetException();
371 case DISP_E_MEMBERNOTFOUND
:
372 throw UnknownPropertyException();
374 case DISP_E_NONAMEDARGS
:
375 throw RuntimeException();
377 case DISP_E_OVERFLOW
:
378 throw CannotConvertException("call to OLE object failed", static_cast<XInterface
*>(
379 static_cast<XWeak
*>(this)), TypeClass_UNKNOWN
, FailReason::OUT_OF_RANGE
, uArgErr
);
381 case DISP_E_PARAMNOTFOUND
:
382 throw IllegalArgumentException("call to OLE object failed", static_cast<XInterface
*>(
383 static_cast<XWeak
*>(this)), ::sal::static_int_cast
< sal_Int16
, unsigned int >( uArgErr
)) ;
385 case DISP_E_TYPEMISMATCH
:
386 throw CannotConvertException("call to OLE object failed", static_cast<XInterface
*>(
387 static_cast<XWeak
*>(this)), TypeClass_UNKNOWN
, FailReason::UNKNOWN
, ::sal::static_int_cast
< sal_Int16
, unsigned int >( uArgErr
));
389 case DISP_E_UNKNOWNINTERFACE
:
390 throw RuntimeException();
392 case DISP_E_UNKNOWNLCID
:
393 throw RuntimeException();
395 case DISP_E_PARAMNOTOPTIONAL
:
396 throw CannotConvertException("call to OLE object failed",static_cast<XInterface
*>(
397 static_cast<XWeak
*>(this)) , TypeClass_UNKNOWN
, FailReason::NO_DEFAULT_AVAILABLE
, uArgErr
);
400 throw RuntimeException();
404 catch (const CannotConvertException
&)
408 catch (const UnknownPropertyException
&)
412 catch (const BridgeRuntimeError
& e
)
414 throw RuntimeException(e
.message
);
416 catch (const Exception
& e
)
418 css::uno::Any anyEx
= cppu::getCaughtException();
419 throw css::lang::WrappedTargetRuntimeException("[automation bridge] unexpected exception in "
420 "IUnknownWrapper::setValue ! Message : \n" +
427 throw RuntimeException(
428 "[automation bridge] unexpected exception in "
429 "IUnknownWrapper::setValue !");
433 Any SAL_CALL
IUnknownWrapper::getValue( const OUString
& aPropertyName
)
435 if ( ! m_spDispatch
)
437 throw RuntimeException(
438 "[automation bridge] The object does not have an IDispatch interface");
443 o2u_attachCurrentThread();
444 ITypeInfo
* pInfo
= getTypeInfo();
445 // I was going to implement an XServiceInfo interface to allow the type
446 // of the automation object to be exposed... but it seems
447 // from looking at comments in the code that it is possible for
448 // this object to actually wrap a UNO object ( I guess if automation is
449 // used from MSO to create Openoffice objects ) Therefore, those objects
450 // will more than likely already have their own XServiceInfo interface.
451 // Instead here I chose a name that should be illegal both in COM and
452 // UNO ( from an IDL point of view ) therefore I think this is a safe
454 if ( aPropertyName
== "$GetTypeName" )
456 if ( pInfo
&& m_sTypeName
.getLength() == 0 )
458 m_sTypeName
= "IDispatch";
461 if ( SUCCEEDED( pInfo
->GetDocumentation( -1, &sName
, nullptr, nullptr, nullptr ) ) )
463 OUString
sTmp( o3tl::toU(LPCOLESTR(sName
)));
464 if ( sTmp
.startsWith("_") )
466 // do we own the memory for pTypeLib, msdn doc is vague
468 CComPtr
< ITypeLib
> pTypeLib
;
470 if ( SUCCEEDED( pInfo
->GetContainingTypeLib( &pTypeLib
.p
, &index
)) )
472 if ( SUCCEEDED( pTypeLib
->GetDocumentation( -1, &sName
, nullptr, nullptr, nullptr ) ) )
474 OUString
sLibName( o3tl::toU(LPCOLESTR(sName
)));
475 m_sTypeName
= sLibName
.concat( "." ).concat( sTmp
);
485 FuncDesc
aDescGet(pInfo
);
486 FuncDesc
aDescPut(pInfo
);
487 VarDesc
aVarDesc(pInfo
);
488 getPropDesc(aPropertyName
, & aDescGet
, & aDescPut
, & aVarDesc
);
489 if ( ! aDescGet
&& ! aDescPut
&& ! aVarDesc
)
492 OUString
msg("[automation bridge]Property \"" + aPropertyName
+
493 "\" is not supported");
494 throw UnknownPropertyException(msg
);
496 // write-only should not be possible
497 OSL_ASSERT( aDescGet
|| ! aDescPut
);
500 DISPPARAMS dispparams
= {nullptr, nullptr, 0, 0};
501 CComVariant varResult
;
503 unsigned int uArgErr
;
506 dispid
= aDescGet
->memid
;
508 dispid
= aVarDesc
->memid
;
510 dispid
= aDescPut
->memid
;
512 hr
= m_spDispatch
->Invoke(dispid
,
515 DISPATCH_PROPERTYGET
,
521 // converting return value and out parameter back to UNO
524 // If the com object implements uno interfaces then we have
525 // to convert the attribute into the expected type.
526 TypeDescription attrInfo
;
527 getAttributeInfo(aPropertyName
, attrInfo
);
529 variantToAny( &varResult
, ret
, Type( attrInfo
.get()->pWeakRef
));
531 variantToAny(&varResult
, ret
);
539 case DISP_E_BADPARAMCOUNT
:
540 case DISP_E_BADVARTYPE
:
541 case DISP_E_EXCEPTION
:
542 throw RuntimeException(OUString(o3tl::toU(excepinfo
.bstrDescription
)));
544 case DISP_E_MEMBERNOTFOUND
:
545 throw UnknownPropertyException(OUString(o3tl::toU(excepinfo
.bstrDescription
)));
548 throw RuntimeException(OUString(o3tl::toU(excepinfo
.bstrDescription
)));
552 catch ( const UnknownPropertyException
& )
556 catch (const BridgeRuntimeError
& e
)
558 throw RuntimeException(e
.message
);
560 catch (const Exception
& e
)
562 css::uno::Any anyEx
= cppu::getCaughtException();
563 throw css::lang::WrappedTargetRuntimeException("[automation bridge] unexpected exception in "
564 "IUnknownWrapper::getValue ! Message : \n" +
570 throw RuntimeException(
571 "[automation bridge] unexpected exception in "
572 "IUnknownWrapper::getValue !");
577 sal_Bool SAL_CALL
IUnknownWrapper::hasMethod( const OUString
& aName
)
579 if ( ! m_spDispatch
)
581 throw RuntimeException(
582 "[automation bridge] The object does not have an IDispatch interface");
588 o2u_attachCurrentThread();
589 ITypeInfo
* pInfo
= getTypeInfo();
590 FuncDesc
aDesc(pInfo
);
591 getFuncDesc(aName
, & aDesc
);
592 // Automation properties can have arguments. Those are treated as methods and
593 //are called through XInvocation::invoke.
596 FuncDesc
aDescGet(pInfo
);
597 FuncDesc
aDescPut(pInfo
);
598 VarDesc
aVarDesc(pInfo
);
599 getPropDesc( aName
, & aDescGet
, & aDescPut
, & aVarDesc
);
600 if ((aDescGet
&& aDescGet
->cParams
> 0)
601 || (aDescPut
&& aDescPut
->cParams
> 0))
607 catch (const BridgeRuntimeError
& e
)
609 throw RuntimeException(e
.message
);
611 catch (const Exception
& e
)
613 css::uno::Any anyEx
= cppu::getCaughtException();
614 throw css::lang::WrappedTargetRuntimeException("[automation bridge] unexpected exception in "
615 "IUnknownWrapper::hasMethod ! Message : \n" +
621 throw RuntimeException("[automation bridge] unexpected exception in "
622 "IUnknownWrapper::hasMethod !");
627 sal_Bool SAL_CALL
IUnknownWrapper::hasProperty( const OUString
& aName
)
629 if ( ! m_spDispatch
)
631 throw RuntimeException("[automation bridge] The object does not have an "
632 "IDispatch interface");
637 o2u_attachCurrentThread();
639 ITypeInfo
* pInfo
= getTypeInfo();
640 FuncDesc
aDescGet(pInfo
);
641 FuncDesc
aDescPut(pInfo
);
642 VarDesc
aVarDesc(pInfo
);
643 getPropDesc(aName
, & aDescGet
, & aDescPut
, & aVarDesc
);
645 // we should probably just check the func kind
646 // basic has been modified to handle properties ( 'get' ) props at
647 // least with parameters
648 // additionally you can call invoke(Get|Set)Property on the bridge
649 // you can determine if a property has parameter is hasMethod
650 // returns true for the name
658 catch (const BridgeRuntimeError
& e
)
660 throw RuntimeException(e
.message
);
662 catch (const Exception
& e
)
664 css::uno::Any anyEx
= cppu::getCaughtException();
665 throw css::lang::WrappedTargetRuntimeException("[automation bridge] unexpected exception in "
666 "IUnknownWrapper::hasProperty ! Message : \n" +
673 throw RuntimeException("[automation bridge] unexpected exception in "
674 "IUnknownWrapper::hasProperty !");
679 Any SAL_CALL
IUnknownWrapper::createBridge( const Any
& modelDepObject
,
680 const Sequence
< sal_Int8
>& /*aProcessId*/, sal_Int16 sourceModelType
,
681 sal_Int16 destModelType
)
684 o2u_attachCurrentThread();
687 (sourceModelType
== UNO
) &&
688 (destModelType
== OLE
) &&
689 (modelDepObject
.getValueTypeClass() == TypeClass_INTERFACE
)
692 Reference
<XInterface
> xInt( *static_cast<XInterface
* const *>(modelDepObject
.getValue()));
693 Reference
<XInterface
> xSelf( static_cast<OWeakObject
*>(this));
697 VARIANT
* pVariant
= static_cast<VARIANT
*>(CoTaskMemAlloc(sizeof(VARIANT
)));
699 VariantInit(pVariant
);
700 if (m_bOriginalDispatch
)
702 pVariant
->vt
= VT_DISPATCH
;
703 pVariant
->pdispVal
= m_spDispatch
;
704 pVariant
->pdispVal
->AddRef();
708 pVariant
->vt
= VT_UNKNOWN
;
709 pVariant
->punkVal
= m_spUnknown
;
710 pVariant
->punkVal
->AddRef();
713 ret
.setValue(static_cast<void*>(&pVariant
), cppu::UnoType
<sal_uIntPtr
>::get());
720 @exception IllegalArgumentException
721 @exception CannotConvertException
722 @exception InvocationTargetException
725 Any
IUnknownWrapper::invokeWithDispIdUnoTlb(const OUString
& sFunctionName
,
726 const Sequence
< Any
>& Params
,
727 Sequence
< sal_Int16
>& OutParamIndex
,
728 Sequence
< Any
>& OutParam
)
733 sal_Int32 parameterCount
= Params
.getLength();
734 sal_Int32 outParameterCount
= 0;
735 typelib_InterfaceMethodTypeDescription
* pMethod
= nullptr;
736 TypeDescription methodDesc
;
737 getMethodInfo(sFunctionName
, methodDesc
);
739 // We need to know whether the IDispatch is from a JScript object.
740 // Then out and in/out parameters have to be treated differently than
741 // with common COM objects.
742 bool bJScriptObject
= isJScriptObject();
743 std::unique_ptr
<CComVariant
[]> sarParams
;
744 std::unique_ptr
<CComVariant
[]> sarParamsRef
;
745 CComVariant
*pVarParams
= nullptr;
746 CComVariant
*pVarParamsRef
= nullptr;
751 pMethod
= reinterpret_cast<typelib_InterfaceMethodTypeDescription
*>(methodDesc
.get());
752 parameterCount
= pMethod
->nParams
;
753 // Create the Array for the array being passed in DISPPARAMS
754 // the array also contains the outparameter (but not the values)
755 if( pMethod
->nParams
> 0)
757 sarParams
.reset(new CComVariant
[ parameterCount
]);
758 pVarParams
= sarParams
.get();
761 // Create the Array for the out an in/out parameter. These values
762 // are referenced by the VT_BYREF VARIANTs in DISPPARAMS.
763 // We need to find out the number of out and in/out parameter.
764 for( sal_Int32 i
=0; i
< parameterCount
; i
++)
766 if( pMethod
->pParams
[i
].bOut
)
772 sarParamsRef
.reset(new CComVariant
[outParameterCount
]);
773 pVarParamsRef
= sarParamsRef
.get();
774 // build up the parameters for IDispatch::Invoke
775 sal_Int32 outParamIndex
=0;
779 for( i
= 0; i
< parameterCount
; i
++)
782 if( pMethod
->pParams
[i
].bIn
&& ! pMethod
->pParams
[i
].bOut
)
784 anyToVariant( &pVarParams
[parameterCount
- i
-1], Params
.getConstArray()[i
]);
786 // Out parameter + in/out parameter
787 else if( pMethod
->pParams
[i
].bOut
)
790 if(pMethod
->pParams
[i
].bIn
)
792 anyToVariant( & var
,Params
[i
]);
793 pVarParamsRef
[outParamIndex
] = var
;
796 switch( pMethod
->pParams
[i
].pTypeRef
->eTypeClass
)
798 case typelib_TypeClass_INTERFACE
:
799 case typelib_TypeClass_STRUCT
:
800 if( ! pMethod
->pParams
[i
].bIn
)
802 pVarParamsRef
[ outParamIndex
].vt
= VT_DISPATCH
;
803 pVarParamsRef
[ outParamIndex
].pdispVal
= nullptr;
805 pVarParams
[parameterCount
- i
-1].vt
= VT_DISPATCH
| VT_BYREF
;
806 pVarParams
[parameterCount
- i
-1].ppdispVal
= &pVarParamsRef
[outParamIndex
].pdispVal
;
808 case typelib_TypeClass_ENUM
:
809 case typelib_TypeClass_LONG
:
810 case typelib_TypeClass_UNSIGNED_LONG
:
811 if( ! pMethod
->pParams
[i
].bIn
)
813 pVarParamsRef
[ outParamIndex
].vt
= VT_I4
;
814 pVarParamsRef
[ outParamIndex
].lVal
= 0;
816 pVarParams
[parameterCount
- i
-1].vt
= VT_I4
| VT_BYREF
;
817 pVarParams
[parameterCount
- i
-1].plVal
= &pVarParamsRef
[outParamIndex
].lVal
;
819 case typelib_TypeClass_SEQUENCE
:
820 if( ! pMethod
->pParams
[i
].bIn
)
822 pVarParamsRef
[ outParamIndex
].vt
= VT_ARRAY
| VT_VARIANT
;
823 pVarParamsRef
[ outParamIndex
].parray
= nullptr;
825 pVarParams
[parameterCount
- i
-1].vt
= VT_ARRAY
| VT_BYREF
| VT_VARIANT
;
826 pVarParams
[parameterCount
- i
-1].pparray
= &pVarParamsRef
[outParamIndex
].parray
;
828 case typelib_TypeClass_ANY
:
829 if( ! pMethod
->pParams
[i
].bIn
)
831 pVarParamsRef
[ outParamIndex
].vt
= VT_EMPTY
;
832 pVarParamsRef
[ outParamIndex
].lVal
= 0;
834 pVarParams
[parameterCount
- i
-1].vt
= VT_VARIANT
| VT_BYREF
;
835 pVarParams
[parameterCount
- i
-1].pvarVal
= &pVarParamsRef
[outParamIndex
];
837 case typelib_TypeClass_BOOLEAN
:
838 if( ! pMethod
->pParams
[i
].bIn
)
840 pVarParamsRef
[ outParamIndex
].vt
= VT_BOOL
;
841 pVarParamsRef
[ outParamIndex
].boolVal
= 0;
843 pVarParams
[parameterCount
- i
-1].vt
= VT_BOOL
| VT_BYREF
;
844 pVarParams
[parameterCount
- i
-1].pboolVal
=
845 & pVarParamsRef
[outParamIndex
].boolVal
;
848 case typelib_TypeClass_STRING
:
849 if( ! pMethod
->pParams
[i
].bIn
)
851 pVarParamsRef
[ outParamIndex
].vt
= VT_BSTR
;
852 pVarParamsRef
[ outParamIndex
].bstrVal
= nullptr;
854 pVarParams
[parameterCount
- i
-1].vt
= VT_BSTR
| VT_BYREF
;
855 pVarParams
[parameterCount
- i
-1].pbstrVal
=
856 & pVarParamsRef
[outParamIndex
].bstrVal
;
859 case typelib_TypeClass_FLOAT
:
860 if( ! pMethod
->pParams
[i
].bIn
)
862 pVarParamsRef
[ outParamIndex
].vt
= VT_R4
;
863 pVarParamsRef
[ outParamIndex
].fltVal
= 0;
865 pVarParams
[parameterCount
- i
-1].vt
= VT_R4
| VT_BYREF
;
866 pVarParams
[parameterCount
- i
-1].pfltVal
=
867 & pVarParamsRef
[outParamIndex
].fltVal
;
869 case typelib_TypeClass_DOUBLE
:
870 if( ! pMethod
->pParams
[i
].bIn
)
872 pVarParamsRef
[ outParamIndex
].vt
= VT_R8
;
873 pVarParamsRef
[ outParamIndex
].dblVal
= 0;
875 pVarParams
[parameterCount
- i
-1].vt
= VT_R8
| VT_BYREF
;
876 pVarParams
[parameterCount
- i
-1].pdblVal
=
877 & pVarParamsRef
[outParamIndex
].dblVal
;
879 case typelib_TypeClass_BYTE
:
880 if( ! pMethod
->pParams
[i
].bIn
)
882 pVarParamsRef
[ outParamIndex
].vt
= VT_UI1
;
883 pVarParamsRef
[ outParamIndex
].bVal
= 0;
885 pVarParams
[parameterCount
- i
-1].vt
= VT_UI1
| VT_BYREF
;
886 pVarParams
[parameterCount
- i
-1].pbVal
=
887 & pVarParamsRef
[outParamIndex
].bVal
;
889 case typelib_TypeClass_CHAR
:
890 case typelib_TypeClass_SHORT
:
891 case typelib_TypeClass_UNSIGNED_SHORT
:
892 if( ! pMethod
->pParams
[i
].bIn
)
894 pVarParamsRef
[ outParamIndex
].vt
= VT_I2
;
895 pVarParamsRef
[ outParamIndex
].iVal
= 0;
897 pVarParams
[parameterCount
- i
-1].vt
= VT_I2
| VT_BYREF
;
898 pVarParams
[parameterCount
- i
-1].piVal
=
899 & pVarParamsRef
[outParamIndex
].iVal
;
903 if( ! pMethod
->pParams
[i
].bIn
)
905 pVarParamsRef
[ outParamIndex
].vt
= VT_EMPTY
;
906 pVarParamsRef
[ outParamIndex
].lVal
= 0;
908 pVarParams
[parameterCount
- i
-1].vt
= VT_VARIANT
| VT_BYREF
;
909 pVarParams
[parameterCount
- i
-1].pvarVal
=
910 & pVarParamsRef
[outParamIndex
];
916 catch (IllegalArgumentException
& e
)
918 e
.ArgumentPosition
= ::sal::static_int_cast
< sal_Int16
, int >( i
);
921 catch (CannotConvertException
& e
)
927 else // it is a JScriptObject
932 for( ; i
< parameterCount
; i
++)
935 if( pMethod
->pParams
[i
].bIn
&& ! pMethod
->pParams
[i
].bOut
)
937 anyToVariant( &pVarParams
[parameterCount
- i
-1], Params
.getConstArray()[i
]);
939 // Out parameter + in/out parameter
940 else if( pMethod
->pParams
[i
].bOut
)
942 CComObject
<JScriptOutParam
>* pParamObject
;
943 if( !SUCCEEDED( CComObject
<JScriptOutParam
>::CreateInstance( &pParamObject
)))
945 throw BridgeRuntimeError(
946 "[automation bridge]IUnknownWrapper::"
947 "invokeWithDispIdUnoTlb\n"
948 "Could not create out parameter at index: " +
949 OUString::number(static_cast<sal_Int32
>(i
)));
952 CComPtr
<IUnknown
> pUnk(pParamObject
->GetUnknown());
953 CComQIPtr
<IDispatch
> pDisp( pUnk
);
955 pVarParams
[ parameterCount
- i
-1].vt
= VT_DISPATCH
;
956 pVarParams
[ parameterCount
- i
-1].pdispVal
= pDisp
;
957 pVarParams
[ parameterCount
- i
-1].pdispVal
->AddRef();
958 // if the param is in/out then put the parameter on index 0
959 if( pMethod
->pParams
[i
].bIn
) // in / out
961 CComVariant varParam
;
962 anyToVariant( &varParam
, Params
.getConstArray()[i
]);
963 CComDispatchDriver
dispDriver( pDisp
);
964 if(FAILED( dispDriver
.PutPropertyByName( L
"0", &varParam
)))
965 throw BridgeRuntimeError(
966 "[automation bridge]IUnknownWrapper::"
967 "invokeWithDispIdUnoTlb\n"
968 "Could not set property \"0\" for the in/out "
975 catch (IllegalArgumentException
& e
)
977 e
.ArgumentPosition
= ::sal::static_int_cast
< sal_Int16
, int >( i
);
980 catch (CannotConvertException
& e
)
987 // No type description Available, that is we have to deal with a COM component,
988 // that does not implements UNO interfaces ( IDispatch based)
991 //We should not run into this block, because invokeWithDispIdComTlb should
992 //have been called instead.
997 CComVariant varResult
;
999 unsigned int uArgErr
;
1000 DISPPARAMS dispparams
= { pVarParams
, nullptr, static_cast<UINT
>(parameterCount
), 0};
1003 FuncDesc
aDesc(getTypeInfo());
1004 getFuncDesc(sFunctionName
, & aDesc
);
1005 // invoking OLE method
1006 hr
= m_spDispatch
->Invoke(aDesc
->memid
,
1008 LOCALE_USER_DEFAULT
,
1015 // converting return value and out parameter back to UNO
1018 if( outParameterCount
&& pMethod
)
1020 OutParamIndex
.realloc( outParameterCount
);
1021 OutParam
.realloc( outParameterCount
);
1022 sal_Int32 outIndex
=0;
1026 for( ; i
< parameterCount
; i
++)
1028 if( pMethod
->pParams
[i
].bOut
)
1030 OutParamIndex
[outIndex
]= static_cast<sal_Int16
>(i
);
1032 if( !bJScriptObject
)
1034 variantToAny( &pVarParamsRef
[outIndex
], outAny
,
1035 Type(pMethod
->pParams
[i
].pTypeRef
), false);
1036 OutParam
[outIndex
++]= outAny
;
1038 else //JScriptObject
1040 if( pVarParams
[i
].vt
== VT_DISPATCH
)
1042 CComDispatchDriver
pDisp( pVarParams
[i
].pdispVal
);
1046 if( SUCCEEDED( pDisp
.GetPropertyByName( L
"0", &varOut
)))
1048 variantToAny( &varOut
, outAny
,
1049 Type(pMethod
->pParams
[parameterCount
- 1 - i
].pTypeRef
), false);
1050 OutParam
[outParameterCount
- 1 - outIndex
++]= outAny
;
1062 if( !bConvRet
) break;
1065 catch(IllegalArgumentException
& e
)
1067 e
.ArgumentPosition
= ::sal::static_int_cast
< sal_Int16
, int >( i
);
1070 catch(CannotConvertException
& e
)
1072 e
.ArgumentIndex
= i
;
1076 // return value, no type information available
1082 variantToAny(&varResult
, ret
, Type( pMethod
->pReturnTypeRef
), false);
1084 variantToAny(&varResult
, ret
, false);
1086 catch (IllegalArgumentException
& e
)
1089 "[automation bridge]IUnknownWrapper::invokeWithDispIdUnoTlb\n"
1090 "Could not convert return value! \n Message: \n" + e
.Message
;
1093 catch (CannotConvertException
& e
)
1096 "[automation bridge]IUnknownWrapper::invokeWithDispIdUnoTlb\n"
1097 "Could not convert return value! \n Message: \n" + e
.Message
;
1103 if( !bConvRet
) // conversion of return or out parameter failed
1104 throw CannotConvertException("Call to COM object failed. Conversion of return or out value failed",
1105 Reference
<XInterface
>( static_cast<XWeak
*>(this), UNO_QUERY
), TypeClass_UNKNOWN
,
1106 FailReason::UNKNOWN
, 0);// lookup error code
1107 // conversion of return or out parameter failed
1112 case DISP_E_BADPARAMCOUNT
:
1113 throw IllegalArgumentException();
1115 case DISP_E_BADVARTYPE
:
1116 throw RuntimeException();
1118 case DISP_E_EXCEPTION
:
1119 throw InvocationTargetException();
1121 case DISP_E_MEMBERNOTFOUND
:
1122 throw IllegalArgumentException();
1124 case DISP_E_NONAMEDARGS
:
1125 throw IllegalArgumentException();
1127 case DISP_E_OVERFLOW
:
1128 throw CannotConvertException("call to OLE object failed", static_cast<XInterface
*>(
1129 static_cast<XWeak
*>(this)), TypeClass_UNKNOWN
, FailReason::OUT_OF_RANGE
, uArgErr
);
1131 case DISP_E_PARAMNOTFOUND
:
1132 throw IllegalArgumentException("call to OLE object failed", static_cast<XInterface
*>(
1133 static_cast<XWeak
*>(this)), ::sal::static_int_cast
< sal_Int16
, unsigned int >( uArgErr
));
1135 case DISP_E_TYPEMISMATCH
:
1136 throw CannotConvertException("call to OLE object failed",static_cast<XInterface
*>(
1137 static_cast<XWeak
*>(this)) , TypeClass_UNKNOWN
, FailReason::UNKNOWN
, uArgErr
);
1139 case DISP_E_UNKNOWNINTERFACE
:
1140 throw RuntimeException() ;
1142 case DISP_E_UNKNOWNLCID
:
1143 throw RuntimeException() ;
1145 case DISP_E_PARAMNOTOPTIONAL
:
1146 throw CannotConvertException("call to OLE object failed", static_cast<XInterface
*>(
1147 static_cast<XWeak
*>(this)), TypeClass_UNKNOWN
, FailReason::NO_DEFAULT_AVAILABLE
, uArgErr
);
1150 throw RuntimeException();
1159 void SAL_CALL
IUnknownWrapper::initialize( const Sequence
< Any
>& aArguments
)
1161 // 1.parameter is IUnknown
1162 // 2.parameter is a boolean which indicates if the COM pointer was an IUnknown or IDispatch
1163 // 3.parameter is a Sequence<Type>
1164 o2u_attachCurrentThread();
1165 OSL_ASSERT(aArguments
.getLength() == 3);
1167 m_spUnknown
= *static_cast<IUnknown
* const *>(aArguments
[0].getValue());
1168 m_spUnknown
.QueryInterface( & m_spDispatch
.p
);
1170 aArguments
[1] >>= m_bOriginalDispatch
;
1171 aArguments
[2] >>= m_seqTypes
;
1173 ITypeInfo
* pType
= nullptr;
1176 // a COM object implementation that has no TypeInfo is still a legal COM object;
1177 // such objects can at least be transported through UNO using the bridge
1178 // so we should allow to create wrappers for them as well
1179 pType
= getTypeInfo();
1181 catch( const BridgeRuntimeError
& )
1183 catch( const Exception
& )
1190 // Get Default member
1191 CComBSTR defaultMemberName
;
1192 if ( SUCCEEDED( pType
->GetDocumentation(0, &defaultMemberName
, nullptr, nullptr, nullptr ) ) )
1194 OUString
usName(o3tl::toU(LPCOLESTR(defaultMemberName
)));
1195 FuncDesc
aDescGet(pType
);
1196 FuncDesc
aDescPut(pType
);
1197 VarDesc
aVarDesc(pType
);
1198 // see if this is a property first ( more likely to be a property then a method )
1199 getPropDesc( usName
, & aDescGet
, & aDescPut
, & aVarDesc
);
1201 if ( !aDescGet
&& !aDescPut
)
1203 getFuncDesc( usName
, &aDescGet
);
1205 throw BridgeRuntimeError( "[automation bridge]IUnknownWrapper::initialize() Failed to get Function or Property desc. for " + usName
);
1207 // now for some funny heuristics to make basic understand what to do
1208 // a single aDescGet ( that doesn't take any params ) would be
1209 // a read only ( defaultmember ) property e.g. this object
1210 // should implement XDefaultProperty
1211 // a single aDescGet ( that *does* ) take params is basically a
1212 // default method e.g. implement XDefaultMethod
1214 // a DescPut ( I guess we only really support a default param with '1' param ) as a setValue ( but I guess we can leave it through, the object will fail if we don't get it right anyway )
1215 if ( aDescPut
|| ( aDescGet
&& aDescGet
->cParams
== 0 ) )
1216 m_bHasDfltProperty
= true;
1217 if ( aDescGet
->cParams
> 0 )
1218 m_bHasDfltMethod
= true;
1219 if ( m_bHasDfltProperty
|| m_bHasDfltMethod
)
1220 m_sDefaultMember
= usName
;
1223 catch ( const BridgeRuntimeError
& e
)
1225 throw RuntimeException( e
.message
);
1227 catch( const Exception
& e
)
1229 css::uno::Any anyEx
= cppu::getCaughtException();
1230 throw css::lang::WrappedTargetRuntimeException(
1231 "[automation bridge] unexpected exception in IUnknownWrapper::initialize() error message: \n" + e
.Message
,
1238 // XDirectInvocation
1239 uno::Any SAL_CALL
IUnknownWrapper::directInvoke( const OUString
& aName
, const uno::Sequence
< uno::Any
>& aParams
)
1243 if ( !m_spDispatch
)
1245 throw RuntimeException(
1246 "[automation bridge] The object does not have an IDispatch interface");
1249 o2u_attachCurrentThread();
1251 if ( !getDispid( aName
, &dispid
) )
1252 throw IllegalArgumentException(
1253 "[automation bridge] The object does not have a function or property "
1254 + aName
, Reference
<XInterface
>(), 0);
1256 CComVariant varResult
;
1257 ExcepInfo excepinfo
;
1258 unsigned int uArgErr
= 0;
1259 INVOKEKIND pInvkinds
[2];
1260 pInvkinds
[0] = INVOKE_FUNC
;
1261 pInvkinds
[1] = aParams
.getLength() ? INVOKE_PROPERTYPUT
: INVOKE_PROPERTYGET
;
1262 HRESULT hInvRes
= E_FAIL
;
1264 // try Invoke first, if it does not work, try put/get property
1265 for ( sal_Int32 nStep
= 0; FAILED( hInvRes
) && nStep
< 2; nStep
++ )
1267 DISPPARAMS dispparams
= {nullptr, nullptr, 0, 0};
1269 std::unique_ptr
<DISPID
[]> arDispidNamedArgs
;
1270 std::unique_ptr
<CComVariant
[]> ptrArgs
;
1271 std::unique_ptr
<CComVariant
[]> ptrRefArgs
; // referenced arguments
1272 CComVariant
* arArgs
= nullptr;
1273 CComVariant
* arRefArgs
= nullptr;
1275 dispparams
.cArgs
= aParams
.getLength();
1277 // Determine the number of named arguments
1278 for ( sal_Int32 nInd
= 0; nInd
< aParams
.getLength(); nInd
++ )
1279 if ( aParams
[nInd
].getValueType() == cppu::UnoType
<NamedArgument
>::get() )
1280 dispparams
.cNamedArgs
++;
1282 // fill the named arguments
1283 if ( dispparams
.cNamedArgs
> 0
1284 && !( dispparams
.cNamedArgs
== 1 && pInvkinds
[nStep
] == INVOKE_PROPERTYPUT
) )
1286 int nSizeAr
= dispparams
.cNamedArgs
+ 1;
1287 if ( pInvkinds
[nStep
] == INVOKE_PROPERTYPUT
)
1288 nSizeAr
= dispparams
.cNamedArgs
;
1290 std::unique_ptr
<OLECHAR
*[]> saNames(new OLECHAR
*[nSizeAr
]);
1291 OLECHAR
** pNames
= saNames
.get();
1292 pNames
[0] = const_cast<OLECHAR
*>(o3tl::toW(aName
.getStr()));
1295 for ( size_t nInd
= 0; nInd
< dispparams
.cArgs
; nInd
++ )
1297 if (auto v
= o3tl::tryAccess
<NamedArgument
>(aParams
[nInd
]))
1299 const NamedArgument
& arg
= *v
;
1301 //We put the parameter names in reverse order into the array,
1302 //so we can use the DISPID array for DISPPARAMS::rgdispidNamedArgs
1303 //The first name in the array is the method name
1304 pNames
[nSizeAr
- 1 - cNamedArg
++] = const_cast<OLECHAR
*>(o3tl::toW(arg
.Name
.getStr()));
1308 arDispidNamedArgs
.reset( new DISPID
[nSizeAr
] );
1309 HRESULT hr
= getTypeInfo()->GetIDsOfNames( pNames
, nSizeAr
, arDispidNamedArgs
.get() );
1310 if ( hr
== E_NOTIMPL
)
1311 hr
= m_spDispatch
->GetIDsOfNames(IID_NULL
, pNames
, nSizeAr
, LOCALE_USER_DEFAULT
, arDispidNamedArgs
.get() );
1313 if ( SUCCEEDED( hr
) )
1315 if ( pInvkinds
[nStep
] == DISPATCH_PROPERTYPUT
)
1317 DISPID
* arIDs
= arDispidNamedArgs
.get();
1318 arIDs
[0] = DISPID_PROPERTYPUT
;
1319 dispparams
.rgdispidNamedArgs
= arIDs
;
1323 DISPID
* arIDs
= arDispidNamedArgs
.get();
1324 dispparams
.rgdispidNamedArgs
= & arIDs
[1];
1327 else if (hr
== DISP_E_UNKNOWNNAME
)
1329 throw IllegalArgumentException(
1330 "[automation bridge]One of the named arguments is wrong!",
1331 Reference
<XInterface
>(), 0);
1335 throw InvocationTargetException(
1336 "[automation bridge] ITypeInfo::GetIDsOfNames returned error "
1337 + OUString::number(static_cast<sal_Int32
>(hr
), 16), Reference
<XInterface
>(), Any());
1342 ptrArgs
.reset(new CComVariant
[dispparams
.cArgs
]);
1343 ptrRefArgs
.reset(new CComVariant
[dispparams
.cArgs
]);
1344 arArgs
= ptrArgs
.get();
1345 arRefArgs
= ptrRefArgs
.get();
1350 sal_Int32 revIndex
= 0;
1351 for ( nInd
= 0; nInd
< sal_Int32(dispparams
.cArgs
); nInd
++)
1353 revIndex
= dispparams
.cArgs
- nInd
- 1;
1354 arRefArgs
[revIndex
].byref
= nullptr;
1356 if ( nInd
< aParams
.getLength() )
1357 anyArg
= aParams
.getConstArray()[nInd
];
1359 // Property Put arguments
1360 if ( anyArg
.getValueType() == cppu::UnoType
<PropertyPutArgument
>::get() )
1362 PropertyPutArgument arg
;
1367 if (anyArg
.getValueType() == cppu::UnoType
<NamedArgument
>::get())
1369 NamedArgument aNamedArgument
;
1370 anyArg
>>= aNamedArgument
;
1371 anyArg
= aNamedArgument
.Value
;
1374 if ( nInd
< aParams
.getLength() && anyArg
.getValueTypeClass() != TypeClass_VOID
)
1376 anyToVariant( &arArgs
[revIndex
], anyArg
, VT_VARIANT
);
1380 arArgs
[revIndex
].vt
= VT_ERROR
;
1381 arArgs
[revIndex
].scode
= DISP_E_PARAMNOTFOUND
;
1385 catch (IllegalArgumentException
& e
)
1387 e
.ArgumentPosition
= ::sal::static_int_cast
< sal_Int16
, sal_Int32
>( nInd
);
1390 catch (CannotConvertException
& e
)
1392 e
.ArgumentIndex
= nInd
;
1396 dispparams
.rgvarg
= arArgs
;
1397 // invoking OLE method
1398 hInvRes
= m_spDispatch
->Invoke( dispid
,
1400 LOCALE_USER_DEFAULT
,
1401 ::sal::static_int_cast
< WORD
, INVOKEKIND
>( pInvkinds
[nStep
] ),
1408 // converting return value and out parameter back to UNO
1409 if ( SUCCEEDED( hInvRes
) )
1410 variantToAny( &varResult
, aResult
, false );
1413 // map error codes to exceptions
1419 case DISP_E_BADPARAMCOUNT
:
1420 throw IllegalArgumentException("[automation bridge] Wrong "
1421 "number of arguments. Object returned DISP_E_BADPARAMCOUNT.",
1424 case DISP_E_BADVARTYPE
:
1425 throw RuntimeException("[automation bridge] One or more "
1426 "arguments have the wrong type. Object returned "
1427 "DISP_E_BADVARTYPE.", nullptr);
1429 case DISP_E_EXCEPTION
:
1430 message
= "[automation bridge]: "
1431 + OUString(o3tl::toU(excepinfo
.bstrDescription
),
1432 ::SysStringLen(excepinfo
.bstrDescription
));
1433 throw InvocationTargetException(message
, Reference
<XInterface
>(), Any());
1435 case DISP_E_MEMBERNOTFOUND
:
1436 message
= "[automation bridge]: A function with the name \""
1437 + aName
+ "\" is not supported. Object returned "
1438 "DISP_E_MEMBERNOTFOUND.";
1439 throw IllegalArgumentException(message
, nullptr, 0);
1441 case DISP_E_NONAMEDARGS
:
1442 throw IllegalArgumentException("[automation bridge] Object "
1443 "returned DISP_E_NONAMEDARGS",nullptr, ::sal::static_int_cast
< sal_Int16
, unsigned int >( uArgErr
));
1445 case DISP_E_OVERFLOW
:
1446 throw CannotConvertException("[automation bridge] Call failed.",
1447 static_cast<XInterface
*>(
1448 static_cast<XWeak
*>(this)), TypeClass_UNKNOWN
, FailReason::OUT_OF_RANGE
, uArgErr
);
1450 case DISP_E_PARAMNOTFOUND
:
1451 throw IllegalArgumentException("[automation bridge]Call failed."
1452 "Object returned DISP_E_PARAMNOTFOUND.",
1453 nullptr, ::sal::static_int_cast
< sal_Int16
, unsigned int >( uArgErr
));
1455 case DISP_E_TYPEMISMATCH
:
1456 throw CannotConvertException("[automation bridge] Call failed. "
1457 "Object returned DISP_E_TYPEMISMATCH",
1458 static_cast<XInterface
*>(
1459 static_cast<XWeak
*>(this)) , TypeClass_UNKNOWN
, FailReason::UNKNOWN
, uArgErr
);
1461 case DISP_E_UNKNOWNINTERFACE
:
1462 throw RuntimeException("[automation bridge] Call failed. "
1463 "Object returned DISP_E_UNKNOWNINTERFACE.",nullptr);
1465 case DISP_E_UNKNOWNLCID
:
1466 throw RuntimeException("[automation bridge] Call failed. "
1467 "Object returned DISP_E_UNKNOWNLCID.",nullptr);
1469 case DISP_E_PARAMNOTOPTIONAL
:
1470 throw CannotConvertException("[automation bridge] Call failed."
1471 "Object returned DISP_E_PARAMNOTOPTIONAL",
1472 static_cast<XInterface
*>(static_cast<XWeak
*>(this)),
1473 TypeClass_UNKNOWN
, FailReason::NO_DEFAULT_AVAILABLE
, uArgErr
);
1476 throw RuntimeException();
1484 sal_Bool SAL_CALL
IUnknownWrapper::hasMember( const OUString
& aName
)
1486 if ( ! m_spDispatch
)
1488 throw RuntimeException(
1489 "[automation bridge] The object does not have an IDispatch interface");
1492 o2u_attachCurrentThread();
1494 return getDispid( aName
, &dispid
);
1498 // UnoConversionUtilities --------------------------------------------------------------------------------
1499 Reference
< XInterface
> IUnknownWrapper::createUnoWrapperInstance()
1501 if( m_nUnoWrapperClass
== INTERFACE_OLE_WRAPPER_IMPL
)
1503 Reference
<XWeak
> xWeak
= static_cast<XWeak
*>( new InterfaceOleWrapper(
1504 m_smgr
, m_nUnoWrapperClass
, m_nComWrapperClass
));
1505 return Reference
<XInterface
>( xWeak
, UNO_QUERY
);
1507 else if( m_nUnoWrapperClass
== UNO_OBJECT_WRAPPER_REMOTE_OPT
)
1509 Reference
<XWeak
> xWeak
= static_cast<XWeak
*>( new UnoObjectWrapperRemoteOpt(
1510 m_smgr
, m_nUnoWrapperClass
, m_nComWrapperClass
));
1511 return Reference
<XInterface
>( xWeak
, UNO_QUERY
);
1514 return Reference
<XInterface
>();
1516 Reference
<XInterface
> IUnknownWrapper::createComWrapperInstance()
1518 Reference
<XWeak
> xWeak
= static_cast<XWeak
*>( new IUnknownWrapper(
1519 m_smgr
, m_nUnoWrapperClass
, m_nComWrapperClass
));
1520 return Reference
<XInterface
>( xWeak
, UNO_QUERY
);
1524 void IUnknownWrapper::getMethodInfo(const OUString
& sName
, TypeDescription
& methodInfo
)
1526 TypeDescription desc
= getInterfaceMemberDescOfCurrentCall(sName
);
1529 typelib_TypeDescription
* pMember
= desc
.get();
1530 if( pMember
->eTypeClass
== typelib_TypeClass_INTERFACE_METHOD
)
1531 methodInfo
= pMember
;
1535 void IUnknownWrapper::getAttributeInfo(const OUString
& sName
, TypeDescription
& attributeInfo
)
1537 TypeDescription desc
= getInterfaceMemberDescOfCurrentCall(sName
);
1540 typelib_TypeDescription
* pMember
= desc
.get();
1541 if( pMember
->eTypeClass
== typelib_TypeClass_INTERFACE_ATTRIBUTE
)
1543 attributeInfo
= reinterpret_cast<typelib_InterfaceAttributeTypeDescription
*>(pMember
)->pAttributeTypeRef
;
1547 TypeDescription
IUnknownWrapper::getInterfaceMemberDescOfCurrentCall(const OUString
& sName
)
1549 TypeDescription ret
;
1551 for( sal_Int32 i
=0; i
< m_seqTypes
.getLength(); i
++)
1553 TypeDescription
_curDesc( m_seqTypes
[i
]);
1554 _curDesc
.makeComplete();
1555 typelib_InterfaceTypeDescription
* pInterface
= reinterpret_cast<typelib_InterfaceTypeDescription
*>(_curDesc
.get());
1558 typelib_InterfaceMemberTypeDescription
* pMember
= nullptr;
1559 //find the member description of the current call
1560 for( int j
=0; j
< pInterface
->nAllMembers
; j
++)
1562 typelib_TypeDescriptionReference
* pTypeRefMember
= pInterface
->ppAllMembers
[j
];
1563 typelib_TypeDescription
* pDescMember
= nullptr;
1564 TYPELIB_DANGER_GET( &pDescMember
, pTypeRefMember
);
1566 typelib_InterfaceMemberTypeDescription
* pInterfaceMember
=
1567 reinterpret_cast<typelib_InterfaceMemberTypeDescription
*>(pDescMember
);
1568 if( OUString( pInterfaceMember
->pMemberName
) == sName
)
1570 pMember
= pInterfaceMember
;
1573 TYPELIB_DANGER_RELEASE( pDescMember
);
1578 ret
= &pMember
->aBase
;
1579 TYPELIB_DANGER_RELEASE( &pMember
->aBase
);
1588 bool IUnknownWrapper::isJScriptObject()
1590 if( m_eJScript
== JScriptUndefined
)
1592 CComDispatchDriver
disp( m_spDispatch
);
1596 if( SUCCEEDED( disp
.GetPropertyByName( JSCRIPT_ID_PROPERTY
, &result
)))
1598 if(result
.vt
== VT_BSTR
)
1600 CComBSTR
name( result
.bstrVal
);
1602 if( name
== CComBSTR(JSCRIPT_ID
))
1603 m_eJScript
= IsJScript
;
1607 if( m_eJScript
== JScriptUndefined
)
1608 m_eJScript
= NoJScript
;
1611 return m_eJScript
!= NoJScript
;
1616 The function ultimately calls IDispatch::Invoke on the wrapped COM object.
1617 The COM object does not implement UNO Interfaces ( via IDispatch). This
1618 is the case when the OleObjectFactory service has been used to create a
1620 @exception IllegalArgumentException
1621 @exception CannotConvertException
1622 @InvocationTargetException
1626 Any
IUnknownWrapper::invokeWithDispIdComTlb(const OUString
& sFuncName
,
1627 const Sequence
< Any
>& Params
,
1628 Sequence
< sal_Int16
>& OutParamIndex
,
1629 Sequence
< Any
>& OutParam
)
1631 // Get type info for the call. It can be a method call or property put or
1632 // property get operation.
1633 FuncDesc
aFuncDesc(getTypeInfo());
1634 getFuncDescForInvoke(sFuncName
, Params
, & aFuncDesc
);
1635 return invokeWithDispIdComTlb( aFuncDesc
, sFuncName
, Params
, OutParamIndex
, OutParam
);
1638 Any
IUnknownWrapper::invokeWithDispIdComTlb(FuncDesc
& aFuncDesc
,
1639 const OUString
& sFuncName
,
1640 const Sequence
< Any
>& Params
,
1641 Sequence
< sal_Int16
>& OutParamIndex
,
1642 Sequence
< Any
>& OutParam
)
1647 DISPPARAMS dispparams
= {nullptr, nullptr, 0, 0};
1648 CComVariant varResult
;
1649 ExcepInfo excepinfo
;
1650 unsigned int uArgErr
;
1652 sal_Int32 nUnoArgs
= Params
.getLength();
1653 DISPID idPropertyPut
= DISPID_PROPERTYPUT
;
1654 std::unique_ptr
<DISPID
[]> arDispidNamedArgs
;
1655 std::unique_ptr
<CComVariant
[]> ptrArgs
;
1656 std::unique_ptr
<CComVariant
[]> ptrRefArgs
; // referenced arguments
1657 CComVariant
* arArgs
= nullptr;
1658 CComVariant
* arRefArgs
= nullptr;
1659 sal_Int32 revIndex
= 0;
1661 //Set the array of DISPIDs for named args if it is a property put operation.
1662 //If there are other named arguments another array is set later on.
1663 if (aFuncDesc
->invkind
== INVOKE_PROPERTYPUT
1664 || aFuncDesc
->invkind
== INVOKE_PROPERTYPUTREF
)
1665 dispparams
.rgdispidNamedArgs
= & idPropertyPut
;
1667 //Determine the number of named arguments
1668 for (int iParam
= 0; iParam
< nUnoArgs
; iParam
++)
1670 const Any
& curArg
= Params
[iParam
];
1671 if (curArg
.getValueType() == cppu::UnoType
<NamedArgument
>::get())
1672 dispparams
.cNamedArgs
++;
1674 //In a property put operation a property value is a named argument (DISPID_PROPERTYPUT).
1675 //Therefore the number of named arguments is increased by one.
1676 //Although named, the argument is not named in an actual language, such as Basic,
1677 //therefore it is never a com.sun.star.bridge.oleautomation.NamedArgument
1678 if (aFuncDesc
->invkind
== DISPATCH_PROPERTYPUT
1679 || aFuncDesc
->invkind
== DISPATCH_PROPERTYPUTREF
)
1680 dispparams
.cNamedArgs
++;
1682 //Determine the number of all arguments and named arguments
1683 if (aFuncDesc
->cParamsOpt
== -1)
1685 //Attribute vararg is set on this method. "Unlimited" number of args
1686 //supported. There can be no optional or defaultvalue on any of the arguments.
1687 dispparams
.cArgs
= nUnoArgs
;
1691 //If there are named arguments, then the dispparams.cArgs
1692 //is the number of supplied args, otherwise it is the expected number.
1693 if (dispparams
.cNamedArgs
)
1694 dispparams
.cArgs
= nUnoArgs
;
1696 dispparams
.cArgs
= aFuncDesc
->cParams
;
1699 //check if there are not too many arguments supplied
1700 if (::sal::static_int_cast
< sal_uInt32
, int >( nUnoArgs
) > dispparams
.cArgs
)
1702 throw IllegalArgumentException(
1703 "[automation bridge] There are too many arguments for this method",
1704 Reference
<XInterface
>(), static_cast<sal_Int16
>(dispparams
.cArgs
));
1707 //Set up the array of DISPIDs (DISPPARAMS::rgdispidNamedArgs)
1708 //for the named arguments.
1709 //If there is only one named arg and if it is because of a property put
1710 //operation, then we need not set up the DISPID array.
1711 if (dispparams
.cNamedArgs
> 0 &&
1712 ! (dispparams
.cNamedArgs
== 1 &&
1713 (aFuncDesc
->invkind
== INVOKE_PROPERTYPUT
||
1714 aFuncDesc
->invkind
== INVOKE_PROPERTYPUTREF
)))
1716 //set up an array containing the member and parameter names
1717 //which is then used in ITypeInfo::GetIDsOfNames
1718 //First determine the size of the array of names which is passed to
1719 //ITypeInfo::GetIDsOfNames. It must hold the method names + the named
1721 int nSizeAr
= dispparams
.cNamedArgs
+ 1;
1722 if (aFuncDesc
->invkind
== INVOKE_PROPERTYPUT
1723 || aFuncDesc
->invkind
== INVOKE_PROPERTYPUTREF
)
1725 nSizeAr
= dispparams
.cNamedArgs
; //counts the DISID_PROPERTYPUT
1728 std::unique_ptr
<OLECHAR
*[]> saNames(new OLECHAR
*[nSizeAr
]);
1729 OLECHAR
** arNames
= saNames
.get();
1730 arNames
[0] = const_cast<OLECHAR
*>(o3tl::toW(sFuncName
.getStr()));
1733 for (size_t iParams
= 0; iParams
< dispparams
.cArgs
; iParams
++)
1735 const Any
& curArg
= Params
[iParams
];
1736 if (auto v
= o3tl::tryAccess
<NamedArgument
>(curArg
))
1738 const NamedArgument
& arg
= *v
;
1739 //We put the parameter names in reverse order into the array,
1740 //so we can use the DISPID array for DISPPARAMS::rgdispidNamedArgs
1741 //The first name in the array is the method name
1742 arNames
[nSizeAr
- 1 - cNamedArg
++] = const_cast<OLECHAR
*>(o3tl::toW(arg
.Name
.getStr()));
1746 //Prepare the array of DISPIDs for ITypeInfo::GetIDsOfNames
1747 //it must be big enough to contain the DISPIDs of the member + parameters
1748 arDispidNamedArgs
.reset(new DISPID
[nSizeAr
]);
1749 HRESULT hr
= getTypeInfo()->GetIDsOfNames(arNames
, nSizeAr
,
1750 arDispidNamedArgs
.get());
1751 if ( hr
== E_NOTIMPL
)
1752 hr
= m_spDispatch
->GetIDsOfNames(IID_NULL
, arNames
, nSizeAr
, LOCALE_USER_DEFAULT
, arDispidNamedArgs
.get() );
1756 // In a "property put" operation, the property value is a named param with the
1757 //special DISPID DISPID_PROPERTYPUT
1758 if (aFuncDesc
->invkind
== DISPATCH_PROPERTYPUT
1759 || aFuncDesc
->invkind
== DISPATCH_PROPERTYPUTREF
)
1761 //Element at index 0 in the DISPID array must be DISPID_PROPERTYPUT
1762 //The first item in the array arDispidNamedArgs is the DISPID for
1763 //the method. We replace it with DISPID_PROPERTYPUT.
1764 DISPID
* arIDs
= arDispidNamedArgs
.get();
1765 arIDs
[0] = DISPID_PROPERTYPUT
;
1766 dispparams
.rgdispidNamedArgs
= arIDs
;
1770 //The first item in the array arDispidNamedArgs is the DISPID for
1771 //the method. It must be removed
1772 DISPID
* arIDs
= arDispidNamedArgs
.get();
1773 dispparams
.rgdispidNamedArgs
= & arIDs
[1];
1776 else if (hr
== DISP_E_UNKNOWNNAME
)
1778 throw IllegalArgumentException(
1779 "[automation bridge]One of the named arguments is wrong!",
1780 Reference
<XInterface
>(), 0);
1784 throw InvocationTargetException(
1785 "[automation bridge] ITypeInfo::GetIDsOfNames returned error "
1786 + OUString::number(static_cast<sal_Int32
>(hr
), 16), Reference
<XInterface
>(), Any());
1791 ptrArgs
.reset(new CComVariant
[dispparams
.cArgs
]);
1792 ptrRefArgs
.reset(new CComVariant
[dispparams
.cArgs
]);
1793 arArgs
= ptrArgs
.get();
1794 arRefArgs
= ptrRefArgs
.get();
1797 for (i
= 0; i
< static_cast<sal_Int32
>(dispparams
.cArgs
); i
++)
1799 revIndex
= dispparams
.cArgs
- i
-1;
1800 arRefArgs
[revIndex
].byref
=nullptr;
1803 anyArg
= Params
.getConstArray()[i
];
1805 unsigned short paramFlags
= PARAMFLAG_FOPT
| PARAMFLAG_FIN
;
1806 VARTYPE varType
= VT_VARIANT
;
1807 if (aFuncDesc
->cParamsOpt
!= -1 || aFuncDesc
->cParams
!= (i
+ 1))
1809 paramFlags
= aFuncDesc
->lprgelemdescParam
[i
].paramdesc
.wParamFlags
;
1810 varType
= getElementTypeDesc(&aFuncDesc
->lprgelemdescParam
[i
].tdesc
);
1813 // Make sure that there is a UNO parameter for every
1814 // expected parameter. If there is no UNO parameter where the
1815 // called function expects one, then it must be optional. Otherwise
1816 // it's a UNO programming error.
1817 if (i
>= nUnoArgs
&& !(paramFlags
& PARAMFLAG_FOPT
))
1819 throw IllegalArgumentException(
1820 ("ole automation bridge: The called function expects an argument at position: "
1821 + OUString::number(i
) + " (index starting at 0)."),
1822 Reference
<XInterface
>(), static_cast<sal_Int16
>(i
));
1825 // Property Put arguments
1826 if (anyArg
.getValueType() == cppu::UnoType
<PropertyPutArgument
>::get())
1828 PropertyPutArgument arg
;
1833 if (anyArg
.getValueType() == cppu::UnoType
<NamedArgument
>::get())
1835 NamedArgument aNamedArgument
;
1836 anyArg
>>= aNamedArgument
;
1837 anyArg
= aNamedArgument
.Value
;
1840 if (paramFlags
& PARAMFLAG_FOUT
&&
1841 ! (paramFlags
& PARAMFLAG_FIN
) )
1843 VARTYPE type
= ::sal::static_int_cast
< VARTYPE
, int >( varType
^ VT_BYREF
);
1846 arRefArgs
[revIndex
].vt
= type
;
1851 arRefArgs
[revIndex
].vt
= VT_ERROR
;
1852 arRefArgs
[revIndex
].scode
= DISP_E_PARAMNOTFOUND
;
1854 if( type
== VT_VARIANT
)
1856 arArgs
[revIndex
].vt
= VT_VARIANT
| VT_BYREF
;
1857 arArgs
[revIndex
].byref
= &arRefArgs
[revIndex
];
1861 arArgs
[revIndex
].vt
= varType
;
1862 if (type
== VT_DECIMAL
)
1863 arArgs
[revIndex
].byref
= & arRefArgs
[revIndex
].decVal
;
1865 arArgs
[revIndex
].byref
= & arRefArgs
[revIndex
].byref
;
1868 // in/out + in byref params
1869 else if (varType
& VT_BYREF
)
1871 VARTYPE type
= ::sal::static_int_cast
< VARTYPE
, int >( varType
^ VT_BYREF
);
1874 if (i
< nUnoArgs
&& anyArg
.getValueTypeClass() != TypeClass_VOID
)
1876 anyToVariant( & arRefArgs
[revIndex
], anyArg
, type
);
1878 else if (paramFlags
& PARAMFLAG_FHASDEFAULT
)
1880 //optional arg with default
1881 VariantCopy( & arRefArgs
[revIndex
],
1882 & aFuncDesc
->lprgelemdescParam
[i
].paramdesc
.
1883 pparamdescex
->varDefaultValue
);
1888 //e.g: call func(x) in basic : func() ' no arg supplied
1889 OSL_ASSERT(paramFlags
& PARAMFLAG_FOPT
);
1890 arRefArgs
[revIndex
].vt
= VT_ERROR
;
1891 arRefArgs
[revIndex
].scode
= DISP_E_PARAMNOTFOUND
;
1894 // Set the converted arguments in the array which will be
1895 // DISPPARAMS::rgvarg
1896 // byref arg VT_XXX |VT_BYREF
1897 arArgs
[revIndex
].vt
= varType
;
1898 if (revIndex
== 0 && aFuncDesc
->invkind
== INVOKE_PROPERTYPUT
)
1900 arArgs
[revIndex
] = arRefArgs
[revIndex
];
1902 else if (type
== VT_DECIMAL
)
1904 arArgs
[revIndex
].byref
= & arRefArgs
[revIndex
].decVal
;
1906 else if (type
== VT_VARIANT
)
1908 if ( ! (paramFlags
& PARAMFLAG_FOUT
))
1909 arArgs
[revIndex
] = arRefArgs
[revIndex
];
1911 arArgs
[revIndex
].byref
= & arRefArgs
[revIndex
];
1915 arArgs
[revIndex
].byref
= & arRefArgs
[revIndex
].byref
;
1916 arArgs
[revIndex
].vt
= ::sal::static_int_cast
< VARTYPE
, int >( arRefArgs
[revIndex
].vt
| VT_BYREF
);
1920 // in parameter no VT_BYREF except for array, interfaces
1922 { // void any stands for optional param
1923 if (i
< nUnoArgs
&& anyArg
.getValueTypeClass() != TypeClass_VOID
)
1925 anyToVariant( & arArgs
[revIndex
], anyArg
, varType
);
1927 //optional arg but no void any supplied
1928 //Basic: obj.func() ' first parameter left out because it is optional
1929 else if (paramFlags
& PARAMFLAG_FHASDEFAULT
)
1931 //optional arg with default either as direct arg : VT_XXX or
1932 VariantCopy( & arArgs
[revIndex
],
1933 & aFuncDesc
->lprgelemdescParam
[i
].paramdesc
.
1934 pparamdescex
->varDefaultValue
);
1936 else if (paramFlags
& PARAMFLAG_FOPT
)
1938 arArgs
[revIndex
].vt
= VT_ERROR
;
1939 arArgs
[revIndex
].scode
= DISP_E_PARAMNOTFOUND
;
1943 arArgs
[revIndex
].vt
= VT_EMPTY
;
1944 arArgs
[revIndex
].lVal
= 0;
1949 catch (IllegalArgumentException
& e
)
1951 e
.ArgumentPosition
= ::sal::static_int_cast
< sal_Int16
, sal_Int32
>( i
);
1954 catch (CannotConvertException
& e
)
1956 e
.ArgumentIndex
= i
;
1959 dispparams
.rgvarg
= arArgs
;
1960 // invoking OLE method
1961 result
= m_spDispatch
->Invoke(aFuncDesc
->memid
,
1963 LOCALE_USER_DEFAULT
,
1964 ::sal::static_int_cast
< WORD
, INVOKEKIND
>( aFuncDesc
->invkind
),
1970 // converting return value and out parameter back to UNO
1974 // allocate space for the out param Sequence and indices Sequence
1975 int outParamsCount
= 0; // includes in/out parameter
1976 for (int j
= 0; j
< aFuncDesc
->cParams
; j
++)
1978 if (aFuncDesc
->lprgelemdescParam
[j
].paramdesc
.wParamFlags
&
1983 OutParamIndex
.realloc(outParamsCount
);
1984 OutParam
.realloc(outParamsCount
);
1985 // Convert out params
1988 int outParamIndex
=0;
1989 for (int paramIndex
= 0; paramIndex
< nUnoArgs
; paramIndex
++)
1991 //Determine the index within the method signature
1992 int realParamIndex
= paramIndex
;
1993 int revParamIndex
= dispparams
.cArgs
- paramIndex
- 1;
1994 if (Params
[paramIndex
].getValueType()
1995 == cppu::UnoType
<NamedArgument
>::get())
1997 //dispparams.rgdispidNamedArgs contains the mapping from index
1998 //of named args list to index of parameter list
1999 realParamIndex
= dispparams
.rgdispidNamedArgs
[revParamIndex
];
2002 // no named arg, always come before named args
2003 if (! (aFuncDesc
->lprgelemdescParam
[realParamIndex
].paramdesc
.wParamFlags
2007 // variantToAny is called with the "reduce range" parameter set to sal_False.
2008 // That causes VT_I4 values not to be converted down to a "lower" type. That
2009 // feature exist for JScript only because it only uses VT_I4 for integer types.
2012 variantToAny( & arRefArgs
[revParamIndex
], outAny
, false );
2014 catch (IllegalArgumentException
& e
)
2016 e
.ArgumentPosition
= static_cast<sal_Int16
>(paramIndex
);
2019 catch (CannotConvertException
& e
)
2021 e
.ArgumentIndex
= paramIndex
;
2024 OutParam
[outParamIndex
] = outAny
;
2025 OutParamIndex
[outParamIndex
] = ::sal::static_int_cast
< sal_Int16
, int >( paramIndex
);
2028 OutParam
.realloc(outParamIndex
);
2029 OutParamIndex
.realloc(outParamIndex
);
2032 variantToAny(&varResult
, ret
, false);
2035 // map error codes to exceptions
2041 case DISP_E_BADPARAMCOUNT
:
2042 throw IllegalArgumentException("[automation bridge] Wrong "
2043 "number of arguments. Object returned DISP_E_BADPARAMCOUNT.",
2046 case DISP_E_BADVARTYPE
:
2047 throw RuntimeException("[automation bridge] One or more "
2048 "arguments have the wrong type. Object returned "
2049 "DISP_E_BADVARTYPE.", nullptr);
2051 case DISP_E_EXCEPTION
:
2052 message
= "[automation bridge]: "
2053 + OUString(o3tl::toU(excepinfo
.bstrDescription
),
2054 ::SysStringLen(excepinfo
.bstrDescription
));
2056 throw InvocationTargetException(message
, Reference
<XInterface
>(), Any());
2058 case DISP_E_MEMBERNOTFOUND
:
2059 message
= "[automation bridge]: A function with the name \""
2060 + sFuncName
+ "\" is not supported. Object returned "
2061 "DISP_E_MEMBERNOTFOUND.";
2062 throw IllegalArgumentException(message
, nullptr, 0);
2064 case DISP_E_NONAMEDARGS
:
2065 throw IllegalArgumentException("[automation bridge] Object "
2066 "returned DISP_E_NONAMEDARGS",nullptr, ::sal::static_int_cast
< sal_Int16
, unsigned int >( uArgErr
));
2068 case DISP_E_OVERFLOW
:
2069 throw CannotConvertException("[automation bridge] Call failed.",
2070 static_cast<XInterface
*>(
2071 static_cast<XWeak
*>(this)), TypeClass_UNKNOWN
, FailReason::OUT_OF_RANGE
, uArgErr
);
2073 case DISP_E_PARAMNOTFOUND
:
2074 throw IllegalArgumentException("[automation bridge]Call failed."
2075 "Object returned DISP_E_PARAMNOTFOUND.",
2076 nullptr, ::sal::static_int_cast
< sal_Int16
, unsigned int >( uArgErr
));
2078 case DISP_E_TYPEMISMATCH
:
2079 throw CannotConvertException("[automation bridge] Call failed. "
2080 "Object returned DISP_E_TYPEMISMATCH",
2081 static_cast<XInterface
*>(
2082 static_cast<XWeak
*>(this)) , TypeClass_UNKNOWN
, FailReason::UNKNOWN
, uArgErr
);
2084 case DISP_E_UNKNOWNINTERFACE
:
2085 throw RuntimeException("[automation bridge] Call failed. "
2086 "Object returned DISP_E_UNKNOWNINTERFACE.",nullptr);
2088 case DISP_E_UNKNOWNLCID
:
2089 throw RuntimeException("[automation bridge] Call failed. "
2090 "Object returned DISP_E_UNKNOWNLCID.",nullptr);
2092 case DISP_E_PARAMNOTOPTIONAL
:
2093 throw CannotConvertException("[automation bridge] Call failed."
2094 "Object returned DISP_E_PARAMNOTOPTIONAL",
2095 static_cast<XInterface
*>(static_cast<XWeak
*>(this)),
2096 TypeClass_UNKNOWN
, FailReason::NO_DEFAULT_AVAILABLE
, uArgErr
);
2099 throw RuntimeException();
2106 void IUnknownWrapper::getFuncDescForInvoke(const OUString
& sFuncName
,
2107 const Sequence
<Any
> & seqArgs
,
2108 FUNCDESC
** pFuncDesc
)
2110 int nUnoArgs
= seqArgs
.getLength();
2111 const Any
* arArgs
= seqArgs
.getConstArray();
2112 ITypeInfo
* pInfo
= getTypeInfo();
2114 //If the last of the positional arguments is a PropertyPutArgument
2115 //then obtain the type info for the property put operation.
2117 //The property value is always the last argument, in a positional argument list
2118 //or in a list of named arguments. A PropertyPutArgument is actually a named argument
2119 //hence it must not be put in an extra NamedArgument structure
2121 arArgs
[nUnoArgs
- 1].getValueType() == cppu::UnoType
<PropertyPutArgument
>::get())
2123 // DISPATCH_PROPERTYPUT
2124 FuncDesc
aDescGet(pInfo
);
2125 FuncDesc
aDescPut(pInfo
);
2126 VarDesc
aVarDesc(pInfo
);
2127 getPropDesc(sFuncName
, & aDescGet
, & aDescPut
, & aVarDesc
);
2130 throw IllegalArgumentException(
2131 "[automation bridge] The object does not have a writeable property: "
2132 + sFuncName
, Reference
<XInterface
>(), 0);
2134 *pFuncDesc
= aDescPut
.Detach();
2137 { // DISPATCH_METHOD
2138 FuncDesc
aFuncDesc(pInfo
);
2139 getFuncDesc(sFuncName
, & aFuncDesc
);
2142 // Fallback: DISPATCH_PROPERTYGET can mostly be called as
2144 ITypeInfo
* pTypeInfo
= getTypeInfo();
2145 FuncDesc
aDescPut(pTypeInfo
);
2146 VarDesc
aVarDesc(pTypeInfo
);
2147 getPropDesc(sFuncName
, & aFuncDesc
, & aDescPut
, & aVarDesc
);
2150 throw IllegalArgumentException(
2151 "[automation bridge] The object does not have a function"
2152 " or readable property \""
2153 + sFuncName
+ "\"", Reference
<XInterface
>(), 0);
2156 *pFuncDesc
= aFuncDesc
.Detach();
2159 bool IUnknownWrapper::getDispid(const OUString
& sFuncName
, DISPID
* id
)
2161 OSL_ASSERT(m_spDispatch
);
2162 LPOLESTR lpsz
= const_cast<LPOLESTR
> (o3tl::toW(sFuncName
.getStr()));
2163 HRESULT hr
= m_spDispatch
->GetIDsOfNames(IID_NULL
, &lpsz
, 1, LOCALE_USER_DEFAULT
, id
);
2166 void IUnknownWrapper::getFuncDesc(const OUString
& sFuncName
, FUNCDESC
** pFuncDesc
)
2169 OSL_ASSERT( * pFuncDesc
== nullptr);
2171 typedef TLBFuncIndexMap::const_iterator cit
;
2172 //We assume there is only one entry with the function name. A property
2173 //would have two entries.
2174 cit itIndex
= m_mapComFunc
.find(sFuncName
);
2175 if (itIndex
== m_mapComFunc
.end())
2177 //try case insensitive with IDispatch::GetIDsOfNames
2179 if (getDispid(sFuncName
, &id
))
2181 CComBSTR memberName
;
2182 unsigned int pcNames
=0;
2183 // get the case sensitive name
2184 if( SUCCEEDED(getTypeInfo()->GetNames( id
, & memberName
, 1, &pcNames
)))
2186 //get the associated index and add an entry to the map
2187 //with the name sFuncName which differs in the casing of the letters to
2188 //the actual name as obtained from ITypeInfo
2189 OUString
sRealName(o3tl::toU(LPCOLESTR(memberName
)));
2190 cit itOrg
= m_mapComFunc
.find(sRealName
);
2191 OSL_ASSERT(itOrg
!= m_mapComFunc
.end());
2192 // maybe this is a property, if so we need
2193 // to store either both id's ( put/get ) or
2194 // just the get. Storing both is more consistent
2195 pair
<cit
, cit
> pItems
= m_mapComFunc
.equal_range( sRealName
);
2196 for ( ;pItems
.first
!= pItems
.second
; ++pItems
.first
)
2197 m_mapComFunc
.insert( TLBFuncIndexMap::value_type ( make_pair(sFuncName
, pItems
.first
->second
) ));
2199 m_mapComFunc
.find( sFuncName
);
2204 #if OSL_DEBUG_LEVEL >= 1
2205 // There must only be one entry if sFuncName represents a function or two
2206 // if it is a property
2207 pair
<cit
, cit
> p
= m_mapComFunc
.equal_range(sFuncName
.toAsciiLowerCase());
2209 for ( ;p
.first
!= p
.second
; p
.first
++, numEntries
++);
2210 OSL_ASSERT( ! (numEntries
> 3) );
2212 if( itIndex
!= m_mapComFunc
.end())
2214 ITypeInfo
* pType
= getTypeInfo();
2215 FUNCDESC
* pDesc
= nullptr;
2216 if (!SUCCEEDED(pType
->GetFuncDesc(itIndex
->second
, & pDesc
)))
2218 throw BridgeRuntimeError("[automation bridge] Could not get "
2219 "FUNCDESC for " + sFuncName
);
2221 if (pDesc
->invkind
== INVOKE_FUNC
)
2223 (*pFuncDesc
) = pDesc
;
2227 pType
->ReleaseFuncDesc(pDesc
);
2230 //else no entry found for sFuncName, pFuncDesc will not be filled in
2233 void IUnknownWrapper::getPropDesc(const OUString
& sFuncName
, FUNCDESC
** pFuncDescGet
,
2234 FUNCDESC
** pFuncDescPut
, VARDESC
** pVarDesc
)
2236 OSL_ASSERT( * pFuncDescGet
== nullptr && * pFuncDescPut
== nullptr);
2238 typedef TLBFuncIndexMap::const_iterator cit
;
2239 pair
<cit
, cit
> p
= m_mapComFunc
.equal_range(sFuncName
);
2240 if (p
.first
== m_mapComFunc
.end())
2242 //try case insensitive with IDispatch::GetIDsOfNames
2244 if (getDispid(sFuncName
, &id
))
2246 CComBSTR memberName
;
2247 unsigned int pcNames
=0;
2248 // get the case sensitive name
2249 if( SUCCEEDED(getTypeInfo()->GetNames( id
, & memberName
, 1, &pcNames
)))
2251 //As opposed to getFuncDesc, we do not add the value because we would
2252 // need to find the get and set description for the property. This would
2253 //mean to iterate over all FUNCDESCs again.
2254 p
= m_mapComFunc
.equal_range(OUString(o3tl::toU(LPCOLESTR(memberName
))));
2259 for ( int i
= 0 ;p
.first
!= p
.second
; p
.first
++, i
++)
2261 // There are a maximum of two entries, property put and property get
2262 OSL_ASSERT( ! (i
> 2) );
2263 ITypeInfo
* pType
= getTypeInfo();
2264 FUNCDESC
* pFuncDesc
= nullptr;
2265 if (SUCCEEDED( pType
->GetFuncDesc(p
.first
->second
, & pFuncDesc
)))
2267 if (pFuncDesc
->invkind
== INVOKE_PROPERTYGET
)
2269 (*pFuncDescGet
) = pFuncDesc
;
2271 else if (pFuncDesc
->invkind
== INVOKE_PROPERTYPUT
||
2272 pFuncDesc
->invkind
== INVOKE_PROPERTYPUTREF
)
2274 //a property can have 3 entries, put, put ref, get
2275 // If INVOKE_PROPERTYPUTREF or INVOKE_PROPERTYPUT is used
2276 //depends on what is found first.
2277 if ( * pFuncDescPut
)
2279 //we already have found one
2280 pType
->ReleaseFuncDesc(pFuncDesc
);
2284 (*pFuncDescPut
) = pFuncDesc
;
2289 pType
->ReleaseFuncDesc(pFuncDesc
);
2292 //ITypeInfo::GetFuncDesc may even provide a funcdesc for a VARDESC
2293 // with invkind = INVOKE_FUNC. Since this function should only return
2294 //a value for a real property (XInvokation::hasMethod, ..::hasProperty
2295 //we need to make sure that sFuncName represents a real property.
2296 VARDESC
* pVD
= nullptr;
2297 if (SUCCEEDED(pType
->GetVarDesc(p
.first
->second
, & pVD
)))
2300 //else no entry for sFuncName, pFuncDesc will not be filled in
2303 VARTYPE
IUnknownWrapper::getUserDefinedElementType( ITypeInfo
* pTypeInfo
, const DWORD nHrefType
)
2305 VARTYPE
_type( VT_NULL
);
2308 CComPtr
<ITypeInfo
> spRefInfo
;
2309 pTypeInfo
->GetRefTypeInfo( nHrefType
, &spRefInfo
.p
);
2312 TypeAttr
attr( spRefInfo
);
2313 spRefInfo
->GetTypeAttr( &attr
);
2314 if ( attr
->typekind
== TKIND_ENUM
)
2316 // We use the type of the first enum value.
2317 if ( attr
->cVars
== 0 )
2319 throw BridgeRuntimeError("[automation bridge] Could not obtain type description");
2321 VarDesc
var( spRefInfo
);
2322 spRefInfo
->GetVarDesc( 0, &var
);
2323 _type
= var
->lpvarValue
->vt
;
2325 else if ( attr
->typekind
== TKIND_INTERFACE
)
2329 else if ( attr
->typekind
== TKIND_DISPATCH
)
2331 _type
= VT_DISPATCH
;
2333 else if ( attr
->typekind
== TKIND_ALIAS
)
2335 // TKIND_ALIAS is a type that is an alias for another type. So get that alias type.
2336 _type
= getUserDefinedElementType( pTypeInfo
, attr
->tdescAlias
.hreftype
);
2340 throw BridgeRuntimeError( "[automation bridge] Unhandled user defined type." );
2347 VARTYPE
IUnknownWrapper::getElementTypeDesc(const TYPEDESC
*desc
)
2349 VARTYPE
_type( VT_NULL
);
2351 if (desc
->vt
== VT_PTR
)
2353 _type
= getElementTypeDesc(desc
->lptdesc
);
2356 else if (desc
->vt
== VT_SAFEARRAY
)
2358 _type
= getElementTypeDesc(desc
->lptdesc
);
2361 else if (desc
->vt
== VT_USERDEFINED
)
2363 ITypeInfo
* thisInfo
= getTypeInfo(); //kept by this instance
2364 _type
= getUserDefinedElementType( thisInfo
, desc
->hreftype
);
2373 void IUnknownWrapper::buildComTlbIndex()
2375 if ( ! m_bComTlbIndexInit
)
2377 MutexGuard
guard(getBridgeMutex());
2379 if ( ! m_bComTlbIndexInit
)
2382 ITypeInfo
* pType
= getTypeInfo();
2383 TypeAttr
typeAttr(pType
);
2384 if( SUCCEEDED( pType
->GetTypeAttr( &typeAttr
)))
2386 for( long i
= 0; i
< typeAttr
->cFuncs
; i
++)
2388 FuncDesc
funcDesc(pType
);
2389 if( SUCCEEDED( pType
->GetFuncDesc( i
, &funcDesc
)))
2391 CComBSTR memberName
;
2392 unsigned int pcNames
=0;
2393 if( SUCCEEDED(pType
->GetNames( funcDesc
->memid
, & memberName
, 1, &pcNames
)))
2395 OUString
usName(o3tl::toU(LPCOLESTR(memberName
)));
2396 m_mapComFunc
.emplace(usName
, i
);
2400 sError
= "[automation bridge] IUnknownWrapper::buildComTlbIndex, "
2401 "ITypeInfo::GetNames failed.";
2405 sError
= "[automation bridge] IUnknownWrapper::buildComTlbIndex, "
2406 "ITypeInfo::GetFuncDesc failed.";
2409 //If we create an Object in JScript and a property then it
2410 //has VARDESC instead of FUNCDESC
2411 for (long i
= 0; i
< typeAttr
->cVars
; i
++)
2413 VarDesc
varDesc(pType
);
2414 if (SUCCEEDED(pType
->GetVarDesc(i
, & varDesc
)))
2416 CComBSTR memberName
;
2417 unsigned int pcNames
= 0;
2418 if (SUCCEEDED(pType
->GetNames(varDesc
->memid
, & memberName
, 1, &pcNames
)))
2420 if (varDesc
->varkind
== VAR_DISPATCH
)
2422 OUString
usName(o3tl::toU(LPCOLESTR(memberName
)));
2423 m_mapComFunc
.emplace(usName
, i
);
2428 sError
= "[automation bridge] IUnknownWrapper::buildComTlbIndex, "
2429 "ITypeInfo::GetNames failed.";
2433 sError
= "[automation bridge] IUnknownWrapper::buildComTlbIndex, "
2434 "ITypeInfo::GetVarDesc failed.";
2439 sError
= "[automation bridge] IUnknownWrapper::buildComTlbIndex, "
2440 "ITypeInfo::GetTypeAttr failed.";
2442 if (sError
.getLength())
2444 throw BridgeRuntimeError(sError
);
2447 m_bComTlbIndexInit
= true;
2453 ITypeInfo
* IUnknownWrapper::getTypeInfo()
2457 throw BridgeRuntimeError("The object has no IDispatch interface!");
2462 MutexGuard
guard(getBridgeMutex());
2465 CComPtr
< ITypeInfo
> spType
;
2466 if( !SUCCEEDED( m_spDispatch
->GetTypeInfo( 0, LOCALE_USER_DEFAULT
, &spType
.p
)))
2468 throw BridgeRuntimeError("[automation bridge]The dispatch object does not "
2469 "support ITypeInfo!");
2472 OSL_DOUBLE_CHECKED_LOCKING_MEMORY_BARRIER();
2474 //If this is a dual interface then TYPEATTR::typekind is usually TKIND_INTERFACE
2475 //We need to get the type description for TKIND_DISPATCH
2476 TypeAttr
typeAttr(spType
.p
);
2477 if( SUCCEEDED(spType
->GetTypeAttr( &typeAttr
)))
2479 if (typeAttr
->typekind
== TKIND_INTERFACE
&&
2480 typeAttr
->wTypeFlags
& TYPEFLAG_FDUAL
)
2482 HREFTYPE refDispatch
;
2483 if (!SUCCEEDED(spType
->GetRefTypeOfImplType(::sal::static_int_cast
< UINT
, int >( -1 ), &refDispatch
)))
2485 throw BridgeRuntimeError(
2486 "[automation bridge] Could not obtain type information "
2487 "for dispatch interface." );
2489 CComPtr
<ITypeInfo
> spTypeDisp
;
2490 if (SUCCEEDED(spType
->GetRefTypeInfo(refDispatch
, & spTypeDisp
)))
2491 m_spTypeInfo
= spTypeDisp
;
2493 else if (typeAttr
->typekind
== TKIND_DISPATCH
)
2495 m_spTypeInfo
= spType
;
2499 throw BridgeRuntimeError(
2500 "[automation bridge] Automation object does not "
2501 "provide type information.");
2506 return m_spTypeInfo
;
2509 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */