xref: /trunk/main/extensions/test/ole/unoTocomCalls/XCallback_Impl/Callback.cpp (revision fc9fd3f14a55d77b35643a64034752a178b2a5b0)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
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.
14  *
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).
20  *
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.
25  *
26  ************************************************************************/
27 // Callback.cpp : Implementation of CCallback
28 #include "stdafx.h"
29 #include "XCallback_Impl.h"
30 #include "Callback.h"
31 
32 /////////////////////////////////////////////////////////////////////////////
33 // CCallback
34 
35 
36 STDMETHODIMP CCallback::func1()
37 {
38     MessageBox( NULL, _T("Callback::func1 called"),_T(""), MB_OK);
39     return S_OK;
40 }
41 
42 STDMETHODIMP CCallback::returnInterface(IDispatch **ppdisp)
43 {
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;
51 }
52 
53 STDMETHODIMP CCallback::outInterface(IDispatch **ppdisp)
54 {
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();
62 
63 //  MessageBox( NULL, _T("CCallback::outInterface"), _T(""), MB_OK);
64     return S_OK;
65 }
66 
67 STDMETHODIMP CCallback::outValuesMixed(long val, long *pval, BSTR string)
68 {
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;
75 }
76 
77 
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 //)
93 {
94 //  if( ! ppdisp || ! ppSimpleStruct || ! aSimpleEnum ||
95 //      ! outSeq || !varAny ||! aBool || ! aChar ||
96 //      ! aString || ! aFloat || ! aDouble || ! aByte ||
97 //      ! aShort || ! aLong || ! aUShort || ! aULong)
98 //      return E_POINTER;
99 
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;
115 }
116 
117 STDMETHODIMP CCallback::outStruct(IDispatch **outStruct)
118 {
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);
124 
125     CComPtr<IDispatch> _dispMgr;
126     if( SUCCEEDED(hr= _dispMgr.CoCreateInstance(L"com.sun.star.ServiceManager")))
127     {
128         CComDispatchDriver manager( _dispMgr);
129         CComVariant param1(L"com.sun.star.reflection.CoreReflection");
130         CComVariant varRet;
131         hr= manager.Invoke1( L"createInstance", &param1, &varRet);
132 
133         CComDispatchDriver reflection( varRet.pdispVal);
134         param1= L"oletest.SimpleStruct";
135         varRet.Clear();
136         hr= reflection.Invoke1( L"forName", &param1, &varRet);
137 
138         CComDispatchDriver classSimpleStruct( varRet.pdispVal);
139 
140         CComPtr<IDispatch> dispStruct;
141         param1.vt= VT_DISPATCH | VT_BYREF;
142         param1.ppdispVal= &dispStruct;
143         if( SUCCEEDED( hr= classSimpleStruct.Invoke1(L"createObject", &param1)))
144         {
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;
152         }
153     }
154     return hr;
155 }
156 
157 STDMETHODIMP CCallback::outEnum(long *outEnum)
158 {
159     if( !outEnum)
160         return E_POINTER;
161     *outEnum= 1;
162     return S_OK;
163 }
164 
165 STDMETHODIMP CCallback::outSeqAny(LPSAFEARRAY* outSeq)
166 {
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++)
174     {
175         SafeArrayPutElement( pArr, &i, (void*)&var[i]);
176     }
177 
178     *outSeq= pArr;
179     return S_OK;
180 }
181 
182 // ATLASSERT //VT_EMPTY
183 
184 
185 STDMETHODIMP CCallback::outAny(VARIANT *outAny)
186 {
187     if( ! outAny)
188         return E_POINTER;
189     outAny->vt= VT_BSTR;
190     outAny->bstrVal= SysAllocString( L"This is a string in a VARIANT");
191 
192     return S_OK;
193 }
194 
195 STDMETHODIMP CCallback::outBool(VARIANT_BOOL *outBool)
196 {
197     if( ! outBool)
198         return E_POINTER;
199     *outBool= VARIANT_TRUE;
200     return S_OK;
201 }
202 
203 STDMETHODIMP CCallback::outChar(short *outChar)
204 {
205     if( !outChar)
206         return E_POINTER;
207     *outChar= (short)L'A';
208     return S_OK;
209 }
210 
211 STDMETHODIMP CCallback::outString(BSTR *outString)
212 {
213     if( !outString)
214         return E_POINTER;
215     *outString= SysAllocString(L"This is a BSTR");
216     return S_OK;
217 }
218 
219 STDMETHODIMP CCallback::outFloat(float *outFloat)
220 {
221     if( !outFloat)
222         return E_POINTER;
223     *outFloat= 3.14f;
224     return S_OK;
225 }
226 
227 STDMETHODIMP CCallback::outDouble(double *outDouble)
228 {
229     if(!outDouble)
230         return E_POINTER;
231 
232     *outDouble= 3.145;
233     return S_OK;
234 }
235 
236 
237 
238 STDMETHODIMP CCallback::outShort(short *outShort)
239 {
240     if(!outShort)
241         return E_POINTER;
242     *outShort= -1;
243     return S_OK;
244 }
245 
246 STDMETHODIMP CCallback::outLong(long *outLong)
247 {
248     if(!outLong)
249         return E_POINTER;
250     *outLong= 0xffffffff;
251     return S_OK;
252 }
253 
254 
255 
256 STDMETHODIMP CCallback::outByte(unsigned char* outByte)
257 {
258     if(!outByte)
259         return E_POINTER;
260     *outByte= 0xff;
261     return S_OK;
262 }
263 
264 STDMETHODIMP CCallback::inoutInterface(IDispatch **ppdisp)
265 {
266     if( !ppdisp)
267         return E_POINTER;
268     CComDispatchDriver disp( *ppdisp);
269     CComVariant param1(L"");
270     disp.Invoke1(L"func", &param1);
271 
272     (*ppdisp)->Release();
273 
274     CComPtr<IDispatch> outDisp;
275     outDisp.CoCreateInstance( L"XCallback_Impl.Simple");
276     *ppdisp= outDisp;
277     (*ppdisp)->AddRef();
278 
279     return S_OK;
280 }
281 
282 STDMETHODIMP CCallback::inoutStruct(IDispatch **inoutVal)
283 {
284     if( !inoutVal)
285         return E_POINTER;
286     HRESULT hr= S_OK;
287     USES_CONVERSION;
288     CComVariant var;
289     CComDispatchDriver disp( *inoutVal);
290 
291     hr= disp.GetPropertyByName(L"message", &var);
292     MessageBox( NULL, W2T(var.bstrVal), _T("XCallback_Impl.Callback"), MB_OK);
293 
294     (*inoutVal)->Release();
295 
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);
301 
302     *inoutVal= dispStruct;
303     (*inoutVal)->AddRef();
304     return hr;
305 }
306 
307 STDMETHODIMP CCallback::inoutEnum(long *inoutVal)
308 {
309     if( !inoutVal)
310         return E_POINTER;
311     *inoutVal= *inoutVal+1;
312 
313     return S_OK;
314 }
315 
316 STDMETHODIMP CCallback::inoutSeqAny(LPSAFEARRAY *pArray)
317 {
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;
326 
327     // the Array is supposet to contain variants
328     CComVariant var;
329     for( long i=0; i<count; i++)
330     {
331         var.Clear();
332         hr= SafeArrayGetElement( *pArray, &i, (void*)&var);
333     }
334 
335     SafeArrayDestroy( *pArray);
336 
337     outSeqAny( pArray);
338     return S_OK;
339 }
340 
341 STDMETHODIMP CCallback::inoutAny(VARIANT *inoutVal)
342 {
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);
348 
349     VariantClear( inoutVal);
350     inoutVal->vt= VT_BSTR;
351     inoutVal->bstrVal=SysAllocString( L" [string] XCallback_Impl.Callback inoutAny");
352     return S_OK;
353 }
354 
355 STDMETHODIMP CCallback::inoutBool(VARIANT_BOOL *inoutVal)
356 {
357     if( !inoutVal)
358         return E_POINTER;
359 
360     *inoutVal= *inoutVal == VARIANT_TRUE ? VARIANT_FALSE : VARIANT_TRUE;
361     return S_OK;
362 }
363 
364 STDMETHODIMP CCallback::inoutChar(short *inoutVal)
365 {
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;
374 }
375 
376 STDMETHODIMP CCallback::inoutString(BSTR *inoutVal)
377 {
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");
384 
385     return S_OK;
386 }
387 
388 STDMETHODIMP CCallback::inoutFloat(float *inoutVal)
389 {
390     if( !inoutVal)
391         return E_POINTER;
392     *inoutVal = *inoutVal+1;
393     return S_OK;
394 }
395 
396 STDMETHODIMP CCallback::inoutDouble(double *inoutVal)
397 {
398     if( !inoutVal)
399         return E_POINTER;
400     *inoutVal= *inoutVal+1;
401     return S_OK;
402 }
403 
404 STDMETHODIMP CCallback::inoutByte(unsigned char *inoutVal)
405 {
406     if( !inoutVal)
407         return E_POINTER;
408     *inoutVal= 0xff;
409     return S_OK;
410 }
411 
412 STDMETHODIMP CCallback::inoutShort(short *inoutVal)
413 {
414     if( !inoutVal)
415         return E_POINTER;
416     *inoutVal= -1;
417     return S_OK;
418 }
419 
420 STDMETHODIMP CCallback::inoutLong(long* inoutVal)
421 {
422     if( !inoutVal)
423         return E_POINTER;
424     *inoutVal= 0xffffffff;
425     return S_OK;
426 }
427 
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)
442 {
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);
456 
457     return S_OK;
458 }
459 
460 
461 STDMETHODIMP CCallback::inValues(short aChar, long aLong, BSTR aString)
462 {
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;
469 }
470 
471 STDMETHODIMP CCallback::outSeqByte(LPSAFEARRAY * outVal)
472 {
473     // TODO: Add your implementation code here
474 
475     return S_OK;
476 }
477 
478 STDMETHODIMP CCallback::inSeqByte( LPSAFEARRAY listeners)
479 {
480 
481     return S_OK;
482 }
483 
484 STDMETHODIMP CCallback::inSeqXEventListener( LPSAFEARRAY listeners, LPSAFEARRAY events)
485 {
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;
493 
494     // We assume thate the count of EventObjects in events is the same
495     for( long i = 0; i < count; i++)
496     {
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)
502         {
503             CComDispatchDriver disp( varListener.pdispVal);
504             hr= disp.Invoke1(L"disposing", &varEvent);
505         }
506 
507     }
508 
509     return S_OK;
510 }
511 
512 
513