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// ReversePInvokePassingByRefNative.cpp : Defines the entry point for the DLL application.
6#include <xplatform.h>
7#include <limits.h>
8#include "platformdefines.h"
9#include "helper.h"
10
11//Func Pointer
12typedef BOOL (__cdecl *DelByteArrByRefAsCdeclCaller)(BYTE** arrByte, BYTE* arraySize);
13typedef BOOL (__cdecl *DelSbyteArrByRefAsCdeclCaller)(CHAR* arraySize, CHAR** arrSbyte);
14typedef BOOL (__cdecl *DelShortArrByRefAsCdeclCaller)(SHORT** arrShort, SHORT* arraySize);
15typedef BOOL (__cdecl *DelUshortArrByRefAsCdeclCaller)(USHORT** arrUshort, USHORT* arraySize);
16typedef BOOL (__cdecl *DelInt32ArrByRefAsCdeclCaller)(LONG** arrInt32, LONG* arraySize);
17typedef BOOL (__cdecl *DelUint32ArrByRefAsCdeclCaller)(ULONG** arrUint32, ULONG* arraySize);
18typedef BOOL (__cdecl *DelLongArrByRefAsCdeclCaller)(LONGLONG** arrLong, LONGLONG* arraySize);
19typedef BOOL (__cdecl *DelUlongArrByRefAsCdeclCaller)(ULONGLONG** arrUlong, ULONGLONG* arraySize);
20typedef BOOL (__cdecl *DelStringArrByRefAsCdeclCaller)(BSTR** arrString, LONG* arraySize);
21
22//#######################################################
23//Test Method
24//#######################################################
25
26//BYTE 0 ==> 20 size Array
27extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalByteArray_AsParam_AsByRef(DelByteArrByRefAsCdeclCaller caller)
28{
29 BYTE arrSize = 0;
30 BYTE* arrByte = InitArray<BYTE>(arrSize);
31
32 if(!caller(&arrByte, &arrSize))
33 {
34 printf("DoCallBack_MarshalByteArray_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
35 CoreClrFree(arrByte);
36 return FALSE;
37 }
38
39 return CheckArray(arrByte, arrSize, (BYTE)20);
40}
41
42//CHAR 1 ==> CHAR.Max size Array
43extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalSbyteArray_AsParam_AsByRef(DelSbyteArrByRefAsCdeclCaller caller)
44{
45 CHAR arrSize = 1;
46 CHAR* arrSbyte = InitArray<CHAR>((size_t)arrSize);
47
48 if(!caller(&arrSize, &arrSbyte))
49 {
50 printf("DoCallBack_MarshalSbyteArray_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
51 CoreClrFree(arrSbyte);
52 return FALSE;
53 }
54
55 return CheckArray(arrSbyte, (size_t)arrSize, (CHAR)127);
56}
57
58//SHORT -1 ==> 20 size Array(Actual: 10 ==> 20)
59extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalShortArray_AsParam_AsByRef(DelShortArrByRefAsCdeclCaller caller)
60{
61 SHORT arrSize = -1;
62 SHORT* arrShort = InitArray<SHORT>(SHORT(10));
63
64 if(!caller(&arrShort, &arrSize))
65 {
66 printf("DoCallBack_MarshalShortArray_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
67 CoreClrFree(arrShort);
68 return FALSE;
69 }
70
71 return CheckArray(arrShort, (size_t)arrSize, (SHORT)20);
72}
73
74//SHORT 10 ==> -1 size Array(Actual: 10 ==> 20)
75extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalShortArrayReturnNegativeSize_AsParam_AsByRef(DelShortArrByRefAsCdeclCaller caller)
76{
77 SHORT arrSize = 10;
78 SHORT* arrShort = InitArray<SHORT>((size_t)arrSize);
79
80 if(!caller(&arrShort, &arrSize))
81 {
82 printf("DoCallBack_MarshalShortArrayReturnNegativeSize_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
83 CoreClrFree(arrShort);
84 return FALSE;
85 }
86
87 if(arrSize == -1)
88 return CheckArray(arrShort, (SHORT)20, (SHORT)20);
89 else
90 return FALSE;
91}
92
93//USHORT ushort.Max ==> 20 size Array
94extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalUshortArray_AsParam_AsByRef(DelUshortArrByRefAsCdeclCaller caller)
95{
96 USHORT arrSize = 65535;
97 USHORT* arrUshort = InitArray<USHORT>(arrSize);
98
99 if(!caller(&arrUshort, &arrSize))
100 {
101 printf("DoCallBack_MarshalUshortArray_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
102 CoreClrFree(arrUshort);
103 return FALSE;
104 }
105
106 return CheckArray(arrUshort, arrSize, (USHORT)20);
107}
108
109//Int32 10 ==> 20 size Array
110extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalInt32Array_AsParam_AsByRef(DelInt32ArrByRefAsCdeclCaller caller)
111{
112 LONG arrSize = 10;
113 LONG* arrInt32 = InitArray<LONG>((size_t)arrSize);
114
115 if(!caller(&arrInt32, &arrSize))
116 {
117 printf("DoCallBack_MarshalInt32Array_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
118 CoreClrFree(arrInt32);
119 return FALSE;
120 }
121
122 return CheckArray(arrInt32, (size_t)arrSize, (LONG)20);
123}
124
125//UInt32 10 ==> 20 size Array
126extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalUint32Array_AsParam_AsByRef(DelUint32ArrByRefAsCdeclCaller caller)
127{
128 ULONG arrSize = 10;
129 ULONG* arrUint32 = InitArray<ULONG>(arrSize);
130
131 if(!caller(&arrUint32, &arrSize))
132 {
133 printf("DoCallBack_MarshalUint32Array_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
134 CoreClrFree(arrUint32);
135 return FALSE;
136 }
137
138 return CheckArray(arrUint32, arrSize, (ULONG)20);
139}
140
141//LONGLONG 10 ==> 20 size Array
142extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalLongArray_AsParam_AsByRef(DelLongArrByRefAsCdeclCaller caller)
143{
144 LONGLONG arrSize = 10;
145 LONGLONG* arrLong = InitArray<LONGLONG>(SIZE_T(arrSize));
146
147 if(!caller(&arrLong, &arrSize))
148 {
149 printf("DoCallBack_MarshalLongArray_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
150 CoreClrFree(arrLong);
151 return FALSE;
152 }
153
154 return CheckArray(arrLong, (SIZE_T)arrSize, 20);
155}
156
157//ULONGLONG 10 ==> 20 size Array
158extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalUlongArray_AsParam_AsByRef(DelUlongArrByRefAsCdeclCaller caller)
159{
160 ULONGLONG arrSize = 10;
161 ULONGLONG* arrUlong = InitArray<ULONGLONG>(SIZE_T(arrSize));
162
163 if(!caller(&arrUlong, &arrSize))
164 {
165 printf("DoCallBack_MarshalUlongArray_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
166 CoreClrFree(arrUlong);
167 return FALSE;
168 }
169
170 return CheckArray(arrUlong, (SIZE_T)arrSize, 20);
171}
172#ifdef _WIN32
173//BSTR 10 ==> 20 size Array
174extern "C" DLL_EXPORT BOOL __cdecl DoCallBack_MarshalStringArray_AsParam_AsByRef(DelStringArrByRefAsCdeclCaller caller)
175{
176 LONG arrSize = 10;
177 BSTR* arrString = InitArrayBSTR(arrSize);
178
179 if(!caller(&arrString, &arrSize))
180 {
181 printf("DoCallBack_MarshalStringArray_AsParam_AsByRef:\n\tThe Caller returns wrong value\n");
182 CoreClrFree(arrString);
183 return FALSE;
184 }
185
186 LONG ExpectedArraySize = 20;
187 BSTR* pExpectedArr = (BSTR*)CoreClrAlloc(sizeof(BSTR)*ExpectedArraySize);
188 for(LONG i = 0; i < ExpectedArraySize; ++i)
189 {
190 pExpectedArr[i] = ToBSTR(ExpectedArraySize - 1 - i);
191 }
192
193 if(!EqualArrayBSTR(arrString, arrSize, pExpectedArr, ExpectedArraySize))
194 {
195 printf("ManagedtoNative Error in Method: %s!\n",__FUNCTION__);
196 return FALSE;
197 }
198
199 CoreClrFree(arrString);
200 CoreClrFree(pExpectedArr);
201 return TRUE;
202}
203#endif
204