@@ -34,119 +34,15 @@ public int GetNativeDataSize()
34
34
}
35
35
}
36
36
37
-
38
- /// <summary>
39
- /// Custom Marshaler to deal with Managed String to Native
40
- /// conversion differences on UCS2/UCS4.
41
- /// </summary>
42
- internal class UcsMarshaler : MarshalerBase
43
- {
44
- internal static readonly int _UCS = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? 2 : 4 ;
45
- internal static readonly Encoding PyEncoding = _UCS == 2 ? Encoding . Unicode : Encoding . UTF32 ;
46
- private static readonly MarshalerBase Instance = new UcsMarshaler ( ) ;
47
-
48
- public override IntPtr MarshalManagedToNative ( object managedObj )
49
- {
50
- if ( managedObj is not string s )
51
- {
52
- return IntPtr . Zero ;
53
- }
54
-
55
- byte [ ] bStr = PyEncoding . GetBytes ( s + "\0 " ) ;
56
- IntPtr mem = Marshal . AllocHGlobal ( bStr . Length ) ;
57
- try
58
- {
59
- Marshal . Copy ( bStr , 0 , mem , bStr . Length ) ;
60
- }
61
- catch ( Exception )
62
- {
63
- Marshal . FreeHGlobal ( mem ) ;
64
- throw ;
65
- }
66
-
67
- return mem ;
68
- }
69
-
70
- public static ICustomMarshaler GetInstance ( string cookie )
71
- {
72
- return Instance ;
73
- }
74
-
75
- public static string ? PtrToStringUni ( IntPtr p )
76
- {
77
- if ( p == IntPtr . Zero )
78
- {
79
- return null ;
80
- }
81
-
82
- int size = GetUnicodeByteLength ( p ) ;
83
- var buffer = new byte [ size ] ;
84
- Marshal . Copy ( p , buffer , 0 , size ) ;
85
- return PyEncoding . GetString ( buffer , 0 , size ) ;
86
- }
87
-
88
- public static int GetUnicodeByteLength ( IntPtr p )
89
- {
90
- var len = 0 ;
91
- while ( true )
92
- {
93
- int c = _UCS == 2
94
- ? Marshal . ReadInt16 ( p , len * 2 )
95
- : Marshal . ReadInt32 ( p , len * 4 ) ;
96
-
97
- if ( c == 0 )
98
- {
99
- return len * _UCS ;
100
- }
101
- checked
102
- {
103
- ++ len ;
104
- }
105
- }
106
- }
107
-
108
- /// <summary>
109
- /// Utility function for Marshaling Unicode on PY3 and AnsiStr on PY2.
110
- /// Use on functions whose Input signatures changed between PY2/PY3.
111
- /// Ex. Py_SetPythonHome
112
- /// </summary>
113
- /// <param name="s">Managed String</param>
114
- /// <returns>
115
- /// Ptr to Native String ANSI(PY2)/Unicode(PY3/UCS2)/UTF32(PY3/UCS4.
116
- /// </returns>
117
- /// <remarks>
118
- /// You MUST deallocate the IntPtr of the Return when done with it.
119
- /// </remarks>
120
- public static IntPtr Py3UnicodePy2StringtoPtr ( string s )
121
- {
122
- return Instance . MarshalManagedToNative ( s ) ;
123
- }
124
-
125
- /// <summary>
126
- /// Utility function for Marshaling Unicode IntPtr on PY3 and
127
- /// AnsiStr IntPtr on PY2 to Managed Strings. Use on Python functions
128
- /// whose return type changed between PY2/PY3.
129
- /// Ex. Py_GetPythonHome
130
- /// </summary>
131
- /// <param name="p">Native Ansi/Unicode/UTF32 String</param>
132
- /// <returns>
133
- /// Managed String
134
- /// </returns>
135
- public static string ? PtrToPy3UnicodePy2String ( IntPtr p )
136
- {
137
- return PtrToStringUni ( p ) ;
138
- }
139
- }
140
-
141
-
142
37
/// <summary>
143
38
/// Custom Marshaler to deal with Managed String Arrays to Native
144
39
/// conversion differences on UCS2/UCS4.
145
40
/// </summary>
146
41
internal class StrArrayMarshaler : MarshalerBase
147
42
{
148
43
private static readonly MarshalerBase Instance = new StrArrayMarshaler ( ) ;
149
- private static readonly Encoding PyEncoding = UcsMarshaler . PyEncoding ;
44
+ internal static readonly int _UCS = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? 2 : 4 ;
45
+ internal static readonly Encoding PyEncoding = _UCS == 2 ? Encoding . Unicode : Encoding . UTF32 ;
150
46
151
47
public override IntPtr MarshalManagedToNative ( object managedObj )
152
48
{
@@ -156,7 +52,7 @@ public override IntPtr MarshalManagedToNative(object managedObj)
156
52
}
157
53
158
54
int totalStrLength = argv . Sum ( arg => arg . Length + 1 ) ;
159
- int memSize = argv . Length * IntPtr . Size + totalStrLength * UcsMarshaler . _UCS ;
55
+ int memSize = argv . Length * IntPtr . Size + totalStrLength * _UCS ;
160
56
161
57
IntPtr mem = Marshal . AllocHGlobal ( memSize ) ;
162
58
try
0 commit comments