/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * * This file incorporates work covered by the following license notice: * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you under the Apache * License, Version 2.0 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
InterfaceOleWrapper::~InterfaceOleWrapper()
{
MutexGuard guard(getBridgeMutex()); // remove entries in global map auto it = UnoObjToWrapperMap.find( reinterpret_cast<sal_uIntPtr>(m_xOrigin.get())); if(it != UnoObjToWrapperMap.end())
UnoObjToWrapperMap.erase(it);
}
COM_DECLSPEC_NOTHROW STDMETHODIMP_(ULONG) InterfaceOleWrapper::AddRef()
{
acquire(); // does not need to guard because one should not rely on the return value of // AddRef anyway return m_refCount;
}
// Drop the three XInterface methods, add the three corresponding IUnknown ones plus the // four IDispatch ones on top of that.
pTypeAttr->cFuncs = aMethods.getLength() - 3 + 3 + 4;
pTypeAttr->cVars = 0;
pTypeAttr->cImplTypes = 1; // FIXME: I think this, too, is always supposed to be as if just for the seven methods in // IDIspatch?
pTypeAttr->cbSizeVft = 7 * sizeof(void*);
pTypeAttr->cbAlignment = 8;
pTypeAttr->wTypeFlags = TYPEFLAG_FHIDDEN|TYPEFLAG_FNONEXTENSIBLE|TYPEFLAG_FDISPATCHABLE;
} else
assert(false);
pTypeAttr->lcid = LOCALE_USER_DEFAULT;
pTypeAttr->memidConstructor = MEMBERID_NIL;
pTypeAttr->memidDestructor = MEMBERID_NIL; // FIXME: Is this correct, just the vtable pointer, right?
pTypeAttr->cbSizeInstance = sizeof(void*);
pTypeAttr->wMajorVerNum = 0;
pTypeAttr->wMinorVerNum = 0;
pTypeAttr->idldescType.wIDLFlags = IDLFLAG_NONE;
SAL_INFO("extensions.olebridge", this << "@CXTypeInfo::GetTypeAttr: " << pTypeAttr);
SAL_INFO("extensions.olebridge", this << "@CXTypeInfo::GetDocumentation(" << memid << ")");
if (pBstrName)
{ if (memid == MEMBERID_NIL)
{
*pBstrName = sal::systools::BStr::newBSTR(msImplementationName);
} elseif (memid == DISPID_VALUE)
{ // MEMBERIDs are the same as DISPIDs, apparently?
*pBstrName = SysAllocString(L"Value");
} else
{ // FIXME: Shouldn't we be able to know the names of the members of UNO interfaces?
*pBstrName = sal::systools::BStr::newBSTR(Concat2View("UnknownNameOfMember#" + OUString::number(memid)));
}
} if (pBstrDocString)
*pBstrDocString = SysAllocString(L""); if (pdwHelpContext)
*pdwHelpContext = 0; if (pBstrHelpFile)
*pBstrHelpFile = nullptr;
return S_OK;
}
HRESULT STDMETHODCALLTYPE CXTypeInfo::GetDllEntry(MEMBERID,
INVOKEKIND,
BSTR *,
BSTR *,
WORD *)
{
SAL_WARN("extensions.olebridge", this << "@CXTypeInfo::GetDllEntry: E_NOTIMPL"); return E_NOTIMPL;
}
SAL_INFO("extensions.olebridge", this << "@CXTypeInfo::GetRefTypeInfo(" << hRefType << ")");
if (!ppTInfo) return E_POINTER;
// FIXME: Is it correct to assume that the only interfaces on which GetRefTypeInfo() would be // called are those that implement ooo::vba::XConnectable?
Reference<ooo::vba::XConnectable> xConnectable(mxOrigin, UNO_QUERY); if (!xConnectable.is()) return E_NOTIMPL;
// This is not actually called any more by my vbscript test after I added the IProvideClassInfo // thing... so all the CXTypeLib stuff is dead code at the moment.
SAL_INFO("extensions.olebridge", this << "@InterfaceOleWrapper::GetTypeInfo(" << iTInfo << ")");
if (!ppTInfo) return E_POINTER;
if (iTInfo != 0) return E_NOTIMPL;
// FIXME: This is surely incorrect. Why is being able to handle GetTypeInfo() here coupled to // being a source for outgoing events, i.e. implementing XConnectable? What would break if we // would use XInterfaceWithIID and its getIID instead?
Reference<ooo::vba::XConnectable> xConnectable(m_xOrigin, UNO_QUERY); if (!xConnectable.is()) return E_NOTIMPL;
SAL_INFO("extensions.olebridge", this << "@InterfaceOleWrapper::GetIDsOfNames:"); for (unsignedint i = 0; i < cNames; ++i)
{ // Initialise returned rgdispid values.
rgdispid[i] = DISPID_UNKNOWN;
if (m_xInvocation->hasMethod(exactName))
{
d.flags |= DISPATCH_METHOD;
bIsMethod = true;
}
if (d.flags != 0)
{
m_MemberInfos.push_back(d);
iter = m_nameToDispIdMap.emplace(exactName, static_cast<DISPID>(m_MemberInfos.size())).first;
if (exactName != name)
{
iter = m_nameToDispIdMap.emplace(name, static_cast<DISPID>(m_MemberInfos.size())).first;
}
}
}
if (iter == m_nameToDispIdMap.end())
{
ret = DISP_E_UNKNOWNNAME;
SAL_INFO("extensions.olebridge", " " << name << ": UNKNOWN");
} else
{
rgdispid[0] = (*iter).second;
SAL_INFO("extensions.olebridge", " " << name << ": " << rgdispid[0]);
if (bIsMethod && cNames > 1)
{
Reference<XIdlMethod> xIdlMethod;
Reference<XIntrospectionAccess> xIntrospectionAccess = m_xInvocation->getIntrospection(); try
{ if (xIntrospectionAccess.is())
xIdlMethod = xIntrospectionAccess->getMethod(exactName, MethodConcept::ALL);
} catch (const NoSuchMethodException&)
{
} if (xIdlMethod.is())
{ auto aParamInfos = xIdlMethod->getParameterInfos(); for (unsignedint i = 1; i < cNames; ++i)
{ bool bFound = false; for (int j = 0; j < aParamInfos.getLength(); ++j)
{ if (aParamInfos[j].aName.equalsIgnoreAsciiCase(o3tl::toU(rgszNames[i])))
{
rgdispid[i] = j;
bFound = true;
SAL_INFO("extensions.olebridge", " " << OUString(o3tl::toU(rgszNames[i])) << ": " << rgdispid[i]); break;
}
} if (!bFound)
SAL_INFO("extensions.olebridge", " " << OUString(o3tl::toU(rgszNames[i])) << ": NOT FOUND");
}
}
}
// Return value should be S_OK only if *all* the names were found. unsignedint i; for (i = 0; i < cNames; ++i) if (rgdispid[i] == DISPID_UNKNOWN) break; if (i == cNames)
ret = S_OK;
}
}
} catch(const BridgeRuntimeError&)
{
OSL_ASSERT(false);
} catch(const Exception&)
{
OSL_ASSERT(false);
} catch(...)
{
OSL_ASSERT(false);
}
return ret;
}
// Note: What the comments here say about JScript possibly holds for Automation clients in general, // like VBScript ones, too. Or not. Hard to say. What is the relevance of JScript nowadays anyway, // and can LO really be used from JScript code on web pages any longer?
// "convertDispparamsArgs" converts VARIANTS to their respecting Any counterparts // The parameters "id", "wFlags" and "pdispparams" equal those as used in // IDispatch::Invoke. The function handles special JavaScript // cases where a VARIANT of type VT_DISPATCH is ambiguous and could represent // an object, array ( JavaScript Array object), out parameter and in/out ( JavaScript Array object) // parameter (JavaScript Array object) // Because all those VT_DISPATCH objects need a different conversion // we have to find out what the object is supposed to be. The function does this // by either using type information or by help of a specialized ValueObject object.
// A. Type Information
// With the help of type information the kind of parameter can be exactly determined // and an appropriate conversion can be chosen. A problem arises if a method expects // an Any. Then the type info does not tell what the type of the value, that is kept // by the any, should be. In this situation the decision whether the param is a // sequence or an object is made upon the fact if the object has a property "0" // ( see function "isJScriptArray"). Since this is unsafe it is recommended to use // the JScript value objects within a JScript script on such an occasion.
// B. JavaScript Value Object ( class JScriptValue )
// A JScriptValue (ValueObject) object is a COM object in that it implements IDispatch and the // IJScriptValue object interface. Such objects are provided by all UNO wrapper // objects used within a JScript script. To obtain an instance one has to call // "_GetValueObject() or Bridge_GetValueObject()" on a UNO wrapper object (class InterfaceOleWrapper). // A value object is appropriately initialized within the script and passed as // parameter to a UNO object method or property. The convertDispparamsArgs function // can easily find out that a param is such an object by querying for the // IJScriptValue interface. By this interface one the type and kind ( out, in/out) // can be determined and the right conversion can be applied. // Using ValueObjects we spare us the effort of acquiring and examining type information // in order to figure out what the an IDispatch parameter is meant for.
// Normal JScript object parameter can be mixed with JScriptValue object. If an // VARIANT contains a VT_DISPATCH that is no JScriptValue than the type information // is used to find out about the required type. void InterfaceOleWrapper::convertDispparamsArgs(DISPID id, unsignedshort/*wFlags*/, DISPPARAMS* pdispparams, Sequence<Any>& rSeq)
{ // Parameters come in reverse order in pdispparams. There might be less parameters than // expected. In that case, assume they are "optional" (but can't be marked as such in UNO IDL), // and fill in the rest with empty Anys. There might also be more than expected. In that case, // assume the oovbaapi UNO IDL hasn't kept up with added optional parameters in MSO, and just // ignore the extra ones, as long as they are empty.
// An example: incoming parameters: <12, 13, "foo/bar.tem"> // // Expected parameters: (string filename, int something, int somethingElse, Any whatever, Any // whateverElse) // // Here the existing incoming parameters are placed in reverse order in the first three outgoing // parameters, and the rest of the outgoing parameters are kept as empty Anys. // // Another example: incoming parameters: <EMPTY, TRUE> // // Expected parameters: (bool flag) // // Here the TRUE is passed as the sole outgoing parameter, and the incoming EMPTY is ignored. // // Still an example: incoming parameters: <"foo.doc", TRUE> // // Expected parameters: (bool flag) // // This throws an error as the incoming string parameter presumably should do something important, // but there is no corresponding outgoing parameter.
//Get type information for the current call
InvocationInfo info; if( ! getInvocationInfoForCall( id, info)) throw BridgeRuntimeError( "[automation bridge]InterfaceOleWrapper::convertDispparamsArgs \n" "Could not obtain type information for current call.");
// Size rSeq according to the number of expected parameters. constint expectedArgs = info.aParamTypes.getLength() + (info.eMemberType == MemberType_PROPERTY ? 1 : 0);
rSeq.realloc( expectedArgs );
Any* pParams = rSeq.getArray();
Any anyParam;
int outgoingArgIndex = 0;
// Go through incoming parameters in reverse order, i.e. in the order as declared in IDL for (int i = std::max(countIncomingArgs, expectedArgs) - 1; i >= 0; i--)
{ // Ignore too many parameters if they are VT_EMPTY anyway if ( outgoingArgIndex >= expectedArgs && pdispparams->rgvarg[i].vt == VT_EMPTY ) continue;
// But otherwise too many parameters is an error if ( outgoingArgIndex >= expectedArgs ) throw BridgeRuntimeError( "[automation bridge] Too many parameters" );
if (i < countIncomingArgs)
{ // A missing (and hopefully optional) arg (in the middle of the argument list) is passed // as an empty Any. if (pdispparams->rgvarg[i].vt == VT_ERROR && pdispparams->rgvarg[i].scode == DISP_E_PARAMNOTFOUND)
{
Any aEmpty;
pParams[ outgoingArgIndex ] = aEmpty;
outgoingArgIndex++; continue;
}
if(convertValueObject( & pdispparams->rgvarg[i], anyParam))
{ //a param is a ValueObject and could be converted
pParams[ outgoingArgIndex ] = anyParam;
outgoingArgIndex++; continue;
}
} else
{ // A missing arg. Let's hope it is de facto optional (there is no way in UNO IDL to mark // a parameter as optional). The corresponding slot in pParams is already a void Any. // Here we don't increase outgoingArgIndex! continue;
}
// If the param is an out, in/out parameter in // JScript (Array object, with value at index 0) then we // extract Array[0] and put the value into varParam. At the end of the loop varParam // is converted if it contains a value otherwise the VARIANT from // DISPPARAMS is converted.
CComVariant varParam;
// Check for JScript out and in/out paramsobjects (VT_DISPATCH). // To find them out we use typeinformation of the function being called.
// No idea how this stuff, originally written for JScript, works for other Automation // clients.
if( pdispparams->rgvarg[i].vt == VT_DISPATCH )
{ if( info.eMemberType == MemberType_METHOD && info.aParamModes[ outgoingArgIndex ] == ParamMode_INOUT)
{ // INOUT-param // Index ( property) "0" contains the actual IN-param. The object is a JScript // Array object. // Get the IN-param at index "0"
IDispatch* pdisp= pdispparams->rgvarg[i].pdispVal;
OLECHAR const * sindex= L"0";
DISPID id2;
DISPPARAMS noParams= {nullptr,nullptr,0,0}; if(SUCCEEDED( hr= pdisp->GetIDsOfNames( IID_NULL, const_cast<OLECHAR **>(&sindex), 1, LOCALE_USER_DEFAULT, &id2)))
hr= pdisp->Invoke( id2, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYGET,
& noParams, & varParam, nullptr, nullptr); if( FAILED( hr))
{ throw BridgeRuntimeError( "[automation bridge] Could not determine " "if the object has a member \"0\". Error: " +
OUString::number(hr));
}
}
}
if( varParam.vt == VT_EMPTY) // then it was no in/out parameter
varParam= pdispparams->rgvarg[i];
if( !m_xInvocation.is() )returnfalse;
Reference<XInvocation2> inv2( m_xInvocation, UNO_QUERY); if( inv2.is())
{ // We need the name of the property or method to get its type information. // The name can be identified through the param "id" // that is kept as value in the map m_nameToDispIdMap. // Problem: the Windows JScript engine sometimes changes small letters to capital // letters as happens in xidlclass_obj.createObject( var) // in JScript. // IDispatch::GetIdsOfNames is then called with "CreateObject" !!! // m_nameToDispIdMap can contain several names for one DISPID but only one is // the exact one. If there's no m_xExactName and therefore no exact name then // there's only one entry in the map.
OUString sMemberName;
auto ci1 = std::find_if(m_nameToDispIdMap.cbegin(), m_nameToDispIdMap.cend(),
[&id](const NameToIdMap::value_type& nameToDispId) { return nameToDispId.second == id; }); // item is a pair<OUString, DISPID> if (ci1 != m_nameToDispIdMap.cend())
sMemberName= (*ci1).first; // Get information for the current call ( property or method). // There could be similar names which only differ in the cases // of letters. First we assume that the name which was passed into // GetIDsOfNames is correct. If we won't get information with that // name then we have the invocation service use the XExactName interface. bool validInfo= true;
InvocationInfo invInfo; try{
invInfo= inv2->getInfoForName( sMemberName, false);
} catch(const IllegalArgumentException&)
{
validInfo= false;
}
// XBridgeSupplier2 --------------------------------------------------- // only bridges itself ( this instance of InterfaceOleWrapper)from UNO to IDispatch // If sourceModelType is UNO than any UNO interface implemented by InterfaceOleWrapper // can bridged to IDispatch ( if destModelType == OLE). The IDispatch is // implemented by this class.
Any SAL_CALL InterfaceOleWrapper::createBridge(const Any& modelDepObject, const Sequence<sal_Int8>& /*ProcessId*/,
sal_Int16 sourceModelType,
sal_Int16 destModelType)
{
// "getType" is used in convertValueObject to map the string denoting the type // to an actual Type object. bool getType( const BSTR name, Type & type)
{ bool ret = false;
typelib_TypeDescription * pDesc= nullptr;
OUString str(o3tl::toU(name));
typelib_typedescription_getByName( &pDesc, str.pData ); if( pDesc)
{
type = Type( pDesc->pWeakRef );
typelib_typedescription_release( pDesc);
ret = true;
} return ret;
}
// special Handling for a JScriptValue object
CComQIPtr<IJScriptValueObject> spValueDest(spDispDest); if (spValueDest)
{
VARIANT_BOOL varBool= VARIANT_FALSE; if ((SUCCEEDED(hr = spValueDest->IsOutParam(&varBool))
&& varBool == VARIANT_TRUE)
|| (SUCCEEDED(hr = spValueDest->IsInOutParam(&varBool))
&& varBool == VARIANT_TRUE))
{ if( SUCCEEDED( spValueDest->Set( CComVariant(), *pSource)))
ret= true;
}
} elseif (pDest->vt == VT_DISPATCH)// VT_DISPATCH -> JScript out param
{ // We use IDispatchEx because its GetDispID function causes the creation // of a property if it does not exist already. This is convenient for // out parameters in JScript. Then the user must not specify property "0" // explicitly
CComQIPtr<IDispatchEx> spDispEx( spDispDest); if( spDispEx)
{
CComBSTR nullProp(L"0");
DISPID dwDispID; if( SUCCEEDED( spDispEx->GetDispID( nullProp, fdexNameEnsure, &dwDispID)))
{
DISPPARAMS dispparams = {nullptr, nullptr, 1, 1};
dispparams.rgvarg = pSource;
DISPID dispidPut = DISPID_PROPERTYPUT;
dispparams.rgdispidNamedArgs = &dispidPut;
if (pSource->vt == VT_UNKNOWN || pSource->vt == VT_DISPATCH ||
(pSource->vt & VT_ARRAY) || (pSource->vt & VT_BYREF))
hr = spDispEx->InvokeEx(dwDispID, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUTREF,
&dispparams, nullptr, nullptr, nullptr); else
hr= spDispEx->InvokeEx(dwDispID, LOCALE_USER_DEFAULT, DISPATCH_PROPERTYPUT,
&dispparams, nullptr, nullptr, nullptr); if( SUCCEEDED(hr))
ret= true;
}
}
} else
ret= writeBackOutParameter( pDest, pSource);
} else// The param can't be a JScript out-parameter ( an Array object), it could be a VBScript
{ // param. The function checks itself for correct VBScript params
ret= writeBackOutParameter( pDest, pSource);
} return ret;
}
// VisualBasic Script passes arguments as VT_VARIANT | VT_BYREF be it in or out parameter. // Thus we are in charge of freeing an eventual value contained by the inner VARIANT // Please note: VariantCopy doesn't free a VT_BYREF value // The out parameters are expected to have always a valid type staticbool writeBackOutParameter(VARIANTARG* pDest, VARIANT* pSource)
{
HRESULT hr; bool ret = false; // Out parameter must be VT_BYREF if ((V_VT(pDest) & VT_BYREF) != 0 )
{
VARTYPE oleTypeFlags = V_VT(pSource);
// if caller accept VARIANT as out parameter, any value must be converted if (V_VT(pDest) == (VT_VARIANT | VT_BYREF))
{ // When the user provides a VARIANT rather than a concrete type // we just copy the source to the out, in/out parameter // VT_DISPATCH, VT_UNKNOWN, VT_ARRAY, VT_BSTR in the VARIANT that // is contained in pDest are released by VariantCopy
VariantCopy(V_VARIANTREF(pDest), pSource);
ret = true;
} else
{ // variantarg and variant must have same type if ((V_VT(pDest) & oleTypeFlags) == oleTypeFlags)
{ if ((oleTypeFlags & VT_ARRAY) != 0)
{ // In / Out Param if( *V_ARRAYREF(pDest) != nullptr)
hr= SafeArrayCopyData( V_ARRAY(pSource), *V_ARRAYREF(pDest)); else // Out Param
hr= SafeArrayCopy(V_ARRAY(pSource), V_ARRAYREF(pDest)); if( SUCCEEDED( hr))
ret = true;
} else
{ // copy base type switch (V_VT(pSource))
{ case VT_I2:
{
*V_I2REF(pDest) = V_I2(pSource);
ret = true; break;
} case VT_I4:
*V_I4REF(pDest) = V_I4(pSource);
ret = true; break; case VT_R4:
*V_R4REF(pDest) = V_R4(pSource);
ret = true; break; case VT_R8:
*V_R8REF(pDest) = V_R8(pSource);
ret = true; break; case VT_CY:
*V_CYREF(pDest) = V_CY(pSource);
ret = true; break; case VT_DATE:
*V_DATEREF(pDest) = V_DATE(pSource);
ret = true; break; case VT_BSTR:
SysFreeString( *pDest->pbstrVal);
*V_BSTRREF(pDest) = SysAllocString(V_BSTR(pSource));
ret = true; break; case VT_DISPATCH: if (*V_DISPATCHREF(pDest) != nullptr)
(*V_DISPATCHREF(pDest))->Release();
*V_DISPATCHREF(pDest) = V_DISPATCH(pSource);
if (*V_DISPATCHREF(pDest) != nullptr)
(*V_DISPATCHREF(pDest))->AddRef();
ret = true; break; case VT_ERROR:
*V_ERRORREF(pDest) = V_ERROR(pSource);
ret = true; break; case VT_BOOL:
*V_BOOLREF(pDest) = V_BOOL(pSource);
ret = true; break; case VT_UNKNOWN: if (*V_UNKNOWNREF(pDest) != nullptr)
(*V_UNKNOWNREF(pDest))->Release();
*V_UNKNOWNREF(pDest) = V_UNKNOWN(pSource);
if (*V_UNKNOWNREF(pDest) != nullptr)
(*V_UNKNOWNREF(pDest))->AddRef();
ret = true; break; case VT_I1:
*V_I1REF(pDest) = V_I1(pSource);
ret = true; break; case VT_UI1:
*V_UI1REF(pDest) = V_UI1(pSource);
ret = true; break; case VT_UI2:
--> --------------------
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.