-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathExample2.cs
More file actions
107 lines (98 loc) · 3.66 KB
/
Example2.cs
File metadata and controls
107 lines (98 loc) · 3.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Text;
using GhostscriptSharp;
using System.Runtime.InteropServices;
namespace Examples
{
/// <summary>
/// Similar to command line gs
/// </summary>
/// <remarks>Port of Ghostscript Example code at http://pages.cs.wisc.edu/~ghost/doc/cvs/API.htm</remarks>
class Example2
{
public const String KernelDllName = "kernel32.dll";
public const String GhostscriptDllDirectory = @"C:\Program Files\gs\gs8.71\bin";
public static String start_string = "systemdict /start get exec" + System.Environment.NewLine;
[DllImport(KernelDllName, SetLastError = true)]
static extern int SetDllDirectory(string lpPathName);
[DllImport(KernelDllName, EntryPoint = "RtlMoveMemory", SetLastError = true)]
static extern int CopyMemory(IntPtr dest, IntPtr source, Int64 count);
static void Main(string[] args)
{
#region StdIn Handler
StringBuilder sbInput = new StringBuilder();
// This is very slow, especially because Ghostscript asks for input 1 character at a time
API.StdinCallback stdin = (caller_handle, str, n) =>
{
if (n == 0)
{
str = IntPtr.Zero;
return 0;
}
if (sbInput.Length == 0)
{
sbInput.AppendLine(Console.ReadLine());
}
if (sbInput.Length > 0)
{
int len = (sbInput.Length < n) ? sbInput.Length : n;
byte[] b = ASCIIEncoding.ASCII.GetBytes(sbInput.ToString(0, len));
GCHandle cHandle = GCHandle.Alloc(b, GCHandleType.Pinned);
IntPtr cPtr = cHandle.AddrOfPinnedObject();
Int64 copyLen = (long)len;
CopyMemory(str, cPtr, copyLen);
cPtr = IntPtr.Zero;
cHandle.Free();
sbInput.Remove(0, len);
return len;
}
return 0;
};
#endregion
#region StdOut Handler
API.StdoutCallback stdout = (caller_handle, buf, len) =>
{
Console.Write(buf.Substring(0, len));
return len;
};
#endregion
#region StdErr Handler
API.StdoutCallback stderr = (caller_handle, buf, len) =>
{
Console.Error.Write(buf.Substring(0, len));
return len;
};
#endregion
string[] gsargv = new string[args.Length + 1];
gsargv[0] = "GhostscriptSharp"; /* actual value doesn't matter */
Array.Copy(args, 0, gsargv, 1, args.Length);
IntPtr minst;
int code, code1;
int exit_code;
SetDllDirectory(GhostscriptDllDirectory);
code = API.CreateAPIInstance(out minst, IntPtr.Zero);
if (code < 0)
{
System.Environment.Exit(1);
}
API.Set_Stdio(minst, stdin, stdout, stderr);
code = API.InitAPI(minst, gsargv.Length, gsargv);
if (code == 0)
{
API.RunString(minst, start_string, 0, out exit_code);
}
code1 = API.ExitAPI(minst);
if ((code == 0) || (code == (int)API.GhostscriptErrorCode.e_Quit))
{
code = code1;
}
API.DeleteAPIInstance(minst);
if ((code == 0) || (code == (int)API.GhostscriptErrorCode.e_Quit))
{
System.Environment.Exit(0);
}
System.Environment.Exit(1);
}
}
}