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.
6 using System.Runtime.InteropServices;
10 /// Pass LPArray Size by ref keyword using SizeParamIndex Attributes
13 public class ClientMarshalArrayAsSizeParamIndexByRefTest
18 [DllImport("PInvokePassingByRefNative")]
19 private static extern bool MarshalCStyleArrayByte_AsByRef_AsSizeParamIndex(
20 ref byte arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref byte[] arrByte);
22 [DllImport("PInvokePassingByRefNative")]
23 private static extern bool MarshalCStyleArraySbyte_AsByRef_AsSizeParamIndex(
24 ref sbyte arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref sbyte[] arrSbyte);
26 [DllImport("PInvokePassingByRefNative")]
27 private static extern bool MarshalCStyleArrayShort_AsByRef_AsSizeParamIndex(
28 ref short arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref short[] arrShort);
30 [DllImport("PInvokePassingByRefNative")]
31 private static extern bool MarshalCStyleArrayShortReturnNegative_AsByRef_AsSizeParamIndex(
32 ref short arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref short[] arrShort);
34 [DllImport("PInvokePassingByRefNative")]
35 private static extern bool MarshalCStyleArrayUshort_AsByRef_AsSizeParamIndex(
36 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] ref ushort[] arrUshort, ref ushort arrSize);
38 [DllImport("PInvokePassingByRefNative")]
39 private static extern bool MarshalCStyleArrayInt_AsByRef_AsSizeParamIndex(
40 ref Int32 arrSize, Int32 unused, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref Int32[] arrInt32);
42 [DllImport("PInvokePassingByRefNative")]
43 private static extern bool MarshalCStyleArrayUInt_AsByRef_AsSizeParamIndex(
44 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 2)] ref UInt32[] arrUInt32, UInt32 unused, ref UInt32 arrSize);
46 [DllImport("PInvokePassingByRefNative")]
47 private static extern bool MarshalCStyleArrayLong_AsByRef_AsSizeParamIndex(
48 ref long arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref long[] arrLong);
50 [DllImport("PInvokePassingByRefNative")]
51 private static extern bool MarshalCStyleArrayUlong_AsByRef_AsSizeParamIndex(
52 ref ulong arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] ref ulong[] arrUlong);
54 [DllImport("PInvokePassingByRefNative")]
55 private static extern bool MarshalCStyleArrayString_AsByRef_AsSizeParamIndex(
56 ref int arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, ArraySubType = UnmanagedType.BStr)] ref string[] arrStr,
57 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0, ArraySubType = UnmanagedType.LPStr)] ref string[] arrStr2);
61 static void SizeParamTypeIsByte()
63 string strDescription = "Scenario(byte==>BYTE):Array_Size(M->N)=1,Array_Size(N->M)= byte.MinValue";
65 Console.WriteLine(strDescription + " Starts!");
67 byte byte_Array_Size = 1;
68 byte[] arrByte = Helper.InitArray<byte>(byte_Array_Size);
69 Assert.IsTrue(MarshalCStyleArrayByte_AsByRef_AsSizeParamIndex(ref byte_Array_Size, ref arrByte));
71 //Construct Expected array
72 int expected_ByteArray_Size = Byte.MinValue;
73 byte[] expectedArrByte = Helper.GetExpChangeArray<byte>(expected_ByteArray_Size);
74 Assert.IsTrue(Helper.EqualArray<byte>(arrByte, (int)byte_Array_Size, expectedArrByte, (int)expectedArrByte.Length));
76 Console.WriteLine(strDescription + " Ends!");
79 static void SizeParamTypeIsSByte()
81 string strDescription = "Scenario(sbyte==>CHAR): Array_Size(M->N) = 10, Array_Size(N->M) = sbyte.Max";
83 Console.WriteLine(strDescription + " Starts!");
85 sbyte sbyte_Array_Size = (sbyte)10;
86 sbyte[] arrSbyte = Helper.InitArray<sbyte>(sbyte_Array_Size);
88 Assert.IsTrue(MarshalCStyleArraySbyte_AsByRef_AsSizeParamIndex(ref sbyte_Array_Size, ref arrSbyte));
91 sbyte[] expectedArrSbyte = Helper.GetExpChangeArray<sbyte>(sbyte.MaxValue);
92 Assert.IsTrue(Helper.EqualArray<sbyte>(arrSbyte, (int)sbyte_Array_Size, expectedArrSbyte, (int)sbyte.MaxValue));
94 Console.WriteLine(strDescription + " Ends!");
97 static void SizeParamTypeIsShort1()
99 string strDescription = "Scenario(short==>SHORT)1: Array_Size(M->N) = -1, Array_Size(N->M) = 20";
101 Console.WriteLine(strDescription + " Starts!");
103 short short_Array_Size = (short)-1;
104 short[] arrShort = Helper.InitArray<short>(10);
105 int expected_ByteArraySize = 20;
107 Assert.IsTrue(MarshalCStyleArrayShort_AsByRef_AsSizeParamIndex(ref short_Array_Size, ref arrShort));
110 short[] expectedArrShort = Helper.GetExpChangeArray<short>(expected_ByteArraySize);
111 Assert.IsTrue(Helper.EqualArray<short>(arrShort, (int)short_Array_Size, expectedArrShort, expectedArrShort.Length));
113 Console.WriteLine(strDescription + " Ends!");
116 static void SizeParamTypeIsShort2()
118 string strDescription = "Scenario(short==>SHORT)2: Array_Size(M->N) = 10, Array_Size(N->M) = -1";
120 Console.WriteLine(strDescription + " Starts!");
122 short short_Array_Size = (short)10;
123 short[] arrShort = Helper.InitArray<short>(10);
124 Assert.Throws<OverflowException>(() => MarshalCStyleArrayShortReturnNegative_AsByRef_AsSizeParamIndex(ref short_Array_Size, ref arrShort));
125 Console.WriteLine(strDescription + " Ends!");
128 static void SizeParamTypeIsUShort()
130 string strDescription = "Scenario(ushort==>USHORT): Array_Size(M->N) = 0, Array_Size(N->M) = ushort.MaxValue";
132 Console.WriteLine(strDescription + " Starts!");
134 ushort ushort_Array_Size = 20;
135 ushort[] arrUshort = Helper.InitArray<ushort>(ushort_Array_Size);
137 int expected_UshortArraySize = ushort.MaxValue;
138 Assert.IsTrue(MarshalCStyleArrayUshort_AsByRef_AsSizeParamIndex(ref arrUshort, ref ushort_Array_Size));
141 ushort[] expectedArrShort = Helper.GetExpChangeArray<ushort>(expected_UshortArraySize);
142 Assert.IsTrue(Helper.EqualArray<ushort>(arrUshort, (int)ushort_Array_Size, expectedArrShort, expectedArrShort.Length));
144 Console.WriteLine(strDescription + " Ends!");
147 static void SizeParamTypeIsInt32()
149 string strDescription = "Scenario(Int32==>LONG):Array_Size(M->N)=10, Array_Size(N->M)=1";
151 Console.WriteLine(strDescription + " Starts!");
153 Int32 Int32_Array_Size = (Int32)10;
154 Int32[] arrInt32 = Helper.InitArray<Int32>(Int32_Array_Size);
156 Assert.IsTrue(MarshalCStyleArrayInt_AsByRef_AsSizeParamIndex(ref Int32_Array_Size, Int32.MaxValue, ref arrInt32));
159 int expected_UshortArraySize = 1;
160 Int32[] expectedArrInt32 = Helper.GetExpChangeArray<Int32>(expected_UshortArraySize);
161 Assert.IsTrue(Helper.EqualArray<Int32>(arrInt32, Int32_Array_Size, expectedArrInt32, expectedArrInt32.Length));
163 Console.WriteLine(strDescription + " Ends!");
166 static void SizeParamTypeIsUInt32()
168 string strDescription = "Scenario(UInt32==>ULONG):Array_Size(M->N)=1234,Array_Size(N->M)=4321";
170 Console.WriteLine(strDescription + " Starts!");
172 UInt32 UInt32_Array_Size = (UInt32)1234;
173 UInt32[] arrUInt32 = Helper.InitArray<UInt32>((Int32)UInt32_Array_Size);
174 Assert.IsTrue(MarshalCStyleArrayUInt_AsByRef_AsSizeParamIndex(ref arrUInt32, 1234, ref UInt32_Array_Size));
177 int expected_UInt32ArraySize = 4321;
178 UInt32[] expectedArrUInt32 = Helper.GetExpChangeArray<UInt32>(expected_UInt32ArraySize);
179 Assert.IsTrue(Helper.EqualArray<UInt32>(arrUInt32, (Int32)UInt32_Array_Size, expectedArrUInt32, (Int32)expectedArrUInt32.Length));
181 Console.WriteLine(strDescription + " Ends!");
184 static void SizeParamTypeIsLong()
186 string strDescription = "Scenario(long==>LONGLONG):Array_Size(M->N)=10,Array_Size(N->M)=20";
188 Console.WriteLine(strDescription + " Starts!");
190 long long_Array_Size = (long)10;
191 long[] arrLong = Helper.InitArray<long>((Int32)long_Array_Size);
192 Assert.IsTrue(MarshalCStyleArrayLong_AsByRef_AsSizeParamIndex(ref long_Array_Size, ref arrLong));
194 //Construct Expected Array
195 int expected_LongArraySize = 20;
196 long[] expectedArrLong = Helper.GetExpChangeArray<long>(expected_LongArraySize);
197 Assert.IsTrue(Helper.EqualArray<long>(arrLong, (Int32)long_Array_Size, expectedArrLong, (Int32)expectedArrLong.Length));
199 Console.WriteLine(strDescription + " Ends!");
202 static void SizeParamTypeIsULong()
204 string strDescription = "Scenario(ulong==>ULONGLONG):Array_Size(M->N)=0, Array_Size(N->M)=0";
206 Console.WriteLine(strDescription + " Starts!");
208 ulong ulong_Array_Size = (ulong)0;
209 ulong[] arrUlong = Helper.InitArray<ulong>((Int32)ulong_Array_Size);
211 Assert.IsTrue(MarshalCStyleArrayUlong_AsByRef_AsSizeParamIndex(ref ulong_Array_Size, ref arrUlong));
214 int expected_ULongArraySize = 0;
215 ulong[] expectedArrUlong = Helper.GetExpChangeArray<ulong>(expected_ULongArraySize);
216 Assert.IsTrue(Helper.EqualArray<ulong>(arrUlong, (Int32)ulong_Array_Size, expectedArrUlong, (Int32)expectedArrUlong.Length));
218 Console.WriteLine(strDescription + " Ends!");
221 static void SizeParamTypeIsString()
223 string strDescription = "Scenario(String==>BSTR):Array_Size(M->N)= 20, Array_Size(N->M)=10";
225 Console.WriteLine(strDescription + " Starts!");
228 String[] arrString = Helper.InitArray<String>(array_Size);
229 String[] arrString2 = Helper.InitArray<String>(array_Size);
231 Assert.IsTrue(MarshalCStyleArrayString_AsByRef_AsSizeParamIndex(ref array_Size, ref arrString, ref arrString2));
234 int expected_StringArraySize = 10;
235 String[] expArrString = Helper.GetExpChangeArray<String>(expected_StringArraySize);
236 Assert.IsTrue(Helper.EqualArray<String>(arrString, array_Size, expArrString, expArrString.Length));
238 Console.WriteLine(strDescription + " Ends!");
244 SizeParamTypeIsByte();
245 SizeParamTypeIsSByte();
246 SizeParamTypeIsShort1();
247 SizeParamTypeIsShort2();
248 SizeParamTypeIsUShort();
249 SizeParamTypeIsInt32();
250 SizeParamTypeIsUInt32();
251 SizeParamTypeIsLong();
252 SizeParamTypeIsULong();
253 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
255 SizeParamTypeIsString();
261 Console.WriteLine($"Test Failure: {e}");