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