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