@@ -76,13 +76,25 @@ public static void Invoke(string[] args, string tmp) => SetTemp(tmp, () =>
7676 _ = NativeMethods . NewInstance ( out var core , IntPtr . Zero ) ;
7777 if ( core == IntPtr . Zero ) throw new GsApiException ( GsApiStatus . UnknownError , "gsapi_new_instance" ) ;
7878
79+ IntPtr [ ] utf8argv = new IntPtr [ args . Length ] ;
80+ for ( int i = 0 ; i < utf8argv . Length ; i ++ )
81+ {
82+ utf8argv [ i ] = NativeUtf8FromString ( args [ i ] ) ;
83+ }
84+
7985 try
8086 {
81- var code = NativeMethods . InitWithArgs ( core , args . Length , args ) ;
87+ NativeMethods . SetArgEncoding ( core , 1 /*GS_ARG_ENCODING_UTF8*/ ) ;
88+ var code = NativeMethods . InitWithArgs ( core , utf8argv . Length , utf8argv ) ;
8289 if ( code < 0 && code != ( int ) GsApiStatus . Quit && code != ( int ) GsApiStatus . Info ) throw new GsApiException ( code ) ;
8390 }
8491 finally
8592 {
93+ for ( int i = 0 ; i < utf8argv . Length ; i ++ )
94+ {
95+ Marshal . FreeHGlobal ( utf8argv [ i ] ) ;
96+ }
97+
8698 _ = NativeMethods . Exit ( core ) ;
8799 NativeMethods . DeleteInstance ( core ) ;
88100 }
@@ -91,7 +103,24 @@ public static void Invoke(string[] args, string tmp) => SetTemp(tmp, () =>
91103 #endregion
92104
93105 #region Implementations
94-
106+ /* --------------------------------------------------------------------- */
107+ ///
108+ /// NativeUtf8FromString
109+ ///
110+ /// <summary>
111+ /// Convert string to nativeUTF8 ptr. *remember to clean up with a call to Marshal.FreeHGlobal.*
112+ /// </summary>
113+ ///
114+ /* --------------------------------------------------------------------- */
115+ public static IntPtr NativeUtf8FromString ( string managedString )
116+ {
117+ int len = System . Text . Encoding . UTF8 . GetByteCount ( managedString ) ;
118+ byte [ ] buffer = new byte [ len + 1 ] ; // null-terminator allocated
119+ System . Text . Encoding . UTF8 . GetBytes ( managedString , 0 , managedString . Length , buffer , 0 ) ;
120+ IntPtr nativeUtf8 = Marshal . AllocHGlobal ( buffer . Length ) ;
121+ Marshal . Copy ( buffer , 0 , nativeUtf8 , buffer . Length ) ;
122+ return nativeUtf8 ;
123+ }
95124 /* --------------------------------------------------------------------- */
96125 ///
97126 /// SetTemp
0 commit comments