summaryrefslogtreecommitdiff
path: root/tests/src/Interop/PInvoke/Decimal/ReversePInvoke/DecimalTest.cs
blob: d63b455f372abef2b489fc7b708f421670b4af02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;

#pragma warning disable 618
[StructLayout(LayoutKind.Sequential)]
public struct Stru_Seq_DecAsStructAsFld
{
    public int number;

    [MarshalAs(UnmanagedType.Struct)]
    public decimal dec;
}

[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Unicode)]
public struct Stru_Exp_DecAsCYAsFld
{
    [FieldOffset(0)]
    public char wc;

    [FieldOffset(8)]
    [MarshalAs(UnmanagedType.Currency)]
    public decimal dec;
}

public class CMain
{
    #region Func Sig   

    // Dec As Struct
    [DllImport("RevNative")]
    static extern bool ReverseCall_TakeDecByInOutRef([MarshalAs(UnmanagedType.FunctionPtr)] Dele_DecInOutRef dele);
    [DllImport("RevNative")]
    static extern bool ReverseCall_TakeDecByOutRef([MarshalAs(UnmanagedType.FunctionPtr)] Dele_DecOutRef dele);
    [DllImport("RevNative")]
    static extern bool ReverseCall_DecRet([MarshalAs(UnmanagedType.FunctionPtr)] Dele_DecRet dele);
    [DllImport("RevNative")]
    static extern bool ReverseCall_TakeStru_Seq_DecAsStructAsFldByInOutRef([MarshalAs(UnmanagedType.FunctionPtr)] Dele_Stru_Seq_DecAsStructAsFldInOutRef dele);

    // Dec As CY
    [DllImport("RevNative")]
    static extern bool ReverseCall_TakeCYByInOutRef([MarshalAs(UnmanagedType.FunctionPtr)] Dele_CYInOutRef dele);
    [DllImport("RevNative")]
    static extern bool ReverseCall_TakeCYByOutRef([MarshalAs(UnmanagedType.FunctionPtr)] Dele_CYOutRef dele);
    [DllImport("RevNative")]
    static extern bool ReverseCall_CYRet([MarshalAs(UnmanagedType.FunctionPtr)] Dele_CYRet dele);
    [DllImport("RevNative")]
    static extern bool ReverseCall_TakeStru_Exp_DecAsCYAsFldByOutRef([MarshalAs(UnmanagedType.FunctionPtr)] Dele_Stru_Exp_DecAsCYAsFldOutRef dele);

    // Dec As LPStruct
    [DllImport("RevNative")]
    static extern bool ReverseCall_TakeDecByInOutRefAsLPStruct([MarshalAs(UnmanagedType.FunctionPtr)] Dele_DecInOutRefAsLPStruct dele);
    [DllImport("RevNative")]
    static extern bool ReverseCall_TakeDecByOutRefAsLPStruct([MarshalAs(UnmanagedType.FunctionPtr)] Dele_DecOutRefAsLPStruct dele);
    //************** ReverseCall Return Int From Net **************//
    [DllImport("RevNative")]
    static extern bool ReverseCall_IntRet([MarshalAs(UnmanagedType.FunctionPtr)] Dele_IntRet dele);

    #endregion

    #region Delegate Set

    // Dec As Struct
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool Dele_DecInOutRef([MarshalAs(UnmanagedType.Struct), In, Out]ref decimal dec);
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool Dele_DecOutRef([MarshalAs(UnmanagedType.Struct), Out]out decimal dec);
    [return: MarshalAs(UnmanagedType.Struct)]
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate decimal Dele_DecRet();
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool Dele_Stru_Seq_DecAsStructAsFldInOutRef([In, Out]ref Stru_Seq_DecAsStructAsFld s);

    // Dec As CY
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool Dele_CYInOutRef([MarshalAs(UnmanagedType.Currency), In, Out]ref decimal dec);
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool Dele_CYOutRef([MarshalAs(UnmanagedType.Currency), Out]out decimal dec);
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.Currency)]
    delegate decimal Dele_CYRet();
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool Dele_Stru_Exp_DecAsCYAsFldOutRef([Out]out Stru_Exp_DecAsCYAsFld s);

    // Dec As LPStruct
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool Dele_DecInOutRefAsLPStruct([MarshalAs(UnmanagedType.LPStruct), In, Out]ref decimal dec);
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate bool Dele_DecOutRefAsLPStruct([MarshalAs(UnmanagedType.LPStruct), Out]out decimal dec);

    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    [return: MarshalAs(UnmanagedType.LPStruct)]
    delegate decimal Dele_DecAsLPStructRet();

    //************** ReverseCall Return Int From Net **************//
    [UnmanagedFunctionPointer(CallingConvention.Winapi)]
    delegate int Dele_IntRet();

    #endregion

    #region AUX For Testing
    const decimal CY_MAX_VALUE = 922337203685477.5807M;
    const decimal CY_MIN_VALUE = -922337203685477.5808M;

    static bool Equals<T>(T expected, T actual)
    {
        if (expected.Equals(actual))
            return true;
        else
            return false;
    }

    #endregion

    #region Method For Testing

    //Dec As Struct
    static bool TakeDecByInOutRef([MarshalAs(UnmanagedType.Struct), In, Out] ref decimal dec)
    {
        if (Equals(decimal.MaxValue, dec))
        {
            dec = decimal.MinValue;
            return true;
        }
        else
            return false;
    }

    static bool TakeDecByOutRef([MarshalAs(UnmanagedType.Struct), Out] out decimal dec)
    {
        dec = decimal.Zero;

        return true;
    }

    [return: MarshalAs(UnmanagedType.Struct)]
    static decimal DecRet()
    {
        return decimal.MinValue;
    }

    static bool TakeStru_Seq_DecAsStructAsFldByInOutRef([In, Out] ref Stru_Seq_DecAsStructAsFld s)
    {
        if (Equals(decimal.MaxValue, s.dec) && Equals(1, s.number))
        {
            s.dec = decimal.MinValue;
            s.number = 2;
            return true;
        }
        else
            return false;
    }

    // Dec As CY
    static bool TakeCYByInOutRef([MarshalAs(UnmanagedType.Currency), In, Out] ref decimal cy)
    {
        if (Equals(CY_MAX_VALUE, cy))
        {
            cy = CY_MIN_VALUE;
            return true;
        }
        else
            return false;
    }

    [return: MarshalAs(UnmanagedType.Currency)]
    static decimal CYRet()
    {
        return CY_MIN_VALUE;
    }

    static bool TakeCYByOutRef([MarshalAs(UnmanagedType.Currency), Out] out decimal dec)
    {
        dec = decimal.Zero;

        return true;
    }

    static bool ReverseCall_TakeStru_Exp_DecAsCYAsFldByOutRef([Out] out Stru_Exp_DecAsCYAsFld s)
    {
        s.dec = CY_MAX_VALUE;
        s.wc = 'C';

        return true;
    }

    //Dec As LPStruct
    static bool TakeDecByInOutRefAsLPStruct([MarshalAs(UnmanagedType.LPStruct), In, Out] ref decimal dec)
    {
        if (Equals(decimal.MaxValue, dec))
        {
            dec = decimal.MinValue;
            return true;
        }
        else
            return false;
    }

    static bool TakeDecByOutRefAsLPStruct([MarshalAs(UnmanagedType.LPStruct), Out] out decimal dec)
    {
        dec = decimal.Zero;

        return true;
    }

    [return: MarshalAs(UnmanagedType.LPStruct)]
    static decimal DecAsLPStructRet()
    {
        return decimal.MinValue;
    }

    //************** ReverseCall Return Int From Net **************//
    [return: MarshalAs(UnmanagedType.I4)]
    static int IntRet()
    {
        return 0x12345678;
    }

    #endregion

    static bool AsStruct()
    {
        Console.WriteLine("AsStruct started.");
        // Dec As Struct
        if (!ReverseCall_TakeDecByInOutRef(new Dele_DecInOutRef(TakeDecByInOutRef)))
        {
            Console.WriteLine("Test Failed: Decimal <-> DECIMAL, Marshal As Struct/Param, Passed By In / Out / Ref .");
            return false;
        }
        if (!ReverseCall_TakeDecByOutRef(new Dele_DecOutRef(TakeDecByOutRef)))
        {
            Console.WriteLine("Test Failed: Decimal <-> DECIMAL, Marshal As Struct/Param, Passed By Out / Ref .");
            return false;
        }
        if (!ReverseCall_DecRet(new Dele_DecRet(DecRet)))
        {
            Console.WriteLine("Test Failed: Decimal <-> DECIMAL, Marshal As Struct/RetVal .");
            return false;
        }
        if (!ReverseCall_TakeStru_Seq_DecAsStructAsFldByInOutRef(new Dele_Stru_Seq_DecAsStructAsFldInOutRef(TakeStru_Seq_DecAsStructAsFldByInOutRef)))
        {
            Console.WriteLine("Test Failed: Decimal <-> DECIMAL, Marshal As Struct/Field, Passed By In / Out / Ref .");
            return false;
        }

        Console.WriteLine("AsStruct end.");
        return true;
    }

    static bool AsCY()
    {
        Console.WriteLine("AsCY started.");
        // Dec As CY
        if (!(ReverseCall_TakeCYByInOutRef(new Dele_CYInOutRef(TakeCYByInOutRef))))
        {
            Console.WriteLine("Test Failed: (ReverseCall_TakeCYByInOutRef(new Dele_CYInOutRef(TakeCYByInOutRef)))");
            return false;
        }
        if (!(ReverseCall_TakeCYByOutRef(new Dele_CYOutRef(TakeCYByOutRef))))
        {
            Console.WriteLine("Test Failed: (ReverseCall_TakeCYByOutRef(new Dele_CYOutRef(TakeCYByOutRef)))");
            return false;
        }

        bool exceptionThrown = false;

        try
        {
            ReverseCall_CYRet(new Dele_CYRet(CYRet));
        }
        catch (MarshalDirectiveException)
        {
            exceptionThrown = true;
        }
        if (!exceptionThrown)
        {
            Console.WriteLine("Expected MarshalDirectiveException from ReverseCall_CYRet(new Dele_CYRet(CYRet)) not thrown");
            return false;
        }

        if (!(ReverseCall_TakeStru_Exp_DecAsCYAsFldByOutRef(new Dele_Stru_Exp_DecAsCYAsFldOutRef(ReverseCall_TakeStru_Exp_DecAsCYAsFldByOutRef))))
        {
            Console.WriteLine("Test Failed: (ReverseCall_TakeStru_Exp_DecAsCYAsFldByOutRef(new Dele_Stru_Exp_DecAsCYAsFldOutRef(ReverseCall_TakeStru_Exp_DecAsCYAsFldByOutRef)))");
            return false;
        }

        Console.WriteLine("AsCY end.");
        return true;
    }

    static bool AsLPStruct()
    {
        Console.WriteLine("AsLPStruct started.");

        if (!ReverseCall_TakeDecByInOutRefAsLPStruct(new Dele_DecInOutRefAsLPStruct(TakeDecByInOutRefAsLPStruct)))
        {
            Console.WriteLine("Test Failed: Decimal <-> DECIMAL, Marshal As LPStruct/Param, Passed By In / Out / Ref");
            return false;
        }
        if (!ReverseCall_TakeDecByOutRefAsLPStruct(new Dele_DecOutRefAsLPStruct(TakeDecByOutRefAsLPStruct)))
        {
            Console.WriteLine("Test Failed: Decimal <-> DECIMAL, Marshal As LPStruct/Param, Passed By Out / Ref");
            return false;
        }


        Console.WriteLine("AsLPStruct end.");
        return true;
    }

    static bool AsInt()
    {
        if (!ReverseCall_IntRet(new Dele_IntRet(IntRet)))
        {
            Console.WriteLine("Test Failed: RET INT");
            return false;
        }

        return true;
    }

    static int Main()
    {
        bool success = true;
        success = success && AsStruct();
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
        {
            success = success && AsCY();
        }
        success = success && AsLPStruct();
        success = success && AsInt();

        return success ? 100 : 101;
    }
}
#pragma warning restore 618