|
2 | 2 | using System.Runtime.InteropServices; |
3 | 3 | using System.Security; |
4 | 4 | using ReflectionBridge.Extensions; |
| 5 | +using System.Linq; |
5 | 6 |
|
6 | 7 | #if (UCS4) |
7 | 8 | using System.Text; |
@@ -703,10 +704,38 @@ internal unsafe static extern IntPtr |
703 | 704 | PyGILState_GetThisThreadState(); |
704 | 705 |
|
705 | 706 | #if (PYTHON32 || PYTHON33 || PYTHON34 || PYTHON35) |
706 | | - [DllImport(Runtime.dll, CallingConvention=CallingConvention.Cdecl, |
707 | | - ExactSpelling=true, CharSet=CharSet.Ansi)] |
708 | | - public unsafe static extern int |
709 | | - Py_Main(int argc, [MarshalAsAttribute(UnmanagedType.LPArray, ArraySubType=UnmanagedType.LPWStr)] string[] argv); |
| 707 | + [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, |
| 708 | + ExactSpelling = true, CharSet = CharSet.Ansi)] |
| 709 | + private unsafe static extern int Py_Main(int argc, [MarshalAsAttribute(UnmanagedType.SysUInt)] IntPtr lplpargv); |
| 710 | + |
| 711 | + public unsafe static int Py_Main(int argc, string[] argv) |
| 712 | + { |
| 713 | + // Totally ignoring argc. |
| 714 | + argc = argv.Length; |
| 715 | + |
| 716 | + int allStringsLength = argv.Sum(x => x.Length + 1); |
| 717 | + int requiredSize = IntPtr.Size * argc + allStringsLength * UCS; |
| 718 | + IntPtr mem = Marshal.AllocHGlobal(requiredSize); |
| 719 | + try |
| 720 | + { |
| 721 | + // Preparing array of pointers to UTF32 strings. |
| 722 | + IntPtr curStrPtr = mem + argc * IntPtr.Size; |
| 723 | + for (int i = 0; i < argv.Length; i++) |
| 724 | + { |
| 725 | + Encoding enc = UCS == 2 ? Encoding.Unicode : Encoding.UTF32; |
| 726 | + byte[] zstr = enc.GetBytes(argv[i] + "\0"); |
| 727 | + Marshal.Copy(zstr, 0, curStrPtr, zstr.Length); |
| 728 | + Marshal.WriteIntPtr(mem + IntPtr.Size * i, curStrPtr); |
| 729 | + curStrPtr += zstr.Length; |
| 730 | + } |
| 731 | + return Py_Main(argc, mem); |
| 732 | + } |
| 733 | + finally |
| 734 | + { |
| 735 | + Marshal.FreeHGlobal(mem); |
| 736 | + } |
| 737 | + } |
| 738 | + |
710 | 739 | #else |
711 | 740 | [DllImport(Runtime.dll, CallingConvention = CallingConvention.Cdecl, |
712 | 741 | ExactSpelling = true, CharSet = CharSet.Ansi)] |
|
0 commit comments