Skip to content

Commit 903b6cb

Browse files
committed
force utf8 encoding to InitWithArgs args
1 parent 8f5c202 commit 903b6cb

2 files changed

Lines changed: 44 additions & 3 deletions

File tree

Libraries/Generating/Sources/Internal/GsApi.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Libraries/Generating/Sources/Internal/GsApiNative.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@ internal static class NativeMethods
5858
[DllImport(LibName, EntryPoint = "gsapi_new_instance")]
5959
public static extern int NewInstance(out IntPtr instance, IntPtr handle);
6060

61+
/* --------------------------------------------------------------------- */
62+
///
63+
/// SetArgEncoding
64+
///
65+
/// <summary>
66+
/// Sets encoding of arguments supplied via the gsapi interface
67+
/// </summary>
68+
///
69+
/* --------------------------------------------------------------------- */
70+
[DllImport(LibName, EntryPoint = "gsapi_set_arg_encoding")]
71+
public static extern int SetArgEncoding(IntPtr instance, int encoding);
72+
6173
/* --------------------------------------------------------------------- */
6274
///
6375
/// InitWithArgs
@@ -68,7 +80,7 @@ internal static class NativeMethods
6880
///
6981
/* --------------------------------------------------------------------- */
7082
[DllImport(LibName, EntryPoint = "gsapi_init_with_args")]
71-
public static extern int InitWithArgs(IntPtr instance, int argc, string[] argv);
83+
public static extern int InitWithArgs(IntPtr instance, int argc, IntPtr[] argv);
7284

7385
/* --------------------------------------------------------------------- */
7486
///

0 commit comments

Comments
 (0)