summaryrefslogtreecommitdiff
path: root/extensions/test/ole/OleClient/funcs.cxx
blob: 67408963765255a04a0613099dc5549708dc3291 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/* -*- 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 <atlbase.h>
#include <stdio.h>
#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 <com/sun/star/bridge/oleautomation/NamedArgument.hpp>
#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;

using ::rtl::OUString;

template< class T >
bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & returned);


Reference< XMultiServiceFactory > objectFactory;//


Reference<XMultiServiceFactory> getMultiServiceFactory()
{
    static Reference< XMultiServiceFactory > factory;
    if( ! objectFactory.is() )
    {
        Reference<XComponentContext> context = defaultBootstrap_InitialComponentContext();
        factory = Reference<XMultiServiceFactory>( context->getServiceManager(), UNO_QUERY);

    }
    return factory;
}

Reference<XInvocation> getComObject( OUString progId)
{
    HRESULT hr= S_OK;
    Reference< XInvocation > ret;
//  Reference<XMultiServiceFactory> fac;
    if(  ! objectFactory.is())
    {   Reference<XMultiServiceFactory> mgr= getMultiServiceFactory();
        Reference<XInterface> xInt= mgr->createInstance(
            OUString(L"com.sun.star.bridge.oleautomation.Factory"));
        objectFactory= Reference<XMultiServiceFactory>::query(  xInt);
    }

    if( objectFactory.is())
    {
        Reference<XInterface> xIntAx= objectFactory->createInstance( progId.getStr());
        if( xIntAx.is() )
        {
            Reference< XInvocation > xInv( xIntAx, UNO_QUERY);
            ret= xInv;
        }
    }
    return ret;
}

Reference<XInvocation> 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>( (sal_Int8*)arId, 16), OLE, UNO );

    Reference<XInvocation> 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<Any> & outArgs,
                  const Sequence<sal_Int16> & indices, const Sequence<Any> & 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() == getVoidCppuType())
            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() == getCppuType((NamedArgument *) 0))
        {
            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<sal_Unicode> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_BOOLEAN:
            {
                Sequence<sal_Bool> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_BYTE:
            {
                Sequence<sal_Int8> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_SHORT:
            {
                Sequence<sal_Int16> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_LONG:
            {
                Sequence<sal_Int32> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_FLOAT:
            {
                Sequence<float> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_DOUBLE:
            {
                Sequence<double> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_STRING:
            {
                Sequence<OUString> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_ANY:
            {
                Sequence<Any> seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        case TypeClass_SEQUENCE:
            {
                //Sequence<sal_Unicode> seq;
                //orig >>= seq;
                //Sequence<Any> seq2;
                //returned >>= seq2;
                //return equalSequences(seq, seq2);
                break;
            }
        case TypeClass_INTERFACE:
            {
                Sequence<Reference<XInvocation> > seq;
                orig >>= seq;
                Sequence<Any> seq2;
                returned >>= seq2;
                return equalSequences(seq, seq2);
            }
        default:
            return false;
    }
    return false;
}

template< class T >
bool equalSequences(const Sequence<T>& seqIn, const Sequence<Any> & 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<Any>& 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<XInvocation> 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: */