@@ -664,8 +664,41 @@ internal unsafe static extern IntPtr
664
664
#if PYTHON3
665
665
[ DllImport ( Runtime . dll , CallingConvention = CallingConvention . Cdecl ,
666
666
ExactSpelling = true , CharSet = CharSet . Ansi ) ]
667
- public unsafe static extern int
668
- Py_Main ( int argc , [ MarshalAsAttribute ( UnmanagedType . LPArray , ArraySubType = UnmanagedType . LPWStr ) ] string [ ] argv ) ;
667
+ private unsafe static extern int
668
+ Py_Main ( int argc , [ MarshalAsAttribute ( UnmanagedType . SysUInt ) ] IntPtr lplpargv ) ;
669
+
670
+ public static int Py_Main ( int argc , string [ ] argv )
671
+ {
672
+ // Totally ignoring argc.
673
+ argc = argv. Length;
674
+
675
+ var allStringsLength = 0 ;
676
+ foreach ( string x in argv)
677
+ {
678
+ allStringsLength += x . Length + 1 ;
679
+ }
680
+ int requiredSize = IntPtr . Size * argc + allStringsLength * UCS ;
681
+ IntPtr mem = Marshal . AllocHGlobal ( requiredSize ) ;
682
+ try
683
+ {
684
+ // Preparing array of pointers to UTF32 strings.
685
+ IntPtr curStrPtr = mem + argc * IntPtr. Size;
686
+ for ( var i = 0 ; i < argv . Length ; i++ )
687
+ {
688
+ // Unicode or UTF8 work
689
+ Encoding enc = UCS == 2 ? Encoding. Unicode : Encoding. UTF32;
690
+ byte [ ] zstr = enc. GetBytes( argv [ i ] + "\0 " ) ;
691
+ Marshal. Copy ( zstr , 0 , curStrPtr , zstr . Length ) ;
692
+ Marshal. WriteIntPtr ( mem + IntPtr . Size * i , curStrPtr ) ;
693
+ curStrPtr += zstr. Length ;
694
+ }
695
+ return Py_Main( argc , mem ) ;
696
+ }
697
+ finally
698
+ {
699
+ Marshal. FreeHGlobal ( mem ) ;
700
+ }
701
+ }
669
702
#elif PYTHON2
670
703
[ DllImport ( Runtime . dll , CallingConvention = CallingConvention . Cdecl ,
671
704
ExactSpelling = true , CharSet = CharSet . Ansi ) ]
@@ -2078,12 +2111,44 @@ internal unsafe static extern IntPtr
2078
2111
#if PYTHON3
2079
2112
[ DllImport ( Runtime . dll , CallingConvention = CallingConvention . Cdecl ,
2080
2113
ExactSpelling = true , CharSet = CharSet . Ansi ) ]
2081
- internal unsafe static extern void
2114
+ private unsafe static extern void
2082
2115
PySys_SetArgvEx (
2083
2116
int argc ,
2084
- [ MarshalAsAttribute ( UnmanagedType . LPArray , ArraySubType = UnmanagedType . LPWStr ) ] string [ ] argv ,
2117
+ [ MarshalAsAttribute ( UnmanagedType . SysUInt ) ] IntPtr lplpargv ,
2085
2118
int updatepath
2086
2119
) ;
2120
+
2121
+ internal static void PySys_SetArgvEx ( int argc , string [ ] argv , int updatepath )
2122
+ {
2123
+ // Totally ignoring argc.
2124
+ argc = argv. Length;
2125
+
2126
+ var allStringsLength = 0 ;
2127
+ foreach ( string x in argv)
2128
+ {
2129
+ allStringsLength += x. Length + 1 ;
2130
+ }
2131
+ int requiredSize = IntPtr. Size * argc + allStringsLength * UCS;
2132
+ IntPtr mem = Marshal. AllocHGlobal( requiredSize ) ;
2133
+ try
2134
+ {
2135
+ // Preparing array of pointers to UTF32 strings.
2136
+ IntPtr curStrPtr = mem + argc * IntPtr. Size;
2137
+ for ( var i = 0 ; i < argv. Length; i++ )
2138
+ {
2139
+ Encoding enc = UCS == 2 ? Encoding. Unicode : Encoding. UTF32;
2140
+ byte [ ] zstr = enc. GetBytes( argv [ i ] + "\0 " ) ;
2141
+ Marshal. Copy ( zstr , 0 , curStrPtr , zstr . Length ) ;
2142
+ Marshal. WriteIntPtr ( mem + IntPtr . Size * i , curStrPtr ) ;
2143
+ curStrPtr += zstr. Length ;
2144
+ }
2145
+ PySys_SetArgvEx( argc , mem , updatepath ) ;
2146
+ }
2147
+ finally
2148
+ {
2149
+ Marshal. FreeHGlobal ( mem ) ;
2150
+ }
2151
+ }
2087
2152
#elif PYTHON2
2088
2153
[ DllImport ( Runtime . dll , CallingConvention = CallingConvention . Cdecl ,
2089
2154
ExactSpelling = true , CharSet = CharSet . Ansi ) ]
0 commit comments