/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * 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 . */ #include #include #include "cppuhelper/bootstrap.hxx" #include "rtl/process.h" #include "typelib/typedescription.hxx" #include "com/sun/star/bridge/ModelDependent.hpp" #include "com/sun/star/bridge/XBridgeSupplier2.hpp" #include "com/sun/star/uno/TypeClass.hpp" #include "com/sun/star/script/XInvocation.hpp" #include "com/sun/star/lang/XMultiServiceFactory.hpp" #include "com/sun/star/uno/XComponentContext.hpp" #include #include "rtl/ustring.hxx" using namespace com::sun::star::bridge; using namespace com::sun::star::bridge::ModelDependent; using namespace com::sun::star::uno; using namespace com::sun::star::lang; using namespace com::sun::star::script; using namespace com::sun::star::bridge::oleautomation; using namespace cppu; template< class T > bool equalSequences(const Sequence& seqIn, const Sequence & returned); Reference< XMultiServiceFactory > objectFactory; Reference getMultiServiceFactory() { static Reference< XMultiServiceFactory > factory; if( ! objectFactory.is() ) { Reference context = defaultBootstrap_InitialComponentContext(); factory = Reference( context->getServiceManager(), UNO_QUERY); } return factory; } Reference getComObject( OUString progId) { HRESULT hr= S_OK; Reference< XInvocation > ret; // Reference fac; if( ! objectFactory.is()) { Reference mgr= getMultiServiceFactory(); Reference xInt= mgr->createInstance( OUString(L"com.sun.star.bridge.oleautomation.Factory")); objectFactory= Reference::query( xInt); } if( objectFactory.is()) { Reference xIntAx= objectFactory->createInstance( progId.getStr()); if( xIntAx.is() ) { Reference< XInvocation > xInv( xIntAx, UNO_QUERY); ret= xInv; } } return ret; } Reference convertComObject( IUnknown* pUnk) { Reference< XMultiServiceFactory > mgr= getMultiServiceFactory(); Reference< XInterface > xIntSupplier= mgr->createInstance(OUString(L"com.sun.star.bridge.OleBridgeSupplier2")); Reference< XBridgeSupplier2 > xSuppl( xIntSupplier, UNO_QUERY); Any any; CComVariant var( pUnk); any <<= ( sal_uInt32)&var; sal_uInt8 arId[16]; rtl_getGlobalProcessId( arId); Any target= xSuppl->createBridge( any, Sequence( (sal_Int8*)arId, 16), OLE, UNO ); Reference ret; target>>= ret; return ret; } /* Parameter values contains the expected return values. The value at index 0 correspond to parameter 0 (left - most). For parameters which are not out or in/out the value must be a void any. The number of items in value must be the same as the number of provided parameter during the call on the method. The parameter outArgs, indices correspond to the sequences which are arguments to XInvocation::invoke */ bool checkOutArgs(const Sequence & outArgs, const Sequence & indices, const Sequence & values) { if (values.getLength() != outArgs.getLength()) return false; //iterate over all parameters. i represents the parameter index for (int i = 0; i < values.getLength(); i++) { if (values[i].getValueType() == cppu::UnoType::get()) continue; //out parameter //Based on the parameter index find the correspondent out value int indexOutSeq = -1; for (int iIndices = indices.getLength() - 1; iIndices >= 0; iIndices --) { if (indices[iIndices] == i) { indexOutSeq = iIndices; break; } } if (indexOutSeq == -1) return false; Any value; Any out; values[i] >>= value; outArgs[indexOutSeq] >>=out; NamedArgument naVal; NamedArgument naOut; value >>= naVal; out >>= naOut; if (values[i].getValueType() == cppu::UnoType::get()) { NamedArgument inNamed; values[i] >>= inNamed; value <<= inNamed.Value; } if (value != outArgs[indexOutSeq]) return false; } return true; } /* The returned sequence always contains Any elements */ bool equalSequences(const Any& orig, const Any& returned) { if (orig.getValueTypeClass() != TypeClass_SEQUENCE) { OSL_ASSERT(0); return false; } TypeDescription td(orig.getValueTypeRef()); typelib_IndirectTypeDescription * indirect_td = (typelib_IndirectTypeDescription *) td.get(); switch (indirect_td->pType->eTypeClass) { case TypeClass_CHAR: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_BOOLEAN: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_BYTE: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_SHORT: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_LONG: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_FLOAT: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_DOUBLE: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_STRING: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_ANY: { Sequence seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } case TypeClass_SEQUENCE: { //Sequence seq; //orig >>= seq; //Sequence seq2; //returned >>= seq2; //return equalSequences(seq, seq2); break; } case TypeClass_INTERFACE: { Sequence > seq; orig >>= seq; Sequence seq2; returned >>= seq2; return equalSequences(seq, seq2); } default: return false; } return false; } template< class T > bool equalSequences(const Sequence& seqIn, const Sequence & seqOut) { if (seqIn.getLength() != seqOut.getLength()) return false; int len = seqIn.getLength(); for (int i = 0; i < len; i++) { Any anyIn; anyIn <<= seqIn[i]; Any anyOut = seqOut[i]; if (anyIn != anyOut) return false; } return true; } void printSequence( Sequence& val) { // typelib_TypeDescription* desc; // val.getValueTypeDescription( &desc); // typelib_typedescription_release( desc); USES_CONVERSION; char buff[1024]; buff[0]=0; char tmpBuf[1024]; tmpBuf[0]=0; sal_Int32 i; for( i=0; i< val.getLength(); i++) { Any& elem= val[i]; switch ( elem.getValueTypeClass()) { case TypeClass_BYTE: sprintf( tmpBuf, "sal_Int8 %d \n", *(sal_Int8*)elem.getValue()); break; case TypeClass_SHORT: sprintf( tmpBuf, "sal_Int16 %d \n", *(sal_Int16*)elem.getValue()); break; case TypeClass_LONG: sprintf( tmpBuf, "sal_Int32 %d \n", *(sal_Int32*)elem.getValue()); break; case TypeClass_DOUBLE: sprintf( tmpBuf, "double %f \n", *(double*)elem.getValue()); break; case TypeClass_FLOAT: sprintf( tmpBuf, "float %f \n", *(float*)elem.getValue()); break; case TypeClass_STRING: sprintf( tmpBuf, "%S \n", (*(OUString*)elem.getValue()).getStr()); break; case TypeClass_INTERFACE: { // we assume that the interface is XInvocation of a AxTestControls.Basic component. Reference inv; elem>>= inv; if( inv.is()) { Any prpVal= inv->getValue( OUString( L"prpString")); sprintf( tmpBuf, "Property prpString: %S \n", (*(OUString*)prpVal.getValue()).getStr()); } break; } default:break; } strcat( buff, tmpBuf); } MessageBox( NULL, A2T(buff), _T("clientTest: printing Sequence elements"), MB_OK); } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */