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