|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Text; |
| 5 | + |
| 6 | +namespace Python.Runtime |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Abstract class defining boiler plate methods that |
| 10 | + /// Custom Marshalers will use. |
| 11 | + /// </summary> |
| 12 | + public abstract class MarshalerBase : ICustomMarshaler |
| 13 | + { |
| 14 | + public object MarshalNativeToManaged(IntPtr pNativeData) |
| 15 | + { |
| 16 | + throw new NotImplementedException(); |
| 17 | + } |
| 18 | + |
| 19 | + public abstract IntPtr MarshalManagedToNative(object managedObj); |
| 20 | + |
| 21 | + public void CleanUpNativeData(IntPtr pNativeData) |
| 22 | + { |
| 23 | + Marshal.FreeHGlobal(pNativeData); |
| 24 | + } |
| 25 | + |
| 26 | + public void CleanUpManagedData(object managedObj) |
| 27 | + { |
| 28 | + // Let GC deal with it |
| 29 | + } |
| 30 | + |
| 31 | + public int GetNativeDataSize() |
| 32 | + { |
| 33 | + return IntPtr.Size; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Custom Marshaler to deal with Managed String to Native |
| 40 | + /// conversion differences on UCS2/UCS4. |
| 41 | + /// </summary> |
| 42 | + public class StrMarshaler : MarshalerBase |
| 43 | + { |
| 44 | + private static readonly MarshalerBase Instance = new StrMarshaler(); |
| 45 | + private static readonly Encoding PyEncoding = Runtime.PyEncoding; |
| 46 | + |
| 47 | + public override IntPtr MarshalManagedToNative(object managedObj) |
| 48 | + { |
| 49 | + var s = managedObj as string; |
| 50 | + |
| 51 | + if (s == null) |
| 52 | + { |
| 53 | + return IntPtr.Zero; |
| 54 | + } |
| 55 | + |
| 56 | + byte[] bStr = PyEncoding.GetBytes(s + "\0"); |
| 57 | + IntPtr mem = Marshal.AllocHGlobal(bStr.Length); |
| 58 | + try |
| 59 | + { |
| 60 | + Marshal.Copy(bStr, 0, mem, bStr.Length); |
| 61 | + } |
| 62 | + catch (Exception) |
| 63 | + { |
| 64 | + Marshal.FreeHGlobal(mem); |
| 65 | + throw; |
| 66 | + } |
| 67 | + |
| 68 | + return mem; |
| 69 | + } |
| 70 | + |
| 71 | + public static ICustomMarshaler GetInstance(string cookie) |
| 72 | + { |
| 73 | + return Instance; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + /// <summary> |
| 79 | + /// Custom Marshaler to deal with Managed String Arrays to Native |
| 80 | + /// conversion differences on UCS2/UCS4. |
| 81 | + /// </summary> |
| 82 | + public class StrArrayMarshaler : MarshalerBase |
| 83 | + { |
| 84 | + private static readonly MarshalerBase Instance = new StrArrayMarshaler(); |
| 85 | + private static readonly Encoding PyEncoding = Runtime.PyEncoding; |
| 86 | + |
| 87 | + public override IntPtr MarshalManagedToNative(object managedObj) |
| 88 | + { |
| 89 | + var argv = managedObj as string[]; |
| 90 | + |
| 91 | + if (argv == null) |
| 92 | + { |
| 93 | + return IntPtr.Zero; |
| 94 | + } |
| 95 | + |
| 96 | + int totalStrLength = argv.Sum(arg => arg.Length + 1); |
| 97 | + int memSize = argv.Length * IntPtr.Size + totalStrLength * Runtime.UCS; |
| 98 | + |
| 99 | + IntPtr mem = Marshal.AllocHGlobal(memSize); |
| 100 | + try |
| 101 | + { |
| 102 | + // Preparing array of pointers to strings |
| 103 | + IntPtr curStrPtr = mem + argv.Length * IntPtr.Size; |
| 104 | + for (var i = 0; i < argv.Length; i++) |
| 105 | + { |
| 106 | + byte[] bStr = PyEncoding.GetBytes(argv[i] + "\0"); |
| 107 | + Marshal.Copy(bStr, 0, curStrPtr, bStr.Length); |
| 108 | + Marshal.WriteIntPtr(mem + i * IntPtr.Size, curStrPtr); |
| 109 | + curStrPtr += bStr.Length; |
| 110 | + } |
| 111 | + } |
| 112 | + catch (Exception) |
| 113 | + { |
| 114 | + Marshal.FreeHGlobal(mem); |
| 115 | + throw; |
| 116 | + } |
| 117 | + |
| 118 | + return mem; |
| 119 | + } |
| 120 | + |
| 121 | + public static ICustomMarshaler GetInstance(string cookie) |
| 122 | + { |
| 123 | + return Instance; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + |
| 128 | + /// <summary> |
| 129 | + /// Custom Marshaler to deal with Managed String to Native |
| 130 | + /// conversion on UTF-8. Use on functions that expect UTF-8 encoded |
| 131 | + /// strings like `PyUnicode_FromStringAndSize` |
| 132 | + /// </summary> |
| 133 | + /// <remarks> |
| 134 | + /// If instead we used `MarshalAs(UnmanagedType.LPWStr)` the output to |
| 135 | + /// `foo` would be `f\x00o\x00o\x00`. |
| 136 | + /// </remarks> |
| 137 | + public class Utf8Marshaler : MarshalerBase |
| 138 | + { |
| 139 | + private static readonly MarshalerBase Instance = new Utf8Marshaler(); |
| 140 | + private static readonly Encoding PyEncoding = Encoding.UTF8; |
| 141 | + |
| 142 | + public override IntPtr MarshalManagedToNative(object managedObj) |
| 143 | + { |
| 144 | + var s = managedObj as string; |
| 145 | + |
| 146 | + if (s == null) |
| 147 | + { |
| 148 | + return IntPtr.Zero; |
| 149 | + } |
| 150 | + |
| 151 | + byte[] bStr = PyEncoding.GetBytes(s + "\0"); |
| 152 | + IntPtr mem = Marshal.AllocHGlobal(bStr.Length); |
| 153 | + try |
| 154 | + { |
| 155 | + Marshal.Copy(bStr, 0, mem, bStr.Length); |
| 156 | + } |
| 157 | + catch (Exception) |
| 158 | + { |
| 159 | + Marshal.FreeHGlobal(mem); |
| 160 | + throw; |
| 161 | + } |
| 162 | + |
| 163 | + return mem; |
| 164 | + } |
| 165 | + |
| 166 | + public static ICustomMarshaler GetInstance(string cookie) |
| 167 | + { |
| 168 | + return Instance; |
| 169 | + } |
| 170 | + } |
| 171 | +} |
0 commit comments