| 1 |
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
| 2 |
/* |
| 3 |
* This file is part of the LibreOffice project. |
| 4 |
* |
| 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/. |
| 8 |
* |
| 9 |
* This file incorporates work covered by the following license notice: |
| 10 |
* |
| 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 . |
| 18 |
*/ |
| 19 |
// Callback.cpp : Implementation of CCallback |
| 20 |
#include "stdafx.h" |
| 21 |
#include "XCallback_Impl.h" |
| 22 |
#include "Callback.h" |
| 23 |
|
| 24 |
|
| 25 |
// CCallback |
| 26 |
|
| 27 |
|
| 28 |
STDMETHODIMP CCallback::func1() |
| 29 |
{ |
| 30 |
MessageBox( NULL, _T("Callback::func1 called"),_T(""), MB_OK); |
| 31 |
return S_OK; |
| 32 |
} |
| 33 |
|
| 34 |
STDMETHODIMP CCallback::returnInterface(IDispatch **ppdisp) |
| 35 |
{ |
| 36 |
if( ! ppdisp) |
| 37 |
return E_POINTER; |
| 38 |
CComPtr<IDispatch> spDisp; |
| 39 |
spDisp.CoCreateInstance( L"XCallback_Impl.Simple"); |
| 40 |
*ppdisp= spDisp; |
| 41 |
(*ppdisp)->AddRef(); |
| 42 |
return S_OK; |
| 43 |
} |
| 44 |
|
| 45 |
STDMETHODIMP CCallback::outInterface(IDispatch **ppdisp) |
| 46 |
{ |
| 47 |
// return S_OK; |
| 48 |
if( ! ppdisp) |
| 49 |
return E_POINTER; |
| 50 |
CComPtr<IDispatch> spDisp; |
| 51 |
spDisp.CoCreateInstance( L"XCallback_Impl.Simple"); |
| 52 |
*ppdisp= spDisp; |
| 53 |
(*ppdisp)->AddRef(); |
| 54 |
|
| 55 |
// MessageBox( NULL, _T("CCallback::outInterface"), _T(""), MB_OK); |
| 56 |
return S_OK; |
| 57 |
} |
| 58 |
|
| 59 |
STDMETHODIMP CCallback::outValuesMixed(long val, long *pval, BSTR string) |
| 60 |
{ |
| 61 |
USES_CONVERSION; |
| 62 |
char buff[1024]; |
| 63 |
*pval = val+1; |
| 64 |
sprintf( buff, "param1: %d, param2 out: %d, param3: %S", val, *pval, string); |
| 65 |
MessageBox( NULL, A2T(buff), A2T("XCallback_Impl.Callback"), MB_OK); |
| 66 |
return S_OK; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
STDMETHODIMP CCallback::outValuesAll( |
| 71 |
/* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppdisp, |
| 72 |
/* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppSimpleStruct, |
| 73 |
/* [out] */ long __RPC_FAR *aSimpleEnum, |
| 74 |
/* [out] */ SAFEARRAY __RPC_FAR * __RPC_FAR *outSeq, |
| 75 |
/* [out] */ VARIANT __RPC_FAR *varAny, |
| 76 |
/* [out] */ VARIANT_BOOL __RPC_FAR *aBool, |
| 77 |
/* [out] */ short __RPC_FAR *aChar, |
| 78 |
/* [out] */ BSTR __RPC_FAR *aString, |
| 79 |
/* [out] */ float __RPC_FAR *aFloat, |
| 80 |
/* [out] */ double __RPC_FAR *aDouble, |
| 81 |
/* [out] */ unsigned char __RPC_FAR *aByte, |
| 82 |
/* [out] */ short __RPC_FAR *aShort, |
| 83 |
/* [out] */ long __RPC_FAR *aLong) |
| 84 |
//) |
| 85 |
{ |
| 86 |
// if( ! ppdisp || ! ppSimpleStruct || ! aSimpleEnum || |
| 87 |
// ! outSeq || !varAny ||! aBool || ! aChar || |
| 88 |
// ! aString || ! aFloat || ! aDouble || ! aByte || |
| 89 |
// ! aShort || ! aLong || ! aUShort || ! aULong) |
| 90 |
// return E_POINTER; |
| 91 |
|
| 92 |
HRESULT hr=S_OK; |
| 93 |
hr= outInterface( ppdisp); |
| 94 |
hr= outStruct( ppSimpleStruct); |
| 95 |
hr= outEnum( aSimpleEnum); |
| 96 |
hr= outSeqAny( outSeq); |
| 97 |
hr= outAny( varAny); |
| 98 |
hr= outBool( aBool); |
| 99 |
hr= outChar( aChar); |
| 100 |
hr= outString( aString); |
| 101 |
hr= outFloat( aFloat); |
| 102 |
hr= outDouble( aDouble); |
| 103 |
hr= outByte( aByte); |
| 104 |
hr= outShort( aShort); |
| 105 |
hr= outLong( aLong); |
| 106 |
return hr; |
| 107 |
} |
| 108 |
|
| 109 |
STDMETHODIMP CCallback::outStruct(IDispatch **outStruct) |
| 110 |
{ |
| 111 |
// return S_OK; |
| 112 |
if( !outStruct) |
| 113 |
return E_POINTER; |
| 114 |
HRESULT hr= E_FAIL; |
| 115 |
// MessageBox( NULL, _T("CCallback::outStruct"), _T(""), MB_OK); |
| 116 |
|
| 117 |
CComPtr<IDispatch> _dispMgr; |
| 118 |
if( SUCCEEDED(hr= _dispMgr.CoCreateInstance(L"com.sun.star.ServiceManager"))) |
| 119 |
{ |
| 120 |
CComDispatchDriver manager( _dispMgr); |
| 121 |
CComVariant param1(L"com.sun.star.reflection.CoreReflection"); |
| 122 |
CComVariant varRet; |
| 123 |
hr= manager.Invoke1( L"createInstance", ¶m1, &varRet); |
| 124 |
|
| 125 |
CComDispatchDriver reflection( varRet.pdispVal); |
| 126 |
param1= L"oletest.SimpleStruct"; |
| 127 |
varRet.Clear(); |
| 128 |
hr= reflection.Invoke1( L"forName", ¶m1, &varRet); |
| 129 |
|
| 130 |
CComDispatchDriver classSimpleStruct( varRet.pdispVal); |
| 131 |
|
| 132 |
CComPtr<IDispatch> dispStruct; |
| 133 |
param1.vt= VT_DISPATCH | VT_BYREF; |
| 134 |
param1.ppdispVal= &dispStruct; |
| 135 |
if( SUCCEEDED( hr= classSimpleStruct.Invoke1(L"createObject", ¶m1))) |
| 136 |
{ |
| 137 |
// Set the value |
| 138 |
CComDispatchDriver simpleStruct( dispStruct); |
| 139 |
param1=L" this is a property string"; |
| 140 |
hr= simpleStruct.PutPropertyByName(L"message", ¶m1); |
| 141 |
*outStruct= dispStruct; |
| 142 |
(*outStruct)->AddRef(); |
| 143 |
hr= S_OK; |
| 144 |
} |
| 145 |
} |
| 146 |
return hr; |
| 147 |
} |
| 148 |
|
| 149 |
STDMETHODIMP CCallback::outEnum(long *outEnum) |
| 150 |
{ |
| 151 |
if( !outEnum) |
| 152 |
return E_POINTER; |
| 153 |
*outEnum= 1; |
| 154 |
return S_OK; |
| 155 |
} |
| 156 |
|
| 157 |
STDMETHODIMP CCallback::outSeqAny(LPSAFEARRAY* outSeq) |
| 158 |
{ |
| 159 |
// _CrtDbgBreak(); |
| 160 |
SAFEARRAY* pArr= SafeArrayCreateVector( VT_VARIANT, 0, 3); |
| 161 |
CComVariant var[3]; |
| 162 |
var[0]=L" variant 0"; |
| 163 |
var[1]=L" variant 1"; |
| 164 |
var[2]=L"variant 2"; |
| 165 |
for( long i=0; i<3; i++) |
| 166 |
{ |
| 167 |
SafeArrayPutElement( pArr, &i, (void*)&var[i]); |
| 168 |
} |
| 169 |
|
| 170 |
*outSeq= pArr; |
| 171 |
return S_OK; |
| 172 |
} |
| 173 |
|
| 174 |
// ATLASSERT //VT_EMPTY |
| 175 |
|
| 176 |
|
| 177 |
STDMETHODIMP CCallback::outAny(VARIANT *outAny) |
| 178 |
{ |
| 179 |
if( ! outAny) |
| 180 |
return E_POINTER; |
| 181 |
outAny->vt= VT_BSTR; |
| 182 |
outAny->bstrVal= SysAllocString( L"This is a string in a VARIANT"); |
| 183 |
|
| 184 |
return S_OK; |
| 185 |
} |
| 186 |
|
| 187 |
STDMETHODIMP CCallback::outBool(VARIANT_BOOL *outBool) |
| 188 |
{ |
| 189 |
if( ! outBool) |
| 190 |
return E_POINTER; |
| 191 |
*outBool= VARIANT_TRUE; |
| 192 |
return S_OK; |
| 193 |
} |
| 194 |
|
| 195 |
STDMETHODIMP CCallback::outChar(short *outChar) |
| 196 |
{ |
| 197 |
if( !outChar) |
| 198 |
return E_POINTER; |
| 199 |
*outChar= (short)L'A'; |
| 200 |
return S_OK; |
| 201 |
} |
| 202 |
|
| 203 |
STDMETHODIMP CCallback::outString(BSTR *outString) |
| 204 |
{ |
| 205 |
if( !outString) |
| 206 |
return E_POINTER; |
| 207 |
*outString= SysAllocString(L"This is a BSTR"); |
| 208 |
return S_OK; |
| 209 |
} |
| 210 |
|
| 211 |
STDMETHODIMP CCallback::outFloat(float *outFloat) |
| 212 |
{ |
| 213 |
if( !outFloat) |
| 214 |
return E_POINTER; |
| 215 |
*outFloat= 3.14f; |
| 216 |
return S_OK; |
| 217 |
} |
| 218 |
|
| 219 |
STDMETHODIMP CCallback::outDouble(double *outDouble) |
| 220 |
{ |
| 221 |
if(!outDouble) |
| 222 |
return E_POINTER; |
| 223 |
|
| 224 |
*outDouble= 3.145; |
| 225 |
return S_OK; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
STDMETHODIMP CCallback::outShort(short *outShort) |
| 231 |
{ |
| 232 |
if(!outShort) |
| 233 |
return E_POINTER; |
| 234 |
*outShort= -1; |
| 235 |
return S_OK; |
| 236 |
} |
| 237 |
|
| 238 |
STDMETHODIMP CCallback::outLong(long *outLong) |
| 239 |
{ |
| 240 |
if(!outLong) |
| 241 |
return E_POINTER; |
| 242 |
*outLong= 0xffffffff; |
| 243 |
return S_OK; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
STDMETHODIMP CCallback::outByte(unsigned char* outByte) |
| 249 |
{ |
| 250 |
if(!outByte) |
| 251 |
return E_POINTER; |
| 252 |
*outByte= 0xff; |
| 253 |
return S_OK; |
| 254 |
} |
| 255 |
|
| 256 |
STDMETHODIMP CCallback::inoutInterface(IDispatch **ppdisp) |
| 257 |
{ |
| 258 |
if( !ppdisp) |
| 259 |
return E_POINTER; |
| 260 |
CComDispatchDriver disp( *ppdisp); |
| 261 |
CComVariant param1(L""); |
| 262 |
disp.Invoke1(L"func", ¶m1); |
| 263 |
|
| 264 |
(*ppdisp)->Release(); |
| 265 |
|
| 266 |
CComPtr<IDispatch> outDisp; |
| 267 |
outDisp.CoCreateInstance( L"XCallback_Impl.Simple"); |
| 268 |
*ppdisp= outDisp; |
| 269 |
(*ppdisp)->AddRef(); |
| 270 |
|
| 271 |
return S_OK; |
| 272 |
} |
| 273 |
|
| 274 |
STDMETHODIMP CCallback::inoutStruct(IDispatch **inoutVal) |
| 275 |
{ |
| 276 |
if( !inoutVal) |
| 277 |
return E_POINTER; |
| 278 |
HRESULT hr= S_OK; |
| 279 |
USES_CONVERSION; |
| 280 |
CComVariant var; |
| 281 |
CComDispatchDriver disp( *inoutVal); |
| 282 |
|
| 283 |
hr= disp.GetPropertyByName(L"message", &var); |
| 284 |
MessageBox( NULL, W2T(var.bstrVal), _T("XCallback_Impl.Callback"), MB_OK); |
| 285 |
|
| 286 |
(*inoutVal)->Release(); |
| 287 |
|
| 288 |
CComDispatchDriver dispStruct; |
| 289 |
hr= outStruct( &dispStruct.p); |
| 290 |
var.Clear(); |
| 291 |
var= L"This struct was created in XCallback_Imp.Callback"; |
| 292 |
hr= dispStruct.PutPropertyByName(L"message", &var); |
| 293 |
|
| 294 |
*inoutVal= dispStruct; |
| 295 |
(*inoutVal)->AddRef(); |
| 296 |
return hr; |
| 297 |
} |
| 298 |
|
| 299 |
STDMETHODIMP CCallback::inoutEnum(long *inoutVal) |
| 300 |
{ |
| 301 |
if( !inoutVal) |
| 302 |
return E_POINTER; |
| 303 |
*inoutVal= *inoutVal+1; |
| 304 |
|
| 305 |
return S_OK; |
| 306 |
} |
| 307 |
|
| 308 |
STDMETHODIMP CCallback::inoutSeqAny(LPSAFEARRAY *pArray) |
| 309 |
{ |
| 310 |
if( !pArray) |
| 311 |
return E_POINTER; |
| 312 |
HRESULT hr= S_OK; |
| 313 |
long lbound=0; |
| 314 |
long ubound=0; |
| 315 |
hr= SafeArrayGetLBound( *pArray, 1, &lbound); |
| 316 |
hr= SafeArrayGetUBound( *pArray, 1, &ubound); |
| 317 |
long count= ubound - lbound + 1; |
| 318 |
|
| 319 |
// the Array is supposed to contain variants |
| 320 |
CComVariant var; |
| 321 |
for( long i=0; i<count; i++) |
| 322 |
{ |
| 323 |
var.Clear(); |
| 324 |
hr= SafeArrayGetElement( *pArray, &i, (void*)&var); |
| 325 |
} |
| 326 |
|
| 327 |
SafeArrayDestroy( *pArray); |
| 328 |
|
| 329 |
outSeqAny( pArray); |
| 330 |
return S_OK; |
| 331 |
} |
| 332 |
|
| 333 |
STDMETHODIMP CCallback::inoutAny(VARIANT *inoutVal) |
| 334 |
{ |
| 335 |
if( !inoutVal) |
| 336 |
return E_POINTER; |
| 337 |
USES_CONVERSION; |
| 338 |
if( inoutVal->vt= VT_BSTR) |
| 339 |
MessageBox( NULL, W2T( inoutVal->bstrVal), _T("XCallback_Impl.Callback"), MB_OK); |
| 340 |
|
| 341 |
VariantClear( inoutVal); |
| 342 |
inoutVal->vt= VT_BSTR; |
| 343 |
inoutVal->bstrVal=SysAllocString( L" [string] XCallback_Impl.Callback inoutAny"); |
| 344 |
return S_OK; |
| 345 |
} |
| 346 |
|
| 347 |
STDMETHODIMP CCallback::inoutBool(VARIANT_BOOL *inoutVal) |
| 348 |
{ |
| 349 |
if( !inoutVal) |
| 350 |
return E_POINTER; |
| 351 |
|
| 352 |
*inoutVal= *inoutVal == VARIANT_TRUE ? VARIANT_FALSE : VARIANT_TRUE; |
| 353 |
return S_OK; |
| 354 |
} |
| 355 |
|
| 356 |
STDMETHODIMP CCallback::inoutChar(short *inoutVal) |
| 357 |
{ |
| 358 |
if( !inoutVal) |
| 359 |
return E_POINTER; |
| 360 |
USES_CONVERSION; |
| 361 |
char buff[256]; |
| 362 |
sprintf( buff, "character value: %C", *inoutVal); |
| 363 |
MessageBox( NULL, A2T(buff), _T("XCallback_Impl.Callback"), MB_OK); |
| 364 |
*inoutVal= L'B'; |
| 365 |
return S_OK; |
| 366 |
} |
| 367 |
|
| 368 |
STDMETHODIMP CCallback::inoutString(BSTR *inoutVal) |
| 369 |
{ |
| 370 |
if( !inoutVal) |
| 371 |
return E_POINTER; |
| 372 |
USES_CONVERSION; |
| 373 |
MessageBox( NULL, W2T(*inoutVal), _T("XCallback_Impl.Callback"), MB_OK); |
| 374 |
SysFreeString(*inoutVal); |
| 375 |
*inoutVal= SysAllocString(L"a string from XCallback_Impl.Callback"); |
| 376 |
|
| 377 |
return S_OK; |
| 378 |
} |
| 379 |
|
| 380 |
STDMETHODIMP CCallback::inoutFloat(float *inoutVal) |
| 381 |
{ |
| 382 |
if( !inoutVal) |
| 383 |
return E_POINTER; |
| 384 |
*inoutVal = *inoutVal+1; |
| 385 |
return S_OK; |
| 386 |
} |
| 387 |
|
| 388 |
STDMETHODIMP CCallback::inoutDouble(double *inoutVal) |
| 389 |
{ |
| 390 |
if( !inoutVal) |
| 391 |
return E_POINTER; |
| 392 |
*inoutVal= *inoutVal+1; |
| 393 |
return S_OK; |
| 394 |
} |
| 395 |
|
| 396 |
STDMETHODIMP CCallback::inoutByte(unsigned char *inoutVal) |
| 397 |
{ |
| 398 |
if( !inoutVal) |
| 399 |
return E_POINTER; |
| 400 |
*inoutVal= 0xff; |
| 401 |
return S_OK; |
| 402 |
} |
| 403 |
|
| 404 |
STDMETHODIMP CCallback::inoutShort(short *inoutVal) |
| 405 |
{ |
| 406 |
if( !inoutVal) |
| 407 |
return E_POINTER; |
| 408 |
*inoutVal= -1; |
| 409 |
return S_OK; |
| 410 |
} |
| 411 |
|
| 412 |
STDMETHODIMP CCallback::inoutLong(long* inoutVal) |
| 413 |
{ |
| 414 |
if( !inoutVal) |
| 415 |
return E_POINTER; |
| 416 |
*inoutVal= 0xffffffff; |
| 417 |
return S_OK; |
| 418 |
} |
| 419 |
|
| 420 |
STDMETHODIMP CCallback::inoutValuesAll( |
| 421 |
/* [out][in] */ IDispatch __RPC_FAR *__RPC_FAR *aXSimple, |
| 422 |
/* [out][in] */ IDispatch __RPC_FAR *__RPC_FAR *aStruct, |
| 423 |
/* [out][in] */ long __RPC_FAR *aEnum, |
| 424 |
/* [out][in] */ SAFEARRAY __RPC_FAR * __RPC_FAR *aSeq, |
| 425 |
/* [out][in] */ VARIANT __RPC_FAR *aAny, |
| 426 |
/* [out][in] */ VARIANT_BOOL __RPC_FAR *aBool, |
| 427 |
/* [out][in] */ short __RPC_FAR *aChar, |
| 428 |
/* [out][in] */ BSTR __RPC_FAR *aString, |
| 429 |
/* [out][in] */ float __RPC_FAR *aFloat, |
| 430 |
/* [out][in] */ double __RPC_FAR *aDouble, |
| 431 |
/* [out][in] */ unsigned char __RPC_FAR *aByte, |
| 432 |
/* [out][in] */ short __RPC_FAR *aShort, |
| 433 |
/* [out][in] */ long __RPC_FAR *aLong) |
| 434 |
{ |
| 435 |
inoutInterface( aXSimple); |
| 436 |
inoutStruct( aStruct); |
| 437 |
inoutEnum( aEnum); |
| 438 |
inoutSeqAny( aSeq); |
| 439 |
inoutAny( aAny); |
| 440 |
inoutBool( aBool); |
| 441 |
inoutChar( aChar); |
| 442 |
inoutString( aString); |
| 443 |
inoutFloat( aFloat); |
| 444 |
inoutDouble( aDouble); |
| 445 |
inoutByte( aByte); |
| 446 |
inoutShort( aShort); |
| 447 |
inoutLong( aLong); |
| 448 |
|
| 449 |
return S_OK; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
STDMETHODIMP CCallback::inValues(short aChar, long aLong, BSTR aString) |
| 454 |
{ |
| 455 |
USES_CONVERSION; |
| 456 |
wchar_t _char= (wchar_t) aChar; |
| 457 |
char buff[1024]; |
| 458 |
sprintf( buff, "Parameters: char= %C, long= %d, string= %s", _char, aLong, W2A(aString)); |
| 459 |
MessageBox( NULL, A2T(buff), _T("XCallback_Impl.Callback"), MB_OK); |
| 460 |
return S_OK; |
| 461 |
} |
| 462 |
|
| 463 |
STDMETHODIMP CCallback::outSeqByte(LPSAFEARRAY * outVal) |
| 464 |
{ |
| 465 |
// TODO: Add your implementation code here |
| 466 |
|
| 467 |
return S_OK; |
| 468 |
} |
| 469 |
|
| 470 |
STDMETHODIMP CCallback::inSeqByte( LPSAFEARRAY listeners) |
| 471 |
{ |
| 472 |
|
| 473 |
return S_OK; |
| 474 |
} |
| 475 |
|
| 476 |
STDMETHODIMP CCallback::inSeqXEventListener( LPSAFEARRAY listeners, LPSAFEARRAY events) |
| 477 |
{ |
| 478 |
HRESULT hr= S_OK; |
| 479 |
long ubound= 0; |
| 480 |
long lbound= 0; |
| 481 |
long count= 0; |
| 482 |
hr= SafeArrayGetUBound( listeners, 1, &ubound); |
| 483 |
hr= SafeArrayGetLBound( listeners, 1, &lbound); |
| 484 |
count= ubound - lbound +1; |
| 485 |
|
| 486 |
// We assume that the count of EventObjects in events is the same |
| 487 |
for( long i = 0; i < count; i++) |
| 488 |
{ |
| 489 |
CComVariant varListener; |
| 490 |
CComVariant varEvent; |
| 491 |
hr= SafeArrayGetElement( listeners, &i, &varListener); |
| 492 |
hr= SafeArrayGetElement( events, &i, &varEvent); |
| 493 |
if( varListener.vt == VT_DISPATCH && varEvent.vt == VT_DISPATCH) |
| 494 |
{ |
| 495 |
CComDispatchDriver disp( varListener.pdispVal); |
| 496 |
hr= disp.Invoke1(L"disposing", &varEvent); |
| 497 |
} |
| 498 |
|
| 499 |
} |
| 500 |
|
| 501 |
return S_OK; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |