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;
8 class ExactSpellingTest
12 [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = true)]
13 public static extern int Marshal_Int_InOut([In, Out] int intValue);
15 [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = true)]
16 public static extern int MarshalPointer_Int_InOut([In, Out] ref int intValue);
18 [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = false)]
19 public static extern int Marshal_Int_InOut2([In, Out] int intValue);
21 [DllImport("ExactSpellingNative", CharSet = CharSet.Ansi, ExactSpelling = false)]
22 public static extern int MarshalPointer_Int_InOut2([In, Out] ref int intValue);
27 [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = true)]
28 public static extern int Marshal_Int_InOut([In, Out] int intValue);
30 [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = true)]
31 public static extern int MarshalPointer_Int_InOut([In, Out] ref int intValue);
33 [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = false)]
34 public static extern int Marshal_Int_InOut2([In, Out] int intValue);
36 [DllImport("ExactSpellingNative", CharSet = CharSet.Unicode, ExactSpelling = false)]
37 public static extern int MarshalPointer_Int_InOut2([In, Out] ref int intValue);
42 [DllImport("ExactSpellingNative", CharSet = CharSet.Auto, ExactSpelling = false)]
43 public static extern int Marshal_Int_InOut2([In, Out] int intValue);
46 public static int Main(string[] args)
49 int intManaged = 1000;
53 Console.WriteLine("Method Unicode.Marshal_Int_InOut: ExactSpelling = true");
54 int int1 = intManaged;
55 int intRet1 = Unicode.Marshal_Int_InOut(int1);
56 failures += Verify(intReturn, intManaged, intRet1, int1);
58 Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut: ExactSpelling = true");
59 int int2 = intManaged;
60 int intRet2 = Unicode.MarshalPointer_Int_InOut(ref int2);
62 failures += Verify(intReturn, intNative, intRet2, int2);
64 Console.WriteLine("Method Ansi.Marshal_Int_InOut: ExactSpelling = true");
65 int int3 = intManaged;
66 int intRet3 = Ansi.Marshal_Int_InOut(int3);
67 failures += Verify(intReturn, intManaged, intRet3, int3);
69 Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut: ExactSpelling = true");
70 int int4 = intManaged;
71 int intRet4 = Ansi.MarshalPointer_Int_InOut(ref int4);
72 failures += Verify(intReturn, intNative, intRet4, int4);
74 int intReturnAnsi = 4000;
75 int intReturnUnicode = 5000;
77 Console.WriteLine("Method Unicode.Marshal_Int_InOut2: ExactSpelling = false");
78 int int5 = intManaged;
79 int intRet5 = Unicode.Marshal_Int_InOut2(int5);
80 failures += Verify(intReturnUnicode, intManaged, intRet5, int5);
82 Console.WriteLine("Method Unicode.MarshalPointer_Int_InOut2: ExactSpelling = false");
83 int int6 = intManaged;
84 int intRet6 = Unicode.MarshalPointer_Int_InOut2(ref int6);
85 failures += Verify(intReturnUnicode, intNative, intRet6, int6);
87 Console.WriteLine("Method Ansi.Marshal_Int_InOut2: ExactSpelling = false");
88 int int7 = intManaged;
89 int intRet7 = Ansi.Marshal_Int_InOut2(int7);
90 failures += Verify(intReturnAnsi, intManaged, intRet7, int7);
92 Console.WriteLine("Method Ansi.MarshalPointer_Int_InOut2: ExactSpelling = false");
93 int int8 = intManaged;
94 int intRet8 = Ansi.MarshalPointer_Int_InOut2(ref int8);
95 failures += Verify(intReturnAnsi, intNative, intRet8, int8);
97 Console.WriteLine("Method Auto.Marshal_Int_InOut: ExactSpelling = false. Verify CharSet.Auto behavior per-platform.");
98 int int9 = intManaged;
99 int intRet9 = Auto.Marshal_Int_InOut2(int9);
101 failures += Verify(intReturnUnicode, intManaged, intRet9, int9);
103 failures += Verify(intReturnAnsi, intManaged, intRet9, int9);
106 return 100 + failures;
109 private static int Verify(int expectedReturnValue, int expectedParameterValue, int actualReturnValue, int actualParameterValue)
112 if (expectedReturnValue != actualReturnValue)
115 Console.WriteLine($"The return value is wrong. Expected {expectedReturnValue}, got {actualReturnValue}");
117 if (expectedParameterValue != actualParameterValue)
120 Console.WriteLine($"The parameter value is changed. Expected {expectedParameterValue}, got {actualParameterValue}");