Add PInvoke/SizeParamIndex tests (#19348)
[platform/upstream/coreclr.git] / tests / src / Interop / PInvoke / SizeParamIndex / PInvoke / PassingByOut / PInvokePassingByOutNative.cpp
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 // PInvokePassingByOutNative.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 //#####################################################################
13 //ByOut Array, ByRef SizeParamIndex
14 //#####################################################################
15
16 //BYTE 0 ==> 20 size Array
17 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayByte_AsByOut_AsSizeParamIndex(BYTE* arrSize, BYTE** ppActual)
18 {
19     return CheckAndChangeArrayByOut(ppActual, arrSize, (BYTE)1);
20 }
21
22 //CHAR 1 ==> CHAR.Max size Array
23 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArraySbyte_AsByOut_AsSizeParamIndex(CHAR* arrSize, CHAR** ppActual)
24 {
25     return CheckAndChangeArrayByOut(ppActual, arrSize, (CHAR)SCHAR_MAX); 
26 }
27
28 //SHORT -1 ==> 20 size Array
29 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayShort_AsByOut_AsSizeParamIndex(/*out*/SHORT* arrSize, SHORT** ppActual)
30 {
31     short shortArray_Size = 16384;//SHRT_MAX+1/2
32
33     *ppActual = (SHORT*)CoreClrAlloc(sizeof(SHORT)*shortArray_Size);
34
35     *arrSize = shortArray_Size;
36
37     for(SHORT i = 0; i < shortArray_Size; ++i)
38     {
39         (*ppActual)[i] = shortArray_Size - 1 - i;
40     }
41     return TRUE;
42 }
43
44 //SHORT 10 ==> -1 size Array
45 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayShortReturnNegative_AsByOut_AsSizeParamIndex(SHORT* arrSize, SHORT** ppActual)
46 {
47     *ppActual = (SHORT*)CoreClrAlloc(sizeof(SHORT)*CArray_Size);
48     *arrSize = -1;
49
50     for(SHORT i = 0; i < CArray_Size; ++i)
51     {
52         (*ppActual)[i] = CArray_Size - 1 - i;
53     }
54     return TRUE;
55 }
56
57 //USHORT ? ==> ushort.Max ==>  size Array 
58 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayUshort_AsByOut_AsSizeParamIndex(USHORT** ppActual, USHORT* arrSize)
59 {
60     return CheckAndChangeArrayByOut(ppActual, arrSize, (USHORT)USHRT_MAX);
61 }
62
63 //Int32 ? ==> 20 size Array
64 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayInt_AsByOut_AsSizeParamIndex(LONG* arrSize,LONG** ppActual)
65 {
66     return CheckAndChangeArrayByOut(ppActual, arrSize, (LONG)0);
67 }
68
69 //ULONG 10 ==> 20 size Array
70 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayUInt_AsByOut_AsSizeParamIndex(ULONG* arrSize, ULONG** ppActual)
71 {
72     return CheckAndChangeArrayByOut(ppActual, arrSize, (ULONG)20);
73 }
74
75 //LONGLONG 10 ==> 20 size Array
76 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayLong_AsByOut_AsSizeParamIndex(LONGLONG* arrSize, LONGLONG** ppActual)
77 {
78     return CheckAndChangeArrayByOut(ppActual, arrSize, (LONGLONG)20);
79 }
80
81 //ULONGLONG 10 ==> 20 size Array
82 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayUlong_AsByOut_AsSizeParamIndex(ULONGLONG** ppActual,ULONGLONG* arrSize,ULONGLONG _unused)
83 {
84     return CheckAndChangeArrayByOut(ppActual, arrSize, (ULONGLONG)1000);
85 }
86 #ifdef _WIN32
87 //String 10 size Array ==> BSTR 20 size Array
88 extern "C" DLL_EXPORT BOOL __stdcall MarshalCStyleArrayString_AsByOut_AsSizeParamIndex(BSTR** ppBSTR,short* arrSize)
89 {
90     *ppBSTR = (BSTR*)CoreClrAlloc(sizeof(BSTR)*CArray_Size);
91     for(int i = 0;i<CArray_Size;++i)
92     {
93         (*ppBSTR)[i] = ToBSTR(CArray_Size - 1 - i);
94     }
95
96     *arrSize = CArray_Size;
97
98     return TRUE;
99 }
100 #endif