Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MarshalEx.cs « Ancillary.Interop « tests « System.Runtime.InteropServices « libraries « src - github.com/dotnet/runtime.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 716dcf28eece6433919123d38ba50f226852a3cd (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

using System.Diagnostics.CodeAnalysis;
using System.Reflection;

namespace System.Runtime.InteropServices
{
    /// <summary>
    /// Marshalling helper methods that will likely live in S.R.IS.Marshal
    /// when we integrate our APIs with dotnet/runtime.
    /// </summary>
    public static class MarshalEx
    {
        /// <summary>
        /// Sets the handle of <paramref name="safeHandle"/> to the specified <paramref name="handle"/>.
        /// </summary>
        /// <param name="safeHandle"><see cref="SafeHandle"/> instance to update</param>
        /// <param name="handle">Pre-existing handle</param>
        public static void InitHandle(SafeHandle safeHandle, IntPtr handle)
        {            
            typeof(SafeHandle).GetMethod("SetHandle", BindingFlags.NonPublic | BindingFlags.Instance)!.Invoke(safeHandle, new object[] { handle });
        }

        /// <summary>
        /// Set the last platform invoke error on the thread 
        /// </summary>
        public static void SetLastPInvokeError(int error)
        {
            MethodInfo? method = typeof(Marshal).GetMethod("SetLastWin32Error", BindingFlags.NonPublic | BindingFlags.Static);
            if (method == null)
            {
                method = typeof(Marshal).GetMethod("SetLastPInvokeError", BindingFlags.Public | BindingFlags.Static);
            }

            method!.Invoke(null, new object[] { error });
        }

        /// <summary>
        /// Get the last system error on the current thread (errno on Unix, GetLastError on Windows)
        /// </summary>
        public static unsafe int GetLastSystemError()
        {
            // Would be internal call that handles getting the last error for the thread using the PAL

            if (OperatingSystem.IsWindows())
            {
                return Kernel32.GetLastError();
            }
            else if (OperatingSystem.IsMacOS())
            {
                return *libc.__error();
            }
            else if (OperatingSystem.IsLinux())
            {
                return *libc.__errno_location();
            }

            throw new NotImplementedException();
        }

        /// <summary>
        /// Set the last system error on the current thread (errno on Unix, SetLastError on Windows)
        /// </summary>
        public static unsafe void SetLastSystemError(int error)
        {
            // Would be internal call that handles setting the last error for the thread using the PAL

            if (OperatingSystem.IsWindows())
            {
                Kernel32.SetLastError(error);
            }
            else if (OperatingSystem.IsMacOS())
            {
                *libc.__error() = error;
            }
            else if (OperatingSystem.IsLinux())
            {
                *libc.__errno_location() = error;
            }
            else
            {
                throw new NotImplementedException();
            }
        }

        private class Kernel32
        {
            [DllImport(nameof(Kernel32))]
            public static extern void SetLastError(int error);

            [DllImport(nameof(Kernel32))]
            public static extern int GetLastError();
        }

        private class libc
        {
            [DllImport(nameof(libc))]
            internal static unsafe extern int* __errno_location();

            [DllImport(nameof(libc))]
            internal static unsafe extern int* __error();
        }
    }
}