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 Array Size by out keyword using SizeParamIndex Attributes
12 public class ClientMarshalArrayAsSizeParamIndexByOutTest
17 [DllImport("PInvokePassingByOutNative")]
18 private static extern bool MarshalCStyleArrayByte_AsByOut_AsSizeParamIndex(
19 out byte arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out byte[] arrByte);
21 [DllImport("PInvokePassingByOutNative")]
22 private static extern bool MarshalCStyleArraySbyte_AsByOut_AsSizeParamIndex(
23 out sbyte arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out sbyte[] arrSbyte);
25 [DllImport("PInvokePassingByOutNative")]
26 private static extern bool MarshalCStyleArrayShort_AsByOut_AsSizeParamIndex(
27 out short arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out short[] arrShort);
29 [DllImport("PInvokePassingByOutNative")]
30 private static extern bool MarshalCStyleArrayShortReturnNegative_AsByOut_AsSizeParamIndex(
31 out short arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out short[] arrShort);
33 [DllImport("PInvokePassingByOutNative")]
34 private static extern bool MarshalCStyleArrayUshort_AsByOut_AsSizeParamIndex(
35 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out ushort[] arrUshort, out ushort arrSize);
37 [DllImport("PInvokePassingByOutNative")]
38 private static extern bool MarshalCStyleArrayInt_AsByOut_AsSizeParamIndex(
39 out Int32 arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out Int32[] arrInt32);
41 [DllImport("PInvokePassingByOutNative")]
42 private static extern bool MarshalCStyleArrayUInt_AsByOut_AsSizeParamIndex(
43 out UInt32 arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out UInt32[] arrUInt32);
45 [DllImport("PInvokePassingByOutNative")]
46 private static extern bool MarshalCStyleArrayLong_AsByOut_AsSizeParamIndex(
47 out long arrSize, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)] out long[] arrLong);
49 [DllImport("PInvokePassingByOutNative")]
50 private static extern bool MarshalCStyleArrayUlong_AsByOut_AsSizeParamIndex(
51 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)] out ulong[] arrUlong, out ulong arrSize, ulong unused);
53 [DllImport("PInvokePassingByOutNative")]
54 private static extern bool MarshalCStyleArrayString_AsByOut_AsSizeParamIndex(
55 [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1, ArraySubType = UnmanagedType.BStr)] out string[] arrInt32, out int arrSize);
59 static void SizeParamTypeIsByte()
61 string strDescription = "Scenario(byte ==> BYTE): Array_Size(N->M) = 1";
63 Console.WriteLine(strDescription + " Starts!");
67 Assert.IsTrue(MarshalCStyleArrayByte_AsByOut_AsSizeParamIndex(out byte_Array_Size, out arrByte));
69 //Construct Expected array
70 int expected_ByteArray_Size = 1;
71 byte[] expectedArrByte = Helper.GetExpChangeArray<byte>(expected_ByteArray_Size);
72 Assert.IsTrue(Helper.EqualArray<byte>(arrByte, (int)byte_Array_Size, expectedArrByte, (int)expectedArrByte.Length));
74 Console.WriteLine(strDescription + " Ends!");
77 static void SizeParamTypeIsSByte()
79 string strDescription = "Scenario(sbyte ==> CHAR):Array_Size(N->M) = sbyte.Max";
81 Console.WriteLine(strDescription + " Starts!");
83 sbyte sbyte_Array_Size;
85 Assert.IsTrue(MarshalCStyleArraySbyte_AsByOut_AsSizeParamIndex(out sbyte_Array_Size, out arrSbyte));
87 sbyte[] expectedArrSbyte = Helper.GetExpChangeArray<sbyte>(sbyte.MaxValue);
88 Assert.IsTrue(Helper.EqualArray<sbyte>(arrSbyte, (int)sbyte_Array_Size, expectedArrSbyte, (int)expectedArrSbyte.Length));
90 Console.WriteLine(strDescription + " Ends!");
93 static void SizeParamTypeIsShort1()
95 string strDescription = "Scenario(short ==> SHORT)1,Array_Size(M->N) = -1, Array_Size(N->M)=(ShortMax+1)/2";
97 Console.WriteLine(strDescription + " Starts!");
99 short shortArray_Size = (short)-1;
100 short[] arrShort = Helper.InitArray<short>(10);
101 Assert.IsTrue(MarshalCStyleArrayShort_AsByOut_AsSizeParamIndex(out shortArray_Size, out arrShort));
103 //Construct Expected Array
104 int expected_ShortArray_Size = 16384;//(SHRT_MAX+1)/2
105 short[] expectedArrShort = Helper.GetExpChangeArray<short>(expected_ShortArray_Size);
106 Assert.IsTrue(Helper.EqualArray<short>(arrShort, (int)shortArray_Size, expectedArrShort, (int)expectedArrShort.Length));
108 Console.WriteLine(strDescription + " Ends!");
111 static void SizeParamTypeIsShort2()
113 string strDescription = "Scenario(short ==> SHORT)2, Array_Size = 10, Array_Size(N->M) = -1";
115 Console.WriteLine(strDescription + " Starts!");
117 short short_Array_Size = (short)10;
118 short[] arrShort = Helper.InitArray<short>(short_Array_Size);
119 Assert.Throws<OverflowException>(() => MarshalCStyleArrayShortReturnNegative_AsByOut_AsSizeParamIndex(out short_Array_Size, out arrShort));
120 Console.WriteLine(strDescription + " Ends!");
123 static void SizeParamTypeIsUShort()
125 string strDescription = "Scenario(ushort==>USHORT): Array_Size(N->M) = ushort.MaxValue";
127 Console.WriteLine(strDescription + " Starts!");
129 ushort ushort_Array_Size;
131 Assert.IsTrue(MarshalCStyleArrayUshort_AsByOut_AsSizeParamIndex(out arrUshort, out ushort_Array_Size));
134 ushort[] expectedArrUshort = Helper.GetExpChangeArray<ushort>(ushort.MaxValue);
135 Assert.IsTrue(Helper.EqualArray<ushort>(arrUshort, (int)ushort_Array_Size, expectedArrUshort, (ushort)expectedArrUshort.Length));
137 Console.WriteLine(strDescription + " Ends!");
140 static void SizeParamTypeIsInt32()
142 string strDescription = "Scenario(Int32 ==> LONG): Array_Size(N->M) = 0 ";
145 Console.WriteLine(strDescription + " Starts!");
147 Int32 Int32_Array_Size;
149 Assert.IsTrue(MarshalCStyleArrayInt_AsByOut_AsSizeParamIndex(out Int32_Array_Size, out arrInt32));
152 Int32[] expectedArrInt32 = Helper.GetExpChangeArray<Int32>(0);
153 Assert.IsTrue(Helper.EqualArray<Int32>(arrInt32, Int32_Array_Size, expectedArrInt32, expectedArrInt32.Length));
155 Console.WriteLine(strDescription + " Ends!");
158 static void SizeParamTypeIsUInt32()
160 string strDescription = "Scenario(UInt32 ==> ULONG): Array_Size(N->M) = 20";
163 Console.WriteLine(strDescription + " Starts!");
165 int expected_UInt32ArraySize = 20;
167 UInt32 UInt32_Array_Size = (UInt32)10;
168 UInt32[] arrUInt32 = Helper.InitArray<UInt32>((Int32)UInt32_Array_Size);
169 Assert.IsTrue(MarshalCStyleArrayUInt_AsByOut_AsSizeParamIndex(out UInt32_Array_Size, out arrUInt32));
172 UInt32[] expectedArrUInt32 = Helper.GetExpChangeArray<UInt32>(expected_UInt32ArraySize);
173 Assert.IsTrue(Helper.EqualArray<UInt32>(arrUInt32, (Int32)UInt32_Array_Size, expectedArrUInt32, (Int32)expectedArrUInt32.Length));
175 Console.WriteLine(strDescription + " Ends!");
178 static void SizeParamTypeIsLong()
180 string strDescription = "Scenario(long ==> LONGLONG): Array_Size(N->M) = 20";
183 Console.WriteLine(strDescription + " Starts!");
185 int expected_LongArraySize = 20;
187 long long_Array_Size = (long)10;
188 long[] arrLong = Helper.InitArray<long>((Int32)long_Array_Size);
189 Assert.IsTrue(MarshalCStyleArrayLong_AsByOut_AsSizeParamIndex(out long_Array_Size, out arrLong));
191 long[] expectedArrLong = Helper.GetExpChangeArray<long>(expected_LongArraySize);
192 Assert.IsTrue(Helper.EqualArray<long>(arrLong, (Int32)long_Array_Size, expectedArrLong, (Int32)expectedArrLong.Length));
194 Console.WriteLine(strDescription + " Ends!");
197 static void SizeParamTypeIsULong()
199 string strDescription = "Scenario(ulong ==> ULONGLONG): Array_Size(N->M) = 1000";
202 Console.WriteLine(strDescription + " Starts!");
204 int expected_ULongArraySize = 1000;
206 ulong ulong_Array_Size = (ulong)10;
207 ulong[] arrUlong = Helper.InitArray<ulong>((Int32)ulong_Array_Size);
208 Assert.IsTrue(MarshalCStyleArrayUlong_AsByOut_AsSizeParamIndex(out arrUlong, out ulong_Array_Size, ulong_Array_Size));
210 ulong[] expectedArrUlong = Helper.GetExpChangeArray<ulong>(expected_ULongArraySize);
211 Assert.IsTrue(Helper.EqualArray<ulong>(arrUlong, (Int32)ulong_Array_Size, expectedArrUlong, (Int32)expectedArrUlong.Length));
213 Console.WriteLine(strDescription + " Ends!");
216 static void SizeParamTypeIsString()
218 string strDescription = "Scenario(String ==> BSTR): Array_Size(N->M) = 20";
221 Console.WriteLine(strDescription + " Starts!");
223 int expected_StringArraySize = 20;
224 int string_Array_Size = 10;
225 String[] arrString = Helper.InitArray<String>(string_Array_Size);
226 Assert.IsTrue(MarshalCStyleArrayString_AsByOut_AsSizeParamIndex(out arrString, out string_Array_Size));
228 String[] expArrString = Helper.GetExpChangeArray<String>(expected_StringArraySize);
229 Assert.IsTrue(Helper.EqualArray<String>(arrString, string_Array_Size, expArrString, expArrString.Length));
230 Console.WriteLine(strDescription + " Ends!");
236 SizeParamTypeIsByte();
237 SizeParamTypeIsSByte();
238 SizeParamTypeIsShort1();
239 SizeParamTypeIsShort2();
240 SizeParamTypeIsUShort();
241 SizeParamTypeIsInt32();
242 SizeParamTypeIsUInt32();
243 SizeParamTypeIsLong();
244 SizeParamTypeIsULong();
245 if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
247 SizeParamTypeIsString();
253 Console.WriteLine($"Test Failure: {e}");