1// Copyright (c) Microsoft. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3//
4
5#include <xplatform.h>
6#include <stdio.h>
7#include <stdlib.h>
8
9const int iManaged = 10;
10const int iNative = 11;
11
12const int ArraySIZE = 5;
13
14
15//Rule for Passing Value
16// Reverse Pinvoke
17//M--->N true,true,true,true,true
18//N----M true,false,true,false,true
19
20//Reverse PInvoke
21//Pass by value
22typedef BOOL (STDMETHODCALLTYPE *CallBackIn)(int size,bool arr[]);
23extern "C" DLL_EXPORT BOOL DoCallBackIn(CallBackIn callback)
24{
25 //Init
26 bool * arr = (bool*)CoreClrAlloc(ArraySIZE);
27 for(int i = 0;i < ArraySIZE; i++ )
28 {
29 if( 0 == i%2)
30 {
31 arr[i] =true;
32 }
33 else
34 {
35 arr[i] = false;
36 }
37 }
38
39 if(!callback(ArraySIZE,arr))
40 {
41 printf("Native Side: in DoCallBackIn, The Callback return wrong value");
42 return false;
43 }
44
45 //Check the data
46 for(int i = 0;i < ArraySIZE; i++ )//Expected:true,false,true,false,true
47 {
48 if((0 == (i%2)) && !arr[i]) //expect true
49 {
50 printf("Native Side:Error in DoCallBackIn.The Item is %d\n",i+1);
51 CoreClrFree(arr);
52 return false;
53 }
54 else if((1 == (i%2))&&arr[i]) //expect false
55 {
56 printf("Native Side:Error in DoCallBackIn.The Item is %d\n",i+1);
57 CoreClrFree(arr);
58 return false;
59 }
60 }
61 CoreClrFree(arr);
62 return true;
63}
64
65typedef BOOL (STDMETHODCALLTYPE *CallBackOut)(int size,bool arr[]);
66extern "C" DLL_EXPORT BOOL DoCallBackOut(CallBackOut callback)
67{
68 bool * arr =(bool *)CoreClrAlloc(ArraySIZE);
69
70 if(!callback(ArraySIZE,arr))
71 {
72 printf("Native Side: in DoCallBackOut, The Callback return wrong value");
73 return FALSE;
74 }
75
76 //Check the data returnd from Managed Side
77 for(int i = 0;i < ArraySIZE; i++ )
78 {
79 if(!arr[i]) //expect true
80 {
81 printf("Native Side:Error in DoCallBackOut.The Item is %d\n",i+1);
82 CoreClrFree(arr);
83 return false;
84 }
85 }
86 CoreClrFree(arr);
87 return true;
88}
89
90typedef BOOL (STDMETHODCALLTYPE *CallBackInOut)(int size,bool arr[]);
91extern "C" DLL_EXPORT BOOL DoCallBackInOut(CallBackInOut callback)
92{
93 //Init
94 bool * arr =(bool *)CoreClrAlloc(ArraySIZE);
95 for(int i = 0;i < ArraySIZE; i++ )
96 {
97 if( 0 == i%2)
98 {
99 arr[i] = true;
100 }
101 else
102 {
103 arr[i] = false;
104 }
105 }
106
107 if(!callback(ArraySIZE,arr))
108 {
109 printf("Native Side: in DoCallBackInOut, The Callback return wrong value");
110
111 return FALSE;
112 }
113
114 //Check the data
115 for(int i = 0;i < ArraySIZE; i++ )
116 {
117 if(!arr[i]) //expect true
118 {
119 printf("Native Side:Error in DoCallBackInOut.The Item is %d\n",i+1);
120 CoreClrFree(arr);
121 return false;
122 }
123 }
124 CoreClrFree(arr);
125 return true;
126}
127
128
129//Reverse PInvoke
130//Pass by reference
131typedef BOOL (STDMETHODCALLTYPE *CallBackRefIn)(int size,bool ** arr);
132extern "C" DLL_EXPORT BOOL DoCallBackRefIn(CallBackRefIn callback)
133{
134 //Init:true,false,true,false,true
135 bool *parr = (bool *)CoreClrAlloc(ArraySIZE);
136
137 for(int i = 0;i < ArraySIZE;++i)
138 {
139 if( 0 == i%2)
140 {
141 parr[i] = true;
142 }
143 else
144 {
145 parr[i] = false;
146 }
147 }
148
149 if(!callback(ArraySIZE,&parr)) // &parr
150 {
151 printf("Native Side: in DoCallBackRefIn, The Callback return wrong value");
152 return FALSE;
153 }
154
155 //Check the data werent changed
156 for(int i = 0;i<ArraySIZE;++i)
157 {
158 if((0==(i%2)) && !parr[i]) //expect true
159 {
160 printf("Native Side:Error in DoCallBackInOut.The Item is %d\n",i+1);
161 CoreClrFree(parr);
162 return false;
163 }
164 else if((1==(i%2))&&parr[i]) //expect false
165 {
166 printf("Native Side:Error in DoCallBackInOut.The Item is %d\n",i+1);
167 CoreClrFree(parr);
168 return false;
169 }
170 }
171 CoreClrFree(parr);
172 return true;
173}
174
175typedef BOOL (STDMETHODCALLTYPE *CallBackRefOut)(int size,bool ** arr);
176extern "C" DLL_EXPORT BOOL DoCallBackRefOut(CallBackRefOut callback)
177{
178
179 bool* parr = NULL;
180
181 if(!callback(ArraySIZE,&parr))
182 {
183 printf("Native Side: in DoCallBackRefOut, The Callback return wrong value");
184 return FALSE;
185 }
186
187 //Check the data were changed to true,true
188 for(int i = 0;i<ArraySIZE;++i)
189 {
190 if(!(*(parr + i))) //expect true
191 {
192 printf("Native Side:Error in DoCallBackRefOut.The Item is %d\n",i+1);
193 CoreClrFree(parr);
194 return false;
195 }
196 }
197 CoreClrFree(parr);
198 return true;
199}
200
201typedef BOOL (STDMETHODCALLTYPE *CallBackRefInOut)(int size,bool ** arr);
202extern "C" DLL_EXPORT BOOL DoCallBackRefInOut(CallBackRefInOut callback)
203{
204 //Init,true,false,true,false
205 bool* parr = (bool*)CoreClrAlloc(ArraySIZE);
206 for(int i = 0;i<ArraySIZE;++i)
207 {
208 if( 0 == i%2)
209 {
210 parr[i] = true;
211 }
212 else
213 {
214 parr[i] = false;
215 }
216 }
217
218 if(!callback(ArraySIZE,&parr))
219 {
220 printf("Native Side: in DoCallBackRefInOut, The Callback return wrong value");
221 return FALSE;
222 }
223
224 //Check the data were changed to true,true
225 for(int i = 0;i<ArraySIZE;++i)
226 {
227 if(!(parr[i])) //expect true
228 {
229 printf("Native Side:Error in DoCallBackRefOut.The Item is %d\n",i+1);
230 CoreClrFree(parr);
231 return false;
232 }
233 }
234 CoreClrFree(parr);
235 return true;
236}
237