Skip to content

Commit

Permalink
Merge pull request #40 from Zhuangkh/master
Browse files Browse the repository at this point in the history
fix filename with utf8 encoding
  • Loading branch information
stchan authored Jan 13, 2022
2 parents 2a05c65 + cd7ffcf commit b4eb2f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
8 changes: 7 additions & 1 deletion PdfScribe/GhostScript64.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ public static void CallAPI(string[] args)
lock (resourceLock)
{
NativeMethods.CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
IntPtr[] utf8Ptrs = new IntPtr[args.Length];
for (int i = 0; i < utf8Ptrs.Length; i++)
utf8Ptrs[i] = NativeMethods.NativeUtf8FromString(args[i]);
try
{
int result = NativeMethods.InitAPI(gsInstancePtr, args.Length, args);
NativeMethods.SetEncoding(gsInstancePtr, NativeMethods.GS_ARG_ENCODING_UTF8);
int result = NativeMethods.InitAPI(gsInstancePtr, args.Length, utf8Ptrs);

if (result < 0)
{
Expand All @@ -36,6 +40,8 @@ public static void CallAPI(string[] args)
}
finally
{
for (int i = 0; i < utf8Ptrs.Length; i++)
Marshal.FreeHGlobal(utf8Ptrs[i]);
Cleanup(gsInstancePtr);
}
}
Expand Down
19 changes: 18 additions & 1 deletion PdfScribe/NativeMethods.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Text;

namespace PdfScribe
{
Expand All @@ -14,11 +15,17 @@ satisfy FxCop requirements
*/

#region Hooks into Ghostscript DLL
internal const int GS_ARG_ENCODING_LOCAL = 0;
internal const int GS_ARG_ENCODING_UTF8 = 1;

[DllImport("gsdll64.dll", EntryPoint = "gsapi_new_instance")]
internal static extern int CreateAPIInstance(out IntPtr pinstance, IntPtr caller_handle);

[DllImport("gsdll64.dll", EntryPoint = "gsapi_init_with_args")]
internal static extern int InitAPI(IntPtr instance, int argc, string[] argv);
internal static extern int InitAPI(IntPtr instance, int argc, IntPtr[] argv);

[DllImport("gsdll64.dll", EntryPoint = "gsapi_set_arg_encoding")]
internal static extern int SetEncoding(IntPtr inst, int encoding);

[DllImport("gsdll64.dll", EntryPoint = "gsapi_exit")]
internal static extern int ExitAPI(IntPtr instance);
Expand All @@ -29,5 +36,15 @@ satisfy FxCop requirements

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
internal static extern bool SetDllDirectory(string lpPathName);

internal static IntPtr NativeUtf8FromString(string managedString)
{
int len = Encoding.UTF8.GetByteCount(managedString);
byte[] buffer = new byte[len + 1]; // null-terminator allocated
Encoding.UTF8.GetBytes(managedString, 0, managedString.Length, buffer, 0);
IntPtr nativeUtf8 = Marshal.AllocHGlobal(buffer.Length);
Marshal.Copy(buffer, 0, nativeUtf8, buffer.Length);
return nativeUtf8;
}
}
}

0 comments on commit b4eb2f9

Please sign in to comment.