-
Notifications
You must be signed in to change notification settings - Fork 780
Expand file tree
/
Copy pathCustomMarshaler.cs
More file actions
171 lines (148 loc) · 4.83 KB
/
Copy pathCustomMarshaler.cs
File metadata and controls
171 lines (148 loc) · 4.83 KB
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
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace Python.Runtime
{
/// <summary>
/// Abstract class defining boiler plate methods that
/// Custom Marshalers will use.
/// </summary>
internal abstract class MarshalerBase : ICustomMarshaler
{
public object MarshalNativeToManaged(IntPtr pNativeData)
{
throw new NotImplementedException();
}
public abstract IntPtr MarshalManagedToNative(object managedObj);
public void CleanUpNativeData(IntPtr pNativeData)
{
Marshal.FreeHGlobal(pNativeData);
}
public void CleanUpManagedData(object managedObj)
{
// Let GC deal with it
}
public int GetNativeDataSize()
{
return IntPtr.Size;
}
}
/// <summary>
/// Custom Marshaler to deal with Managed String to Native
/// conversion differences on UCS2/UCS4.
/// </summary>
internal class UcsMarshaler : MarshalerBase
{
internal static readonly int _UCS = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? 2 : 4;
internal static readonly Encoding PyEncoding = _UCS == 2 ? Encodings.UTF16 : Encodings.UTF32;
private static readonly MarshalerBase Instance = new UcsMarshaler();
public override IntPtr MarshalManagedToNative(object managedObj)
{
if (managedObj is not string s)
{
return IntPtr.Zero;
}
byte[] bStr = PyEncoding.GetBytes(s + "\0");
IntPtr mem = Marshal.AllocHGlobal(bStr.Length);
try
{
Marshal.Copy(bStr, 0, mem, bStr.Length);
}
catch (Exception)
{
Marshal.FreeHGlobal(mem);
throw;
}
return mem;
}
public static ICustomMarshaler GetInstance(string cookie)
{
return Instance;
}
public static string? PtrToString(IntPtr p)
{
if (p == IntPtr.Zero)
{
return null;
}
int size = GetUnicodeByteLength(p);
var buffer = new byte[size];
Marshal.Copy(p, buffer, 0, size);
return PyEncoding.GetString(buffer, 0, size);
}
public static int GetUnicodeByteLength(IntPtr p)
{
var len = 0;
while (true)
{
int c = _UCS == 2
? Marshal.ReadInt16(p, len * 2)
: Marshal.ReadInt32(p, len * 4);
if (c == 0)
{
return len * _UCS;
}
checked
{
++len;
}
}
}
/// <summary>
/// Utility function for Marshaling Unicode
/// </summary>
/// <param name="s">Managed String</param>
/// <returns>
/// Ptr to Native String
/// </returns>
/// <remarks>
/// You MUST deallocate the IntPtr of the Return when done with it.
/// </remarks>
public static IntPtr StringToPtr(string s)
{
return Instance.MarshalManagedToNative(s);
}
}
/// <summary>
/// Custom Marshaler to deal with Managed String Arrays to Native
/// conversion differences on UCS2/UCS4.
/// </summary>
internal class StrArrayMarshaler : MarshalerBase
{
private static readonly MarshalerBase Instance = new StrArrayMarshaler();
private static readonly Encoding PyEncoding = UcsMarshaler.PyEncoding;
public override IntPtr MarshalManagedToNative(object managedObj)
{
if (managedObj is not string[] argv)
{
return IntPtr.Zero;
}
int totalStrLength = argv.Sum(arg => arg.Length + 1);
int memSize = argv.Length * IntPtr.Size + totalStrLength * UcsMarshaler._UCS;
IntPtr mem = Marshal.AllocHGlobal(memSize);
try
{
// Preparing array of pointers to strings
IntPtr curStrPtr = mem + argv.Length * IntPtr.Size;
for (var i = 0; i < argv.Length; i++)
{
byte[] bStr = PyEncoding.GetBytes(argv[i] + "\0");
Marshal.Copy(bStr, 0, curStrPtr, bStr.Length);
Marshal.WriteIntPtr(mem + i * IntPtr.Size, curStrPtr);
curStrPtr += bStr.Length;
}
}
catch (Exception)
{
Marshal.FreeHGlobal(mem);
throw;
}
return mem;
}
public static ICustomMarshaler GetInstance(string? cookie)
{
return Instance;
}
}
}