bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / test / ole / OleClient / funcs.cxx
bloba72c7c49eba9c0435946e7952fec0e7557679fa1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
21 #include <atlbase.h>
22 #include <stdio.h>
23 #include "cppuhelper/bootstrap.hxx"
24 #include "rtl/process.h"
25 #include "typelib/typedescription.hxx"
27 #include "com/sun/star/bridge/ModelDependent.hpp"
28 #include "com/sun/star/bridge/XBridgeSupplier2.hpp"
29 #include "com/sun/star/uno/TypeClass.hpp"
30 #include "com/sun/star/script/XInvocation.hpp"
31 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
32 #include "com/sun/star/uno/XComponentContext.hpp"
33 #include <com/sun/star/bridge/oleautomation/NamedArgument.hpp>
34 #include "rtl/ustring.hxx"
36 using namespace com::sun::star::bridge;
37 using namespace com::sun::star::bridge::ModelDependent;
38 using namespace com::sun::star::uno;
39 using namespace com::sun::star::lang;
40 using namespace com::sun::star::script;
41 using namespace com::sun::star::bridge::oleautomation;
42 using namespace cppu;
45 template< class T >
46 bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & returned);
49 Reference< XMultiServiceFactory > objectFactory;
52 Reference<XMultiServiceFactory> getMultiServiceFactory()
54 static Reference< XMultiServiceFactory > factory;
55 if( ! objectFactory.is() )
57 Reference<XComponentContext> context = defaultBootstrap_InitialComponentContext();
58 factory.set(context->getServiceManager(), UNO_QUERY);
61 return factory;
64 Reference<XInvocation> getComObject( OUString progId)
66 HRESULT hr= S_OK;
67 Reference< XInvocation > ret;
68 if( ! objectFactory.is())
69 { Reference<XMultiServiceFactory> mgr= getMultiServiceFactory();
70 Reference<XInterface> xInt= mgr->createInstance(
71 "com.sun.star.bridge.oleautomation.Factory");
72 objectFactory.set(xInt, UNO_QUERY);
75 if( objectFactory.is())
77 Reference<XInterface> xIntAx= objectFactory->createInstance( progId.getStr());
78 if( xIntAx.is() )
80 Reference< XInvocation > xInv( xIntAx, UNO_QUERY);
81 ret= xInv;
84 return ret;
87 Reference<XInvocation> convertComObject( IUnknown* pUnk)
89 Reference< XMultiServiceFactory > mgr= getMultiServiceFactory();
90 Reference< XInterface > xIntSupplier= mgr->createInstance("com.sun.star.bridge.OleBridgeSupplier2");
91 Reference< XBridgeSupplier2 > xSuppl( xIntSupplier, UNO_QUERY);
93 Any any;
94 CComVariant var( pUnk);
95 any <<= (sal_uIntPtr) &var;
96 sal_uInt8 arId[16];
97 rtl_getGlobalProcessId( arId);
98 Any target= xSuppl->createBridge( any, Sequence<sal_Int8>( (sal_Int8*)arId, 16), OLE, UNO );
100 Reference<XInvocation> ret;
101 target>>= ret;
102 return ret;
106 Parameter values contains the expected return values. The value at index 0
107 correspond to parameter 0 (left - most). For parameters which are not out or
108 in/out the value must be a void any.
110 The number of items in value must be the
111 same as the number of provided parameter during the call on the method.
113 The parameter outArgs, indices correspond to the sequences which are
114 arguments to XInvocation::invoke
116 bool checkOutArgs(const Sequence<Any> & outArgs,
117 const Sequence<sal_Int16> & indices, const Sequence<Any> & values)
119 if (values.getLength() != outArgs.getLength())
120 return false;
121 //iterate over all parameters. i represents the parameter index
122 for (int i = 0; i < values.getLength(); i++)
124 if (values[i].getValueType() == cppu::UnoType<void>::get())
125 continue;
126 //out parameter
127 //Based on the parameter index find the correspondent out value
128 int indexOutSeq = -1;
129 for (int iIndices = indices.getLength() - 1; iIndices >= 0; iIndices --)
131 if (indices[iIndices] == i)
133 indexOutSeq = iIndices;
134 break;
137 if (indexOutSeq == -1)
138 return false;
140 Any value;
141 Any out;
142 values[i] >>= value;
143 outArgs[indexOutSeq] >>=out;
144 NamedArgument naVal;
145 NamedArgument naOut;
146 value >>= naVal;
147 out >>= naOut;
148 if (values[i].getValueType() == cppu::UnoType<NamedArgument>::get())
150 NamedArgument inNamed;
151 values[i] >>= inNamed;
152 value <<= inNamed.Value;
154 if (value != outArgs[indexOutSeq])
155 return false;
157 return true;
160 /* The returned sequence always contains Any elements
162 bool equalSequences(const Any& orig, const Any& returned)
164 if (orig.getValueTypeClass() != TypeClass_SEQUENCE)
166 OSL_ASSERT(0);
167 return false;
169 TypeDescription td(orig.getValueTypeRef());
170 typelib_IndirectTypeDescription * indirect_td = (typelib_IndirectTypeDescription *) td.get();
172 switch (indirect_td->pType->eTypeClass)
174 case TypeClass_CHAR:
176 Sequence<sal_Unicode> seq;
177 orig >>= seq;
178 Sequence<Any> seq2;
179 returned >>= seq2;
180 return equalSequences(seq, seq2);
182 case TypeClass_BOOLEAN:
184 Sequence<sal_Bool> seq;
185 orig >>= seq;
186 Sequence<Any> seq2;
187 returned >>= seq2;
188 return equalSequences(seq, seq2);
190 case TypeClass_BYTE:
192 Sequence<sal_Int8> seq;
193 orig >>= seq;
194 Sequence<Any> seq2;
195 returned >>= seq2;
196 return equalSequences(seq, seq2);
198 case TypeClass_SHORT:
200 Sequence<sal_Int16> seq;
201 orig >>= seq;
202 Sequence<Any> seq2;
203 returned >>= seq2;
204 return equalSequences(seq, seq2);
206 case TypeClass_LONG:
208 Sequence<sal_Int32> seq;
209 orig >>= seq;
210 Sequence<Any> seq2;
211 returned >>= seq2;
212 return equalSequences(seq, seq2);
214 case TypeClass_FLOAT:
216 Sequence<float> seq;
217 orig >>= seq;
218 Sequence<Any> seq2;
219 returned >>= seq2;
220 return equalSequences(seq, seq2);
222 case TypeClass_DOUBLE:
224 Sequence<double> seq;
225 orig >>= seq;
226 Sequence<Any> seq2;
227 returned >>= seq2;
228 return equalSequences(seq, seq2);
230 case TypeClass_STRING:
232 Sequence<OUString> seq;
233 orig >>= seq;
234 Sequence<Any> seq2;
235 returned >>= seq2;
236 return equalSequences(seq, seq2);
238 case TypeClass_ANY:
240 Sequence<Any> seq;
241 orig >>= seq;
242 Sequence<Any> seq2;
243 returned >>= seq2;
244 return equalSequences(seq, seq2);
246 case TypeClass_SEQUENCE:
248 //Sequence<sal_Unicode> seq;
249 //orig >>= seq;
250 //Sequence<Any> seq2;
251 //returned >>= seq2;
252 //return equalSequences(seq, seq2);
253 break;
255 case TypeClass_INTERFACE:
257 Sequence<Reference<XInvocation> > seq;
258 orig >>= seq;
259 Sequence<Any> seq2;
260 returned >>= seq2;
261 return equalSequences(seq, seq2);
263 default:
264 return false;
266 return false;
269 template< class T >
270 bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & seqOut)
272 if (seqIn.getLength() != seqOut.getLength())
273 return false;
274 int len = seqIn.getLength();
275 for (int i = 0; i < len; i++)
277 Any anyIn;
278 anyIn <<= seqIn[i];
279 Any anyOut = seqOut[i];
280 if (anyIn != anyOut)
281 return false;
284 return true;
287 void printSequence( Sequence<Any>& val)
290 // typelib_TypeDescription* desc;
291 // val.getValueTypeDescription( &desc);
292 // typelib_typedescription_release( desc);
294 USES_CONVERSION;
295 char buff[1024];
296 buff[0]=0;
297 char tmpBuf[1024];
298 tmpBuf[0]=0;
299 sal_Int32 i;
301 for( i=0; i< val.getLength(); i++)
303 Any& elem= val[i];
304 switch ( elem.getValueTypeClass())
306 case TypeClass_BYTE:
307 sprintf( tmpBuf, "sal_Int8 %d \n", *(sal_Int8*)elem.getValue());
308 break;
309 case TypeClass_SHORT:
310 sprintf( tmpBuf, "sal_Int16 %d \n", *(sal_Int16*)elem.getValue());
311 break;
312 case TypeClass_LONG:
313 sprintf( tmpBuf, "sal_Int32 %d \n", *(sal_Int32*)elem.getValue());
314 break;
315 case TypeClass_DOUBLE:
316 sprintf( tmpBuf, "double %f \n", *(double*)elem.getValue());
317 break;
318 case TypeClass_FLOAT:
319 sprintf( tmpBuf, "float %f \n", *(float*)elem.getValue());
320 break;
321 case TypeClass_STRING:
322 sprintf( tmpBuf, "%S \n", (*(OUString*)elem.getValue()).getStr());
323 break;
324 case TypeClass_INTERFACE:
326 // we assume that the interface is XInvocation of an AxTestControls.Basic component.
327 Reference<XInvocation> inv;
328 elem>>= inv;
329 if( inv.is())
331 Any prpVal= inv->getValue( OUString( L"prpString"));
332 sprintf( tmpBuf, "Property prpString: %S \n", (*(OUString*)prpVal.getValue()).getStr());
334 break;
336 default:break;
338 strcat( buff, tmpBuf);
342 MessageBox( NULL, A2T(buff), _T("clientTest: printing Sequence elements"), MB_OK);
345 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */