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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/* -*- 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 .
 */
// Callback.cpp : Implementation of CCallback
#include "stdafx.h"
#include "XCallback_Impl.h"
#include "Callback.h"


// CCallback


STDMETHODIMP CCallback::func1()
{
    MessageBox( NULL, _T("Callback::func1 called"),_T(""), MB_OK);
    return S_OK;
}

STDMETHODIMP CCallback::returnInterface(IDispatch **ppdisp)
{
    if( ! ppdisp)
        return E_POINTER;
    CComPtr<IDispatch> spDisp;
    spDisp.CoCreateInstance( L"XCallback_Impl.Simple");
    *ppdisp= spDisp;
    (*ppdisp)->AddRef();
    return S_OK;
}

STDMETHODIMP CCallback::outInterface(IDispatch **ppdisp)
{
//  return S_OK;
    if( ! ppdisp)
        return E_POINTER;
    CComPtr<IDispatch> spDisp;
    spDisp.CoCreateInstance( L"XCallback_Impl.Simple");
    *ppdisp= spDisp;
    (*ppdisp)->AddRef();

//  MessageBox( NULL, _T("CCallback::outInterface"), _T(""), MB_OK);
    return S_OK;
}

STDMETHODIMP CCallback::outValuesMixed(long val, long *pval, BSTR string)
{
    USES_CONVERSION;
    char buff[1024];
    *pval = val+1;
    sprintf( buff, "param1: %d, param2 out: %d, param3: %S", val, *pval, string);
    MessageBox( NULL, A2T(buff), A2T("XCallback_Impl.Callback"), MB_OK);
    return S_OK;
}


STDMETHODIMP CCallback::outValuesAll(
            /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppdisp,
            /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppSimpleStruct,
            /* [out] */ long __RPC_FAR *aSimpleEnum,
            /* [out] */ SAFEARRAY __RPC_FAR * __RPC_FAR *outSeq,
            /* [out] */ VARIANT __RPC_FAR *varAny,
            /* [out] */ VARIANT_BOOL __RPC_FAR *aBool,
            /* [out] */ short __RPC_FAR *aChar,
            /* [out] */ BSTR __RPC_FAR *aString,
            /* [out] */ float __RPC_FAR *aFloat,
            /* [out] */ double __RPC_FAR *aDouble,
            /* [out] */  unsigned char __RPC_FAR *aByte,
            /* [out] */ short __RPC_FAR *aShort,
            /* [out] */ long __RPC_FAR *aLong)
//)
{
//  if( ! ppdisp || ! ppSimpleStruct || ! aSimpleEnum ||
//      ! outSeq || !varAny ||! aBool || ! aChar ||
//      ! aString || ! aFloat || ! aDouble || ! aByte ||
//      ! aShort || ! aLong || ! aUShort || ! aULong)
//      return E_POINTER;

    HRESULT hr=S_OK;
    hr= outInterface( ppdisp);<--- hr is assigned
    hr= outStruct( ppSimpleStruct);<--- hr is overwritten<--- hr is assigned
    hr= outEnum( aSimpleEnum);<--- hr is overwritten<--- hr is assigned
    hr= outSeqAny( outSeq);<--- hr is overwritten<--- hr is assigned
    hr= outAny( varAny);<--- hr is overwritten<--- hr is assigned
    hr= outBool( aBool);<--- hr is overwritten<--- hr is assigned
    hr= outChar( aChar);<--- hr is overwritten<--- hr is assigned
    hr= outString( aString);<--- hr is overwritten<--- hr is assigned
    hr= outFloat( aFloat);<--- hr is overwritten<--- hr is assigned
    hr= outDouble( aDouble);<--- hr is overwritten<--- hr is assigned
    hr= outByte( aByte);<--- hr is overwritten<--- hr is assigned
    hr= outShort( aShort);<--- hr is overwritten<--- hr is assigned
    hr= outLong( aLong);<--- hr is overwritten
    return hr;
}

STDMETHODIMP CCallback::outStruct(IDispatch **outStruct)
{
//  return S_OK;
    if( !outStruct)
        return E_POINTER;
    HRESULT hr= E_FAIL;
//  MessageBox( NULL, _T("CCallback::outStruct"), _T(""), MB_OK);

    CComPtr<IDispatch> _dispMgr;
    if( SUCCEEDED(hr= _dispMgr.CoCreateInstance(L"com.sun.star.ServiceManager")))
    {
        CComDispatchDriver manager( _dispMgr);
        CComVariant param1(L"com.sun.star.reflection.CoreReflection");
        CComVariant varRet;
        hr= manager.Invoke1( L"createInstance", &param1, &varRet);<--- hr is assigned

        CComDispatchDriver reflection( varRet.pdispVal);
        param1= L"oletest.SimpleStruct";
        varRet.Clear();
        hr= reflection.Invoke1( L"forName", &param1, &varRet);<--- hr is overwritten<--- hr is assigned

        CComDispatchDriver classSimpleStruct( varRet.pdispVal);

        CComPtr<IDispatch> dispStruct;
        param1.vt= VT_DISPATCH | VT_BYREF;
        param1.ppdispVal= &dispStruct;
        if( SUCCEEDED( hr= classSimpleStruct.Invoke1(L"createObject", &param1)))<--- hr is overwritten
        {
            // Set the value
            CComDispatchDriver simpleStruct( dispStruct);
            param1=L" this is a property string";
            hr= simpleStruct.PutPropertyByName(L"message", &param1);<--- hr is assigned
            *outStruct= dispStruct;
            (*outStruct)->AddRef();
            hr= S_OK;<--- hr is overwritten
        }
    }
    return hr;
}

STDMETHODIMP CCallback::outEnum(long *outEnum)
{
    if( !outEnum)
        return E_POINTER;
    *outEnum= 1;
    return S_OK;
}

STDMETHODIMP CCallback::outSeqAny(LPSAFEARRAY* outSeq)
{
//  _CrtDbgBreak();
    SAFEARRAY* pArr= SafeArrayCreateVector( VT_VARIANT, 0, 3);
    CComVariant var[3];
    var[0]=L" variant 0";
    var[1]=L" variant 1";
    var[2]=L"variant 2";
    for( long i=0; i<3; i++)
    {
        SafeArrayPutElement( pArr, &i, (void*)&var[i]);
    }

    *outSeq= pArr;
    return S_OK;
}

// ATLASSERT //VT_EMPTY


STDMETHODIMP CCallback::outAny(VARIANT *outAny)
{
    if( ! outAny)
        return E_POINTER;
    outAny->vt= VT_BSTR;
    outAny->bstrVal= SysAllocString( L"This is a string in a VARIANT");

    return S_OK;
}

STDMETHODIMP CCallback::outBool(VARIANT_BOOL *outBool)
{
    if( ! outBool)
        return E_POINTER;
    *outBool= VARIANT_TRUE;
    return S_OK;
}

STDMETHODIMP CCallback::outChar(short *outChar)
{
    if( !outChar)
        return E_POINTER;
    *outChar= (short)L'A';
    return S_OK;
}

STDMETHODIMP CCallback::outString(BSTR *outString)
{
    if( !outString)
        return E_POINTER;
    *outString= SysAllocString(L"This is a BSTR");
    return S_OK;
}

STDMETHODIMP CCallback::outFloat(float *outFloat)
{
    if( !outFloat)
        return E_POINTER;
    *outFloat= 3.14f;
    return S_OK;
}

STDMETHODIMP CCallback::outDouble(double *outDouble)
{
    if(!outDouble)
        return E_POINTER;

    *outDouble= 3.145;
    return S_OK;
}



STDMETHODIMP CCallback::outShort(short *outShort)
{
    if(!outShort)
        return E_POINTER;
    *outShort= -1;
    return S_OK;
}

STDMETHODIMP CCallback::outLong(long *outLong)
{
    if(!outLong)
        return E_POINTER;
    *outLong= 0xffffffff;
    return S_OK;
}



STDMETHODIMP CCallback::outByte(unsigned char* outByte)
{
    if(!outByte)
        return E_POINTER;
    *outByte= 0xff;
    return S_OK;
}

STDMETHODIMP CCallback::inoutInterface(IDispatch **ppdisp)
{
    if( !ppdisp)
        return E_POINTER;
    CComDispatchDriver disp( *ppdisp);
    CComVariant param1(L"");
    disp.Invoke1(L"func", &param1);

    (*ppdisp)->Release();

    CComPtr<IDispatch> outDisp;
    outDisp.CoCreateInstance( L"XCallback_Impl.Simple");
    *ppdisp= outDisp;
    (*ppdisp)->AddRef();

    return S_OK;
}

STDMETHODIMP CCallback::inoutStruct(IDispatch **inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    HRESULT hr= S_OK;
    USES_CONVERSION;
    CComVariant var;
    CComDispatchDriver disp( *inoutVal);

    hr= disp.GetPropertyByName(L"message", &var);<--- hr is assigned
    MessageBox( NULL, W2T(var.bstrVal), _T("XCallback_Impl.Callback"), MB_OK);

    (*inoutVal)->Release();

    CComDispatchDriver dispStruct;
    hr= outStruct( &dispStruct.p);<--- hr is overwritten<--- hr is assigned
    var.Clear();
    var= L"This struct was created in XCallback_Imp.Callback";
    hr= dispStruct.PutPropertyByName(L"message", &var);<--- hr is overwritten

    *inoutVal= dispStruct;
    (*inoutVal)->AddRef();
    return hr;
}

STDMETHODIMP CCallback::inoutEnum(long *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    *inoutVal= *inoutVal+1;

    return S_OK;
}

STDMETHODIMP CCallback::inoutSeqAny(LPSAFEARRAY *pArray)
{
    if( !pArray)
        return E_POINTER;
    HRESULT hr= S_OK;
    long lbound=0;
    long ubound=0;
    hr= SafeArrayGetLBound( *pArray, 1, &lbound);<--- hr is assigned
    hr= SafeArrayGetUBound( *pArray, 1, &ubound);<--- hr is overwritten<--- Variable 'hr' is assigned a value that is never used.
    long count= ubound - lbound + 1;

    // the Array is supposet to contain variants
    CComVariant var;
    for( long i=0; i<count; i++)
    {
        var.Clear();
        hr= SafeArrayGetElement( *pArray, &i, (void*)&var);<--- Variable 'hr' is assigned a value that is never used.
    }

    SafeArrayDestroy( *pArray);

    outSeqAny( pArray);
    return S_OK;
}

STDMETHODIMP CCallback::inoutAny(VARIANT *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    USES_CONVERSION;
    if( inoutVal->vt= VT_BSTR)
        MessageBox( NULL, W2T( inoutVal->bstrVal), _T("XCallback_Impl.Callback"), MB_OK);

    VariantClear( inoutVal);
    inoutVal->vt= VT_BSTR;
    inoutVal->bstrVal=SysAllocString( L" [string] XCallback_Impl.Callback inoutAny");
    return S_OK;
}

STDMETHODIMP CCallback::inoutBool(VARIANT_BOOL *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;

    *inoutVal= *inoutVal == VARIANT_TRUE ? VARIANT_FALSE : VARIANT_TRUE;
    return S_OK;
}

STDMETHODIMP CCallback::inoutChar(short *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    USES_CONVERSION;
    char buff[256];
    sprintf( buff, "character value: %C", *inoutVal);
    MessageBox( NULL, A2T(buff), _T("XCallback_Impl.Callback"), MB_OK);
    *inoutVal= L'B';
    return S_OK;
}

STDMETHODIMP CCallback::inoutString(BSTR *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    USES_CONVERSION;
    MessageBox( NULL, W2T(*inoutVal), _T("XCallback_Impl.Callback"), MB_OK);
    SysFreeString(*inoutVal);
    *inoutVal= SysAllocString(L"a string from XCallback_Impl.Callback");

    return S_OK;
}

STDMETHODIMP CCallback::inoutFloat(float *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    *inoutVal = *inoutVal+1;
    return S_OK;
}

STDMETHODIMP CCallback::inoutDouble(double *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    *inoutVal= *inoutVal+1;
    return S_OK;
}

STDMETHODIMP CCallback::inoutByte(unsigned char *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    *inoutVal= 0xff;
    return S_OK;
}

STDMETHODIMP CCallback::inoutShort(short *inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    *inoutVal= -1;
    return S_OK;
}

STDMETHODIMP CCallback::inoutLong(long* inoutVal)
{
    if( !inoutVal)
        return E_POINTER;
    *inoutVal= 0xffffffff;
    return S_OK;
}

STDMETHODIMP CCallback::inoutValuesAll(
            /* [out][in] */ IDispatch __RPC_FAR *__RPC_FAR *aXSimple,
            /* [out][in] */ IDispatch __RPC_FAR *__RPC_FAR *aStruct,
            /* [out][in] */ long __RPC_FAR *aEnum,
            /* [out][in] */ SAFEARRAY __RPC_FAR * __RPC_FAR *aSeq,
            /* [out][in] */ VARIANT __RPC_FAR *aAny,
            /* [out][in] */ VARIANT_BOOL __RPC_FAR *aBool,
            /* [out][in] */ short __RPC_FAR *aChar,
            /* [out][in] */ BSTR __RPC_FAR *aString,
            /* [out][in] */ float __RPC_FAR *aFloat,
            /* [out][in] */ double __RPC_FAR *aDouble,
            /* [out][in] */ unsigned char __RPC_FAR *aByte,
            /* [out][in] */ short __RPC_FAR *aShort,
            /* [out][in] */ long __RPC_FAR *aLong)
{
    inoutInterface( aXSimple);
    inoutStruct( aStruct);
    inoutEnum( aEnum);
    inoutSeqAny( aSeq);
    inoutAny( aAny);
    inoutBool( aBool);
    inoutChar( aChar);
    inoutString( aString);
    inoutFloat( aFloat);
    inoutDouble( aDouble);
    inoutByte( aByte);
    inoutShort( aShort);
    inoutLong( aLong);

    return S_OK;
}


STDMETHODIMP CCallback::inValues(short aChar, long aLong, BSTR aString)
{
    USES_CONVERSION;
    wchar_t _char= (wchar_t) aChar;
    char buff[1024];
    sprintf( buff, "Parameters: char= %C, long= %d, string= %s", _char, aLong, W2A(aString));
    MessageBox( NULL, A2T(buff), _T("XCallback_Impl.Callback"), MB_OK);
    return S_OK;
}

STDMETHODIMP CCallback::outSeqByte(LPSAFEARRAY * outVal)
{
    // TODO: Add your implementation code here

    return S_OK;
}

STDMETHODIMP CCallback::inSeqByte( LPSAFEARRAY listeners)
{

    return S_OK;
}

STDMETHODIMP CCallback::inSeqXEventListener( LPSAFEARRAY listeners, LPSAFEARRAY events)
{
    HRESULT hr= S_OK;
    long ubound= 0;
    long lbound= 0;
    long count= 0;
    hr= SafeArrayGetUBound( listeners, 1, &ubound);<--- hr is assigned
    hr= SafeArrayGetLBound( listeners, 1, &lbound);<--- hr is overwritten<--- Variable 'hr' is assigned a value that is never used.
    count= ubound - lbound +1;

    // We assume that the count of EventObjects in events is the same
    for( long i = 0; i < count; i++)
    {
        CComVariant varListener;
        CComVariant varEvent;
        hr= SafeArrayGetElement( listeners, &i, &varListener);<--- hr is assigned
        hr= SafeArrayGetElement( events, &i, &varEvent);<--- hr is overwritten<--- Variable 'hr' is assigned a value that is never used.
        if( varListener.vt == VT_DISPATCH && varEvent.vt == VT_DISPATCH)
        {
            CComDispatchDriver disp( varListener.pdispVal);
            hr= disp.Invoke1(L"disposing", &varEvent);<--- Variable 'hr' is assigned a value that is never used.
        }

    }

    return S_OK;
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */