1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5#include <stdlib.h>
6#include <stdio.h>
7#include <xplatform.h>
8
9/*-----------------------------------------------------------------------------*
10* *
11* For MarshalDelegateAsField_AsFunctionPtr.cs *
12* / MarshalDelegateAsField_AsDefault.cs *
13*-----------------------------------------------------------------------------*/
14
15typedef int (STDMETHODCALLTYPE *FuncPtr)();
16
17//auxiliary verification value
18const int COMMONMETHODCALLED_RIGHT_RETVAL = 10;
19
20extern "C" DLL_EXPORT int STDMETHODCALLTYPE CommonMethod()
21{
22 printf("\n\tCalling CommonMethodCalled() by FuncPtr...");
23 return COMMONMETHODCALLED_RIGHT_RETVAL;
24}
25
26//FuncPtr funcPtr = CommonMethod;
27
28///////////////////////Struct_Sequential/////////////////////////
29typedef struct _Struct1_FuncPtrAsField1_Seq{
30 BOOL verification;
31 FuncPtr funcPtr;
32} Struct1_FuncPtrAsField1_Seq;
33
34extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateAsFieldInStruct_Seq(Struct1_FuncPtrAsField1_Seq sfs)
35{
36 if(!sfs.verification || sfs.funcPtr == NULL)
37 {
38 printf("TakeDelegateAsFieldInStruct_Seq:NULL field member.\n");
39 return FALSE;
40 }
41 else
42 {
43 return sfs.verification && (sfs.funcPtr() == COMMONMETHODCALLED_RIGHT_RETVAL);
44 }
45}
46
47///////////////////////Struct_Explicit///////////////////////
48typedef struct _Struct1_FuncPtrAsField2_Exp{
49 BOOL verification;
50 int Padding;
51 FuncPtr funcPtr;
52} Struct1_FuncPtrAsField2_Exp;
53
54extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateAsFieldInStruct_Exp(Struct1_FuncPtrAsField2_Exp sfe)
55{
56 if(!sfe.verification || sfe.funcPtr == NULL)
57 {
58 printf("TakeDelegateAsFieldInStruct_Exp:NULL field member.\n");
59 return FALSE;
60 }
61 else
62 {
63 return sfe.verification && sfe.funcPtr() == COMMONMETHODCALLED_RIGHT_RETVAL;
64 }
65}
66
67///////////////////////Struct_Sequential/////////////////////////
68class Class1_FuncPtrAsField3_Seq{
69public:
70 BOOL verification;
71 FuncPtr funcPtr;
72} ;
73
74extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateAsFieldInClass_Seq(Class1_FuncPtrAsField3_Seq *cfs)
75{
76 if(!cfs->verification || cfs->funcPtr == NULL)
77 {
78 printf("TakeDelegateAsFieldInClass_Seq:NULL field member.\n");
79 return FALSE;
80 }
81 else
82 {
83 return cfs->verification && (cfs->funcPtr() == COMMONMETHODCALLED_RIGHT_RETVAL);
84 }
85}
86
87///////////////////////Struct_Explicit///////////////////////
88class Class1_FuncPtrAsField4_Exp{
89public:
90 BOOL verification;
91 int Padding;
92 FuncPtr funcPtr;
93};
94
95extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE TakeDelegateAsFieldInClass_Exp(Class1_FuncPtrAsField4_Exp *cfe)
96{
97 if(!cfe->verification || cfe->funcPtr == NULL)
98 {
99 printf("TakeDelegateAsFieldInClass_Exp:NULL field member.\n");
100 return FALSE;
101 }
102 else
103 {
104 return cfe->verification && (cfe->funcPtr() == COMMONMETHODCALLED_RIGHT_RETVAL);
105 }
106}
107
108#ifdef _WIN32
109#include <windows.h>
110
111/*-----------------------------------------------------------------------------*
112* *
113* For MarshalDelegateAsField_AsInterface.cs *
114* *
115*-----------------------------------------------------------------------------*/
116
117#import "mscorlib.tlb" no_namespace named_guids raw_interfaces_only rename("ReportEvent","ReportEventNew")
118
119typedef struct{
120 int result1;
121 int result2;
122 int result3;
123} Result;
124
125const int COMMONMETHOD1_RESULT = 10;
126const int COMMONMETHOD2_RESULT = 20;
127const int COMMONMETHOD3_RESULT = 30;
128
129const Result expected = {
130 COMMONMETHOD1_RESULT,
131 COMMONMETHOD2_RESULT,
132 COMMONMETHOD3_RESULT
133};
134
135Result result = {0,0,0};
136
137void STDMETHODCALLTYPE ResetToZero()
138{
139 result.result1 = result.result2 = result.result3 = 0;
140}
141
142extern "C" DLL_EXPORT void STDMETHODCALLTYPE CommonMethod1()
143{
144 printf("\n\tCommonMethod1() Calling...\n");
145 result.result1 = COMMONMETHOD1_RESULT;
146}
147
148extern "C" DLL_EXPORT void STDMETHODCALLTYPE CommonMethod2()
149{
150 printf("\n\tCommonMethod2() Calling...\n");
151 result.result2 = COMMONMETHOD2_RESULT;
152}
153
154extern "C" DLL_EXPORT void STDMETHODCALLTYPE CommonMethod3()
155{
156 printf("\n\tCommonMethod3() Calling...\n");
157 result.result3 = COMMONMETHOD3_RESULT;
158}
159
160bool STDMETHODCALLTYPE Verify(Result expectedR, Result resultR)
161{
162 return expectedR.result1 == resultR.result1
163 && expectedR.result2 == resultR.result2
164 && expectedR.result3 == resultR.result3;
165}
166
167
168typedef struct _Struct3_InterfacePtrAsField1_Seq{
169 BOOL verification;
170 _Delegate * p_dele;
171}Struct3_InterfacePtrAsField1_Seq;
172
173extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrAsFieldInStruct_Seq(Struct3_InterfacePtrAsField1_Seq sis)
174{
175 HRESULT hr;
176
177 ResetToZero();
178
179 if(sis.verification == NULL || sis.p_dele == NULL)
180 {
181 printf("NULL field member.\n");
182 return FALSE;
183 }
184 else
185 {
186 hr = sis.p_dele->DynamicInvoke( NULL, NULL);
187 if(FAILED(hr))
188 {
189 return FALSE;
190 }
191 bool tempBool = sis.verification && Verify( expected, result);
192
193
194 //IDispatch::Invoke
195 ResetToZero();
196
197 BSTR bstrNames[1];
198 bstrNames[0] = SysAllocString(L"DynamicInvoke");
199 DISPID dispid = 0;
200 hr = sis.p_dele->GetIDsOfNames(
201 IID_NULL,
202 bstrNames,
203 sizeof(bstrNames) / sizeof(bstrNames[0]),
204 GetUserDefaultLCID(),
205 &dispid);
206
207 SysFreeString(bstrNames[0]);
208
209 if(FAILED(hr))
210 {
211 printf("\nERROR: Invoke failed: 0x%x\n", (unsigned int)hr);
212 return FALSE;
213 }
214
215 VARIANT args[1];
216 VariantInit(&args[0]);
217 args[0].vt = VT_ARRAY|VT_VARIANT;
218 args[0].parray = NULL;
219 DISPPARAMS params = { args, NULL, 1, 0 };
220
221 hr = sis.p_dele->Invoke(
222 dispid,
223 IID_NULL,
224 GetUserDefaultLCID(),
225 DISPATCH_METHOD,
226 &params,
227 NULL,
228 NULL,
229 NULL);
230
231 if(FAILED(hr))
232 {
233 printf("\nERROR: Invoke failed: 0x%x\n", (unsigned int)hr);
234 return FALSE;
235 }
236
237 return tempBool && Verify(expected, result);
238 }
239}
240
241typedef struct _Struct3_InterfacePtrAsField2_Exp{
242 bool verification;
243 int Padding;
244 _Delegate * p_dele;
245}Struct3_InterfacePtrAsField2_Exp;
246
247extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrAsFieldInStruct_Exp(Struct3_InterfacePtrAsField2_Exp sie)
248{
249 HRESULT hr;
250
251 ResetToZero();
252
253 if(sie.verification == NULL || sie.p_dele == NULL)
254 {
255 printf("NULL field member.\n");
256 return FALSE;
257 }
258 else
259 {
260 hr = sie.p_dele->DynamicInvoke(NULL, NULL);
261 if(FAILED(hr))
262 {
263 return FALSE;
264 }
265 bool tempBool = sie.verification && Verify( expected, result);
266
267
268 //IDispatch::Invoke
269 ResetToZero();
270
271 BSTR bstrNames[1];
272 bstrNames[0] = SysAllocString(L"DynamicInvoke");
273 DISPID dispid = 0;
274 hr = sie.p_dele->GetIDsOfNames(
275 IID_NULL,
276 bstrNames,
277 sizeof(bstrNames) / sizeof(bstrNames[0]),
278 GetUserDefaultLCID(),
279 &dispid);
280
281 SysFreeString(bstrNames[0]);
282
283 if(FAILED(hr))
284 {
285 printf("\nERROR: Invoke failed: 0x%x\n", (unsigned int)hr);
286 return FALSE;
287 }
288
289 VARIANT args[1];
290 VariantInit(&args[0]);
291 args[0].vt = VT_ARRAY|VT_VARIANT;
292 args[0].parray = NULL;
293 DISPPARAMS params = { args, NULL, 1, 0 };
294
295 hr = sie.p_dele->Invoke(
296 dispid,
297 IID_NULL,
298 GetUserDefaultLCID(),
299 DISPATCH_METHOD,
300 &params,
301 NULL,
302 NULL,
303 NULL);
304 if(FAILED(hr))
305 {
306 printf("\nERROR: Invoke failed: 0x%x\n", (unsigned int)hr);
307 return FALSE;
308 }
309
310 return tempBool && Verify(expected, result);
311 }
312}
313
314class Class3_InterfacePtrAsField3_Seq
315{
316public:
317 bool verification;
318 _Delegate * p_dele;
319};
320
321extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrAsFieldInClass_Seq(Class3_InterfacePtrAsField3_Seq *cis)
322{
323 HRESULT hr;
324
325 ResetToZero();
326
327 if(cis->verification == NULL || cis->p_dele == NULL)
328 {
329 printf("NULL field member.\n");
330 return FALSE;
331 }
332 else
333 {
334 hr = (cis->p_dele)->DynamicInvoke(NULL, NULL);
335 if(FAILED(hr))
336 {
337 return FALSE;
338 }
339 bool tempBool = cis->verification && Verify( expected, result);
340
341
342 //IDispatch::Invoke
343 BSTR bstrNames[1];
344 bstrNames[0] = SysAllocString(L"DynamicInvoke");
345 DISPID dispid = 0;
346 hr = (cis->p_dele)->GetIDsOfNames(
347 IID_NULL,
348 bstrNames,
349 sizeof(bstrNames) / sizeof(bstrNames[0]),
350 GetUserDefaultLCID(),
351 &dispid);
352
353 SysFreeString(bstrNames[0]);
354
355 if(FAILED(hr))
356 {
357 printf("\nERROR: Invoke failed: 0x%x\n", (unsigned int)hr);
358 return FALSE;
359 }
360
361 VARIANT args[1];
362 VariantInit(&args[0]);
363 args[0].vt = VT_ARRAY|VT_VARIANT;
364 args[0].parray = NULL;
365 DISPPARAMS params = { args, NULL, 1, 0 };
366
367 hr = (cis->p_dele)->Invoke(
368 dispid,
369 IID_NULL,
370 GetUserDefaultLCID(),
371 DISPATCH_METHOD,
372 &params,
373 NULL,
374 NULL,
375 NULL);
376
377 if(FAILED(hr))
378 {
379 printf("\nERROR: Invoke failed: 0x%x\n", (unsigned int)hr);
380 return FALSE;
381 }
382
383 return tempBool && Verify(expected, result);
384 }
385
386}
387
388class Class3_InterfacePtrAsField4_Exp{
389public:
390 bool verification;
391 int Padding;
392 _Delegate * p_dele;
393};
394
395extern "C" DLL_EXPORT BOOL STDMETHODCALLTYPE Take_DelegatePtrAsFieldInClass_Exp(Class3_InterfacePtrAsField4_Exp *cie)
396{
397 HRESULT hr;
398
399 ResetToZero();
400
401 if(cie->verification == NULL || cie->p_dele == NULL)
402 {
403 printf("NULL field member.\n");
404 return FALSE;
405 }
406 else
407 {
408 hr = (cie->p_dele)->DynamicInvoke(NULL, NULL);
409 if(FAILED(hr))
410 {
411 return FALSE;
412 }
413 bool tempBool = cie->verification && Verify( expected, result);
414
415
416 //IDispatch::Invoke
417 BSTR bstrNames[1];
418 bstrNames[0] = SysAllocString(L"DynamicInvoke");
419 DISPID dispid = 0;
420 hr = (cie->p_dele)->GetIDsOfNames(
421 IID_NULL,
422 bstrNames,
423 sizeof(bstrNames) / sizeof(bstrNames[0]),
424 GetUserDefaultLCID(),
425 &dispid);
426
427 SysFreeString(bstrNames[0]);
428
429 if(FAILED(hr))
430 {
431 printf("\nERROR: Invoke failed: 0x%x\n", (unsigned int)hr);
432 return FALSE;
433 }
434
435 VARIANT args[1];
436 VariantInit(&args[0]);
437 args[0].vt = VT_ARRAY|VT_VARIANT;
438 args[0].parray = NULL;
439 DISPPARAMS params = { args, NULL, 1, 0 };
440
441 hr = (cie->p_dele)->Invoke(
442 dispid,
443 IID_NULL,
444 GetUserDefaultLCID(),
445 DISPATCH_METHOD,
446 &params,
447 NULL,
448 NULL,
449 NULL);
450 if(FAILED(hr))
451 {
452 printf("\nERROR: Invoke failed: 0x%x\n", (unsigned int)hr);
453 return FALSE;
454 }
455
456 return tempBool && Verify(expected, result);
457 }
458}
459#endif
460