merged tag ooo/OOO330_m14
[LibreOffice.git] / extensions / test / ole / unoTocomCalls / XCallback_Impl / Callback.cpp
blob90ae64e3f330fb35ca3d7478c13306a9a009417f
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
27 // Callback.cpp : Implementation of CCallback
28 #include "stdafx.h"
29 #include "XCallback_Impl.h"
30 #include "Callback.h"
32 /////////////////////////////////////////////////////////////////////////////
33 // CCallback
36 STDMETHODIMP CCallback::func1()
38 MessageBox( NULL, _T("Callback::func1 called"),_T(""), MB_OK);
39 return S_OK;
42 STDMETHODIMP CCallback::returnInterface(IDispatch **ppdisp)
44 if( ! ppdisp)
45 return E_POINTER;
46 CComPtr<IDispatch> spDisp;
47 spDisp.CoCreateInstance( L"XCallback_Impl.Simple");
48 *ppdisp= spDisp;
49 (*ppdisp)->AddRef();
50 return S_OK;
53 STDMETHODIMP CCallback::outInterface(IDispatch **ppdisp)
55 // return S_OK;
56 if( ! ppdisp)
57 return E_POINTER;
58 CComPtr<IDispatch> spDisp;
59 spDisp.CoCreateInstance( L"XCallback_Impl.Simple");
60 *ppdisp= spDisp;
61 (*ppdisp)->AddRef();
63 // MessageBox( NULL, _T("CCallback::outInterface"), _T(""), MB_OK);
64 return S_OK;
67 STDMETHODIMP CCallback::outValuesMixed(long val, long *pval, BSTR string)
69 USES_CONVERSION;
70 char buff[1024];
71 *pval = val+1;
72 sprintf( buff, "param1: %d, param2 out: %d, param3: %S", val, *pval, string);
73 MessageBox( NULL, A2T(buff), A2T("XCallback_Impl.Callback"), MB_OK);
74 return S_OK;
78 STDMETHODIMP CCallback::outValuesAll(
79 /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppdisp,
80 /* [out] */ IDispatch __RPC_FAR *__RPC_FAR *ppSimpleStruct,
81 /* [out] */ long __RPC_FAR *aSimpleEnum,
82 /* [out] */ SAFEARRAY __RPC_FAR * __RPC_FAR *outSeq,
83 /* [out] */ VARIANT __RPC_FAR *varAny,
84 /* [out] */ VARIANT_BOOL __RPC_FAR *aBool,
85 /* [out] */ short __RPC_FAR *aChar,
86 /* [out] */ BSTR __RPC_FAR *aString,
87 /* [out] */ float __RPC_FAR *aFloat,
88 /* [out] */ double __RPC_FAR *aDouble,
89 /* [out] */ unsigned char __RPC_FAR *aByte,
90 /* [out] */ short __RPC_FAR *aShort,
91 /* [out] */ long __RPC_FAR *aLong)
92 //)
94 // if( ! ppdisp || ! ppSimpleStruct || ! aSimpleEnum ||
95 // ! outSeq || !varAny ||! aBool || ! aChar ||
96 // ! aString || ! aFloat || ! aDouble || ! aByte ||
97 // ! aShort || ! aLong || ! aUShort || ! aULong)
98 // return E_POINTER;
100 HRESULT hr=S_OK;
101 hr= outInterface( ppdisp);
102 hr= outStruct( ppSimpleStruct);
103 hr= outEnum( aSimpleEnum);
104 hr= outSeqAny( outSeq);
105 hr= outAny( varAny);
106 hr= outBool( aBool);
107 hr= outChar( aChar);
108 hr= outString( aString);
109 hr= outFloat( aFloat);
110 hr= outDouble( aDouble);
111 hr= outByte( aByte);
112 hr= outShort( aShort);
113 hr= outLong( aLong);
114 return hr;
117 STDMETHODIMP CCallback::outStruct(IDispatch **outStruct)
119 // return S_OK;
120 if( !outStruct)
121 return E_POINTER;
122 HRESULT hr= E_FAIL;
123 // MessageBox( NULL, _T("CCallback::outStruct"), _T(""), MB_OK);
125 CComPtr<IDispatch> _dispMgr;
126 if( SUCCEEDED(hr= _dispMgr.CoCreateInstance(L"com.sun.star.ServiceManager")))
128 CComDispatchDriver manager( _dispMgr);
129 CComVariant param1(L"com.sun.star.reflection.CoreReflection");
130 CComVariant varRet;
131 hr= manager.Invoke1( L"createInstance", &param1, &varRet);
133 CComDispatchDriver reflection( varRet.pdispVal);
134 param1= L"oletest.SimpleStruct";
135 varRet.Clear();
136 hr= reflection.Invoke1( L"forName", &param1, &varRet);
138 CComDispatchDriver classSimpleStruct( varRet.pdispVal);
140 CComPtr<IDispatch> dispStruct;
141 param1.vt= VT_DISPATCH | VT_BYREF;
142 param1.ppdispVal= &dispStruct;
143 if( SUCCEEDED( hr= classSimpleStruct.Invoke1(L"createObject", &param1)))
145 // Set the value
146 CComDispatchDriver simpleStruct( dispStruct);
147 param1=L" this is a property string";
148 hr= simpleStruct.PutPropertyByName(L"message", &param1);
149 *outStruct= dispStruct;
150 (*outStruct)->AddRef();
151 hr= S_OK;
154 return hr;
157 STDMETHODIMP CCallback::outEnum(long *outEnum)
159 if( !outEnum)
160 return E_POINTER;
161 *outEnum= 1;
162 return S_OK;
165 STDMETHODIMP CCallback::outSeqAny(LPSAFEARRAY* outSeq)
167 // _CrtDbgBreak();
168 SAFEARRAY* pArr= SafeArrayCreateVector( VT_VARIANT, 0, 3);
169 CComVariant var[3];
170 var[0]=L" variant 0";
171 var[1]=L" variant 1";
172 var[2]=L"variant 2";
173 for( long i=0; i<3; i++)
175 SafeArrayPutElement( pArr, &i, (void*)&var[i]);
178 *outSeq= pArr;
179 return S_OK;
182 // ATLASSERT //VT_EMPTY
185 STDMETHODIMP CCallback::outAny(VARIANT *outAny)
187 if( ! outAny)
188 return E_POINTER;
189 outAny->vt= VT_BSTR;
190 outAny->bstrVal= SysAllocString( L"This is a string in a VARIANT");
192 return S_OK;
195 STDMETHODIMP CCallback::outBool(VARIANT_BOOL *outBool)
197 if( ! outBool)
198 return E_POINTER;
199 *outBool= VARIANT_TRUE;
200 return S_OK;
203 STDMETHODIMP CCallback::outChar(short *outChar)
205 if( !outChar)
206 return E_POINTER;
207 *outChar= (short)L'A';
208 return S_OK;
211 STDMETHODIMP CCallback::outString(BSTR *outString)
213 if( !outString)
214 return E_POINTER;
215 *outString= SysAllocString(L"This is a BSTR");
216 return S_OK;
219 STDMETHODIMP CCallback::outFloat(float *outFloat)
221 if( !outFloat)
222 return E_POINTER;
223 *outFloat= 3.14f;
224 return S_OK;
227 STDMETHODIMP CCallback::outDouble(double *outDouble)
229 if(!outDouble)
230 return E_POINTER;
232 *outDouble= 3.145;
233 return S_OK;
238 STDMETHODIMP CCallback::outShort(short *outShort)
240 if(!outShort)
241 return E_POINTER;
242 *outShort= -1;
243 return S_OK;
246 STDMETHODIMP CCallback::outLong(long *outLong)
248 if(!outLong)
249 return E_POINTER;
250 *outLong= 0xffffffff;
251 return S_OK;
256 STDMETHODIMP CCallback::outByte(unsigned char* outByte)
258 if(!outByte)
259 return E_POINTER;
260 *outByte= 0xff;
261 return S_OK;
264 STDMETHODIMP CCallback::inoutInterface(IDispatch **ppdisp)
266 if( !ppdisp)
267 return E_POINTER;
268 CComDispatchDriver disp( *ppdisp);
269 CComVariant param1(L"");
270 disp.Invoke1(L"func", &param1);
272 (*ppdisp)->Release();
274 CComPtr<IDispatch> outDisp;
275 outDisp.CoCreateInstance( L"XCallback_Impl.Simple");
276 *ppdisp= outDisp;
277 (*ppdisp)->AddRef();
279 return S_OK;
282 STDMETHODIMP CCallback::inoutStruct(IDispatch **inoutVal)
284 if( !inoutVal)
285 return E_POINTER;
286 HRESULT hr= S_OK;
287 USES_CONVERSION;
288 CComVariant var;
289 CComDispatchDriver disp( *inoutVal);
291 hr= disp.GetPropertyByName(L"message", &var);
292 MessageBox( NULL, W2T(var.bstrVal), _T("XCallback_Impl.Callback"), MB_OK);
294 (*inoutVal)->Release();
296 CComDispatchDriver dispStruct;
297 hr= outStruct( &dispStruct.p);
298 var.Clear();
299 var= L"This struct was created in XCallback_Imp.Callback";
300 hr= dispStruct.PutPropertyByName(L"message", &var);
302 *inoutVal= dispStruct;
303 (*inoutVal)->AddRef();
304 return hr;
307 STDMETHODIMP CCallback::inoutEnum(long *inoutVal)
309 if( !inoutVal)
310 return E_POINTER;
311 *inoutVal= *inoutVal+1;
313 return S_OK;
316 STDMETHODIMP CCallback::inoutSeqAny(LPSAFEARRAY *pArray)
318 if( !pArray)
319 return E_POINTER;
320 HRESULT hr= S_OK;
321 long lbound=0;
322 long ubound=0;
323 hr= SafeArrayGetLBound( *pArray, 1, &lbound);
324 hr= SafeArrayGetUBound( *pArray, 1, &ubound);
325 long count= ubound - lbound + 1;
327 // the Array is supposet to contain variants
328 CComVariant var;
329 for( long i=0; i<count; i++)
331 var.Clear();
332 hr= SafeArrayGetElement( *pArray, &i, (void*)&var);
335 SafeArrayDestroy( *pArray);
337 outSeqAny( pArray);
338 return S_OK;
341 STDMETHODIMP CCallback::inoutAny(VARIANT *inoutVal)
343 if( !inoutVal)
344 return E_POINTER;
345 USES_CONVERSION;
346 if( inoutVal->vt= VT_BSTR)
347 MessageBox( NULL, W2T( inoutVal->bstrVal), _T("XCallback_Impl.Callback"), MB_OK);
349 VariantClear( inoutVal);
350 inoutVal->vt= VT_BSTR;
351 inoutVal->bstrVal=SysAllocString( L" [string] XCallback_Impl.Callback inoutAny");
352 return S_OK;
355 STDMETHODIMP CCallback::inoutBool(VARIANT_BOOL *inoutVal)
357 if( !inoutVal)
358 return E_POINTER;
360 *inoutVal= *inoutVal == VARIANT_TRUE ? VARIANT_FALSE : VARIANT_TRUE;
361 return S_OK;
364 STDMETHODIMP CCallback::inoutChar(short *inoutVal)
366 if( !inoutVal)
367 return E_POINTER;
368 USES_CONVERSION;
369 char buff[256];
370 sprintf( buff, "character value: %C", *inoutVal);
371 MessageBox( NULL, A2T(buff), _T("XCallback_Impl.Callback"), MB_OK);
372 *inoutVal= L'B';
373 return S_OK;
376 STDMETHODIMP CCallback::inoutString(BSTR *inoutVal)
378 if( !inoutVal)
379 return E_POINTER;
380 USES_CONVERSION;
381 MessageBox( NULL, W2T(*inoutVal), _T("XCallback_Impl.Callback"), MB_OK);
382 SysFreeString(*inoutVal);
383 *inoutVal= SysAllocString(L"a string from XCallback_Impl.Callback");
385 return S_OK;
388 STDMETHODIMP CCallback::inoutFloat(float *inoutVal)
390 if( !inoutVal)
391 return E_POINTER;
392 *inoutVal = *inoutVal+1;
393 return S_OK;
396 STDMETHODIMP CCallback::inoutDouble(double *inoutVal)
398 if( !inoutVal)
399 return E_POINTER;
400 *inoutVal= *inoutVal+1;
401 return S_OK;
404 STDMETHODIMP CCallback::inoutByte(unsigned char *inoutVal)
406 if( !inoutVal)
407 return E_POINTER;
408 *inoutVal= 0xff;
409 return S_OK;
412 STDMETHODIMP CCallback::inoutShort(short *inoutVal)
414 if( !inoutVal)
415 return E_POINTER;
416 *inoutVal= -1;
417 return S_OK;
420 STDMETHODIMP CCallback::inoutLong(long* inoutVal)
422 if( !inoutVal)
423 return E_POINTER;
424 *inoutVal= 0xffffffff;
425 return S_OK;
428 STDMETHODIMP CCallback::inoutValuesAll(
429 /* [out][in] */ IDispatch __RPC_FAR *__RPC_FAR *aXSimple,
430 /* [out][in] */ IDispatch __RPC_FAR *__RPC_FAR *aStruct,
431 /* [out][in] */ long __RPC_FAR *aEnum,
432 /* [out][in] */ SAFEARRAY __RPC_FAR * __RPC_FAR *aSeq,
433 /* [out][in] */ VARIANT __RPC_FAR *aAny,
434 /* [out][in] */ VARIANT_BOOL __RPC_FAR *aBool,
435 /* [out][in] */ short __RPC_FAR *aChar,
436 /* [out][in] */ BSTR __RPC_FAR *aString,
437 /* [out][in] */ float __RPC_FAR *aFloat,
438 /* [out][in] */ double __RPC_FAR *aDouble,
439 /* [out][in] */ unsigned char __RPC_FAR *aByte,
440 /* [out][in] */ short __RPC_FAR *aShort,
441 /* [out][in] */ long __RPC_FAR *aLong)
443 inoutInterface( aXSimple);
444 inoutStruct( aStruct);
445 inoutEnum( aEnum);
446 inoutSeqAny( aSeq);
447 inoutAny( aAny);
448 inoutBool( aBool);
449 inoutChar( aChar);
450 inoutString( aString);
451 inoutFloat( aFloat);
452 inoutDouble( aDouble);
453 inoutByte( aByte);
454 inoutShort( aShort);
455 inoutLong( aLong);
457 return S_OK;
461 STDMETHODIMP CCallback::inValues(short aChar, long aLong, BSTR aString)
463 USES_CONVERSION;
464 wchar_t _char= (wchar_t) aChar;
465 char buff[1024];
466 sprintf( buff, "Parameters: char= %C, long= %d, string= %s", _char, aLong, W2A(aString));
467 MessageBox( NULL, A2T(buff), _T("XCallback_Impl.Callback"), MB_OK);
468 return S_OK;
471 STDMETHODIMP CCallback::outSeqByte(LPSAFEARRAY * outVal)
473 // TODO: Add your implementation code here
475 return S_OK;
478 STDMETHODIMP CCallback::inSeqByte( LPSAFEARRAY listeners)
481 return S_OK;
484 STDMETHODIMP CCallback::inSeqXEventListener( LPSAFEARRAY listeners, LPSAFEARRAY events)
486 HRESULT hr= S_OK;
487 long ubound= 0;
488 long lbound= 0;
489 long count= 0;
490 hr= SafeArrayGetUBound( listeners, 1, &ubound);
491 hr= SafeArrayGetLBound( listeners, 1, &lbound);
492 count= ubound - lbound +1;
494 // We assume thate the count of EventObjects in events is the same
495 for( long i = 0; i < count; i++)
497 CComVariant varListener;
498 CComVariant varEvent;
499 hr= SafeArrayGetElement( listeners, &i, &varListener);
500 hr= SafeArrayGetElement( events, &i, &varEvent);
501 if( varListener.vt == VT_DISPATCH && varEvent.vt == VT_DISPATCH)
503 CComDispatchDriver disp( varListener.pdispVal);
504 hr= disp.Invoke1(L"disposing", &varEvent);
509 return S_OK;