1*2a97ec55SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*2a97ec55SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*2a97ec55SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*2a97ec55SAndrew Rist  * distributed with this work for additional information
6*2a97ec55SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*2a97ec55SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*2a97ec55SAndrew Rist  * "License"); you may not use this file except in compliance
9*2a97ec55SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*2a97ec55SAndrew Rist  *
11*2a97ec55SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*2a97ec55SAndrew Rist  *
13*2a97ec55SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*2a97ec55SAndrew Rist  * software distributed under the License is distributed on an
15*2a97ec55SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*2a97ec55SAndrew Rist  * KIND, either express or implied.  See the License for the
17*2a97ec55SAndrew Rist  * specific language governing permissions and limitations
18*2a97ec55SAndrew Rist  * under the License.
19*2a97ec55SAndrew Rist  *
20*2a97ec55SAndrew Rist  *************************************************************/
21*2a97ec55SAndrew Rist 
22*2a97ec55SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_extensions.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <atlbase.h>
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir #include "cppuhelper/bootstrap.hxx"
30cdf0e10cSrcweir #include "rtl/process.h"
31cdf0e10cSrcweir #include "typelib/typedescription.hxx"
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include "com/sun/star/bridge/ModelDependent.hpp"
34cdf0e10cSrcweir #include "com/sun/star/bridge/XBridgeSupplier2.hpp"
35cdf0e10cSrcweir #include "com/sun/star/uno/TypeClass.hpp"
36cdf0e10cSrcweir #include "com/sun/star/script/XInvocation.hpp"
37cdf0e10cSrcweir #include "com/sun/star/lang/XMultiServiceFactory.hpp"
38cdf0e10cSrcweir #include "com/sun/star/uno/XComponentContext.hpp"
39cdf0e10cSrcweir #include <com/sun/star/bridge/oleautomation/NamedArgument.hpp>
40cdf0e10cSrcweir #include "rtl/ustring.hxx"
41cdf0e10cSrcweir 
42cdf0e10cSrcweir using namespace com::sun::star::bridge;
43cdf0e10cSrcweir using namespace com::sun::star::bridge::ModelDependent;
44cdf0e10cSrcweir using namespace com::sun::star::uno;
45cdf0e10cSrcweir using namespace com::sun::star::lang;
46cdf0e10cSrcweir using namespace com::sun::star::script;
47cdf0e10cSrcweir using namespace com::sun::star::bridge::oleautomation;
48cdf0e10cSrcweir using namespace rtl;
49cdf0e10cSrcweir using namespace cppu;
50cdf0e10cSrcweir 
51cdf0e10cSrcweir template< class T >
52cdf0e10cSrcweir bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & returned);
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir Reference< XMultiServiceFactory > objectFactory;//
56cdf0e10cSrcweir 
57cdf0e10cSrcweir 
getMultiServiceFactory()58cdf0e10cSrcweir Reference<XMultiServiceFactory> getMultiServiceFactory()
59cdf0e10cSrcweir {
60cdf0e10cSrcweir 	static Reference< XMultiServiceFactory > factory;
61cdf0e10cSrcweir 	if( ! objectFactory.is() )
62cdf0e10cSrcweir 	{
63cdf0e10cSrcweir 		Reference<XComponentContext> context = defaultBootstrap_InitialComponentContext();
64cdf0e10cSrcweir 		factory = Reference<XMultiServiceFactory>( context->getServiceManager(), UNO_QUERY);
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	}
67cdf0e10cSrcweir 	return factory;
68cdf0e10cSrcweir }
69cdf0e10cSrcweir 
getComObject(OUString progId)70cdf0e10cSrcweir Reference<XInvocation> getComObject( OUString progId)
71cdf0e10cSrcweir {
72cdf0e10cSrcweir 	HRESULT hr= S_OK;
73cdf0e10cSrcweir 	Reference< XInvocation > ret;
74cdf0e10cSrcweir //	Reference<XMultiServiceFactory> fac;
75cdf0e10cSrcweir 	if(  ! objectFactory.is())
76cdf0e10cSrcweir 	{	Reference<XMultiServiceFactory> mgr= getMultiServiceFactory();
77cdf0e10cSrcweir 		Reference<XInterface> xInt= mgr->createInstance(
78cdf0e10cSrcweir 			OUString(L"com.sun.star.bridge.oleautomation.Factory"));
79cdf0e10cSrcweir 		objectFactory= Reference<XMultiServiceFactory>::query(  xInt);
80cdf0e10cSrcweir 	}
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 	if( objectFactory.is())
83cdf0e10cSrcweir 	{
84cdf0e10cSrcweir 		Reference<XInterface> xIntAx= objectFactory->createInstance( progId.getStr());
85cdf0e10cSrcweir 		if( xIntAx.is() )
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir 			Reference< XInvocation > xInv( xIntAx, UNO_QUERY);
88cdf0e10cSrcweir 			ret= xInv;
89cdf0e10cSrcweir 		}
90cdf0e10cSrcweir 	}
91cdf0e10cSrcweir 	return ret;
92cdf0e10cSrcweir }
93cdf0e10cSrcweir 
convertComObject(IUnknown * pUnk)94cdf0e10cSrcweir Reference<XInvocation> convertComObject( IUnknown* pUnk)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir 	Reference< XMultiServiceFactory > mgr= getMultiServiceFactory();
97cdf0e10cSrcweir 	Reference< XInterface > xIntSupplier= mgr->createInstance(OUString(L"com.sun.star.bridge.OleBridgeSupplier2"));
98cdf0e10cSrcweir 	Reference< XBridgeSupplier2 > xSuppl( xIntSupplier, UNO_QUERY);
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 	Any any;
101cdf0e10cSrcweir 	CComVariant var( pUnk);
102cdf0e10cSrcweir 	any <<= ( sal_uInt32)&var;
103cdf0e10cSrcweir 	sal_uInt8 arId[16];
104cdf0e10cSrcweir 	rtl_getGlobalProcessId( arId);
105cdf0e10cSrcweir 	Any target=	xSuppl->createBridge( any, Sequence<sal_Int8>( (sal_Int8*)arId, 16), OLE, UNO );
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 	Reference<XInvocation> ret;
108cdf0e10cSrcweir 	target>>= ret;
109cdf0e10cSrcweir 	return ret;
110cdf0e10cSrcweir }
111cdf0e10cSrcweir 
112cdf0e10cSrcweir /*
113cdf0e10cSrcweir   Parameter values contains the expected return values. The value at index 0
114cdf0e10cSrcweir   correspond to parameter 0 (left - most). For parameters which are not out or
115cdf0e10cSrcweir   in/out the value must be a void any.
116cdf0e10cSrcweir 
117cdf0e10cSrcweir   The number of items in value must be the
118cdf0e10cSrcweir   same as the number of provided parameter during the  call on the  method.
119cdf0e10cSrcweir 
120cdf0e10cSrcweir   The parameter outArgs, indices correspond to the sequences which are
121cdf0e10cSrcweir   arguments to XInvocation::invoke
122cdf0e10cSrcweir  */
checkOutArgs(const Sequence<Any> & outArgs,const Sequence<sal_Int16> & indices,const Sequence<Any> & values)123cdf0e10cSrcweir bool checkOutArgs(const Sequence<Any> & outArgs,
124cdf0e10cSrcweir                   const Sequence<sal_Int16> & indices, const Sequence<Any> & values)
125cdf0e10cSrcweir {
126cdf0e10cSrcweir     if (values.getLength() != outArgs.getLength())
127cdf0e10cSrcweir         return false;
128cdf0e10cSrcweir     //iterate over all parameters. i represents the parameter index
129cdf0e10cSrcweir     for (int i = 0; i < values.getLength(); i++)
130cdf0e10cSrcweir     {
131cdf0e10cSrcweir         if (values[i].getValueType() == getVoidCppuType())
132cdf0e10cSrcweir             continue;
133cdf0e10cSrcweir         //out parameter
134cdf0e10cSrcweir         //Based on the parameter index find the correspondent out value
135cdf0e10cSrcweir         int indexOutSeq = -1;
136cdf0e10cSrcweir         for (int iIndices = indices.getLength() - 1; iIndices >= 0; iIndices --)
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             if (indices[iIndices] == i)
139cdf0e10cSrcweir             {
140cdf0e10cSrcweir                 indexOutSeq = iIndices;
141cdf0e10cSrcweir                 break;
142cdf0e10cSrcweir             }
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir         if (indexOutSeq == -1)
145cdf0e10cSrcweir             return false;
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 		Any value;
148cdf0e10cSrcweir 		Any out;
149cdf0e10cSrcweir 		values[i] >>= value;
150cdf0e10cSrcweir 		outArgs[indexOutSeq] >>=out;
151cdf0e10cSrcweir 		NamedArgument naVal;
152cdf0e10cSrcweir 		NamedArgument naOut;
153cdf0e10cSrcweir 		value >>= naVal;
154cdf0e10cSrcweir 		out >>= naOut;
155cdf0e10cSrcweir         if (values[i].getValueType() == getCppuType((NamedArgument *) 0))
156cdf0e10cSrcweir         {
157cdf0e10cSrcweir             NamedArgument inNamed;
158cdf0e10cSrcweir             values[i] >>= inNamed;
159cdf0e10cSrcweir             value <<= inNamed.Value;
160cdf0e10cSrcweir         }
161cdf0e10cSrcweir 		if (value != outArgs[indexOutSeq])
162cdf0e10cSrcweir             return false;
163cdf0e10cSrcweir     }
164cdf0e10cSrcweir     return true;
165cdf0e10cSrcweir }
166cdf0e10cSrcweir 
167cdf0e10cSrcweir /* The returned sequence always contains Any elements
168cdf0e10cSrcweir */
equalSequences(const Any & orig,const Any & returned)169cdf0e10cSrcweir bool equalSequences(const Any& orig, const Any& returned)
170cdf0e10cSrcweir {
171cdf0e10cSrcweir 	if (orig.getValueTypeClass() != TypeClass_SEQUENCE)
172cdf0e10cSrcweir 	{
173cdf0e10cSrcweir 		OSL_ASSERT(0);
174cdf0e10cSrcweir 		return false;
175cdf0e10cSrcweir 	}
176cdf0e10cSrcweir 	TypeDescription td(orig.getValueTypeRef());
177cdf0e10cSrcweir 	typelib_IndirectTypeDescription * indirect_td = (typelib_IndirectTypeDescription *) td.get();
178cdf0e10cSrcweir 
179cdf0e10cSrcweir 	switch (indirect_td->pType->eTypeClass)
180cdf0e10cSrcweir 	{
181cdf0e10cSrcweir 		case TypeClass_CHAR:
182cdf0e10cSrcweir 			{
183cdf0e10cSrcweir 				Sequence<sal_Unicode> seq;
184cdf0e10cSrcweir 				orig >>= seq;
185cdf0e10cSrcweir 				Sequence<Any> seq2;
186cdf0e10cSrcweir 				returned >>= seq2;
187cdf0e10cSrcweir 				return equalSequences(seq, seq2);
188cdf0e10cSrcweir 			}
189cdf0e10cSrcweir 		case TypeClass_BOOLEAN:
190cdf0e10cSrcweir 			{
191cdf0e10cSrcweir 				Sequence<sal_Bool> seq;
192cdf0e10cSrcweir 				orig >>= seq;
193cdf0e10cSrcweir 				Sequence<Any> seq2;
194cdf0e10cSrcweir 				returned >>= seq2;
195cdf0e10cSrcweir 				return equalSequences(seq, seq2);
196cdf0e10cSrcweir 			}
197cdf0e10cSrcweir 		case TypeClass_BYTE:
198cdf0e10cSrcweir 			{
199cdf0e10cSrcweir 				Sequence<sal_Int8> seq;
200cdf0e10cSrcweir 				orig >>= seq;
201cdf0e10cSrcweir 				Sequence<Any> seq2;
202cdf0e10cSrcweir 				returned >>= seq2;
203cdf0e10cSrcweir 				return equalSequences(seq, seq2);
204cdf0e10cSrcweir 			}
205cdf0e10cSrcweir 		case TypeClass_SHORT:
206cdf0e10cSrcweir 			{
207cdf0e10cSrcweir 				Sequence<sal_Int16> seq;
208cdf0e10cSrcweir 				orig >>= seq;
209cdf0e10cSrcweir 				Sequence<Any> seq2;
210cdf0e10cSrcweir 				returned >>= seq2;
211cdf0e10cSrcweir 				return equalSequences(seq, seq2);
212cdf0e10cSrcweir 			}
213cdf0e10cSrcweir 		case TypeClass_LONG:
214cdf0e10cSrcweir 			{
215cdf0e10cSrcweir 				Sequence<sal_Int32> seq;
216cdf0e10cSrcweir 				orig >>= seq;
217cdf0e10cSrcweir 				Sequence<Any> seq2;
218cdf0e10cSrcweir 				returned >>= seq2;
219cdf0e10cSrcweir 				return equalSequences(seq, seq2);
220cdf0e10cSrcweir 			}
221cdf0e10cSrcweir 		case TypeClass_FLOAT:
222cdf0e10cSrcweir 			{
223cdf0e10cSrcweir 				Sequence<float> seq;
224cdf0e10cSrcweir 				orig >>= seq;
225cdf0e10cSrcweir 				Sequence<Any> seq2;
226cdf0e10cSrcweir 				returned >>= seq2;
227cdf0e10cSrcweir 				return equalSequences(seq, seq2);
228cdf0e10cSrcweir 			}
229cdf0e10cSrcweir 		case TypeClass_DOUBLE:
230cdf0e10cSrcweir 			{
231cdf0e10cSrcweir 				Sequence<double> seq;
232cdf0e10cSrcweir 				orig >>= seq;
233cdf0e10cSrcweir 				Sequence<Any> seq2;
234cdf0e10cSrcweir 				returned >>= seq2;
235cdf0e10cSrcweir 				return equalSequences(seq, seq2);
236cdf0e10cSrcweir 			}
237cdf0e10cSrcweir 		case TypeClass_STRING:
238cdf0e10cSrcweir 			{
239cdf0e10cSrcweir 				Sequence<OUString> seq;
240cdf0e10cSrcweir 				orig >>= seq;
241cdf0e10cSrcweir 				Sequence<Any> seq2;
242cdf0e10cSrcweir 				returned >>= seq2;
243cdf0e10cSrcweir 				return equalSequences(seq, seq2);
244cdf0e10cSrcweir 			}
245cdf0e10cSrcweir 		case TypeClass_ANY:
246cdf0e10cSrcweir 			{
247cdf0e10cSrcweir 				Sequence<Any> seq;
248cdf0e10cSrcweir 				orig >>= seq;
249cdf0e10cSrcweir 				Sequence<Any> seq2;
250cdf0e10cSrcweir 				returned >>= seq2;
251cdf0e10cSrcweir 				return equalSequences(seq, seq2);
252cdf0e10cSrcweir 			}
253cdf0e10cSrcweir 		case TypeClass_SEQUENCE:
254cdf0e10cSrcweir 			{
255cdf0e10cSrcweir 				//Sequence<sal_Unicode> seq;
256cdf0e10cSrcweir 				//orig >>= seq;
257cdf0e10cSrcweir 				//Sequence<Any> seq2;
258cdf0e10cSrcweir 				//returned >>= seq2;
259cdf0e10cSrcweir 				//return equalSequences(seq, seq2);
260cdf0e10cSrcweir 				break;
261cdf0e10cSrcweir 			}
262cdf0e10cSrcweir 		case TypeClass_INTERFACE:
263cdf0e10cSrcweir 			{
264cdf0e10cSrcweir 				Sequence<Reference<XInvocation> > seq;
265cdf0e10cSrcweir 				orig >>= seq;
266cdf0e10cSrcweir 				Sequence<Any> seq2;
267cdf0e10cSrcweir 				returned >>= seq2;
268cdf0e10cSrcweir 				return equalSequences(seq, seq2);
269cdf0e10cSrcweir 			}
270cdf0e10cSrcweir 		default:
271cdf0e10cSrcweir 			return false;
272cdf0e10cSrcweir 	}
273cdf0e10cSrcweir     return false;
274cdf0e10cSrcweir }
275cdf0e10cSrcweir 
276cdf0e10cSrcweir template< class T >
equalSequences(const Sequence<T> & seqIn,const Sequence<Any> & seqOut)277cdf0e10cSrcweir bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & seqOut)
278cdf0e10cSrcweir {
279cdf0e10cSrcweir 	if (seqIn.getLength() != seqOut.getLength())
280cdf0e10cSrcweir 		return false;
281cdf0e10cSrcweir 	int len = seqIn.getLength();
282cdf0e10cSrcweir 	for (int i = 0; i < len; i++)
283cdf0e10cSrcweir 	{
284cdf0e10cSrcweir 		Any anyIn;
285cdf0e10cSrcweir 		anyIn <<= seqIn[i];
286cdf0e10cSrcweir 		Any anyOut = seqOut[i];
287cdf0e10cSrcweir 		if (anyIn != anyOut)
288cdf0e10cSrcweir 			return false;
289cdf0e10cSrcweir 	}
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 	return true;
292cdf0e10cSrcweir }
293cdf0e10cSrcweir 
printSequence(Sequence<Any> & val)294cdf0e10cSrcweir void printSequence( Sequence<Any>& val)
295cdf0e10cSrcweir {
296cdf0e10cSrcweir 
297cdf0e10cSrcweir //	typelib_TypeDescription* desc;
298cdf0e10cSrcweir //	val.getValueTypeDescription( &desc);
299cdf0e10cSrcweir //	typelib_typedescription_release( desc);
300cdf0e10cSrcweir 
301cdf0e10cSrcweir 	USES_CONVERSION;
302cdf0e10cSrcweir 	char buff[1024];
303cdf0e10cSrcweir 	buff[0]=0;
304cdf0e10cSrcweir 	char tmpBuf[1024];
305cdf0e10cSrcweir 	tmpBuf[0]=0;
306cdf0e10cSrcweir 	sal_Int32 i;
307cdf0e10cSrcweir 
308cdf0e10cSrcweir 	for( i=0; i< val.getLength(); i++)
309cdf0e10cSrcweir 	{
310cdf0e10cSrcweir 		Any& elem= val[i];
311cdf0e10cSrcweir 		switch ( elem.getValueTypeClass())
312cdf0e10cSrcweir 		{
313cdf0e10cSrcweir 		case TypeClass_BYTE:
314cdf0e10cSrcweir 			 sprintf( tmpBuf, "sal_Int8 %d \n", *(sal_Int8*)elem.getValue());
315cdf0e10cSrcweir 			 break;
316cdf0e10cSrcweir 		case TypeClass_SHORT:
317cdf0e10cSrcweir 			 sprintf( tmpBuf, "sal_Int16 %d \n", *(sal_Int16*)elem.getValue());
318cdf0e10cSrcweir 			 break;
319cdf0e10cSrcweir 		case TypeClass_LONG:
320cdf0e10cSrcweir 			 sprintf( tmpBuf, "sal_Int32 %d \n", *(sal_Int32*)elem.getValue());
321cdf0e10cSrcweir 			 break;
322cdf0e10cSrcweir 		case TypeClass_DOUBLE:
323cdf0e10cSrcweir 			 sprintf( tmpBuf, "double %f \n", *(double*)elem.getValue());
324cdf0e10cSrcweir 			 break;
325cdf0e10cSrcweir 		case TypeClass_FLOAT:
326cdf0e10cSrcweir 			 sprintf( tmpBuf, "float %f \n", *(float*)elem.getValue());
327cdf0e10cSrcweir 			 break;
328cdf0e10cSrcweir 		case TypeClass_STRING:
329cdf0e10cSrcweir 			 sprintf( tmpBuf, "%S \n", (*(OUString*)elem.getValue()).getStr());
330cdf0e10cSrcweir 			 break;
331cdf0e10cSrcweir 		case TypeClass_INTERFACE:
332cdf0e10cSrcweir 			{
333cdf0e10cSrcweir 			// we assume that the interface is XInvocation of a AxTestControls.Basic component.
334cdf0e10cSrcweir 			Reference<XInvocation> inv;
335cdf0e10cSrcweir 			elem>>= inv;
336cdf0e10cSrcweir 			if( inv.is())
337cdf0e10cSrcweir 			{
338cdf0e10cSrcweir 				Any prpVal= inv->getValue( OUString( L"prpString"));
339cdf0e10cSrcweir 				sprintf( tmpBuf, "Property prpString: %S \n", (*(OUString*)prpVal.getValue()).getStr());
340cdf0e10cSrcweir 			}
341cdf0e10cSrcweir 			break;
342cdf0e10cSrcweir 			}
343cdf0e10cSrcweir 		default:break;
344cdf0e10cSrcweir 		}
345cdf0e10cSrcweir 		strcat( buff, tmpBuf);
346cdf0e10cSrcweir 
347cdf0e10cSrcweir 	}
348cdf0e10cSrcweir 
349cdf0e10cSrcweir 	MessageBox( NULL, A2T(buff), _T("clientTest: printing Sequence elements"), MB_OK);
350cdf0e10cSrcweir }
351