forked from cedrozor/myrtille
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GhostScript64.cs
63 lines (56 loc) · 2.07 KB
/
GhostScript64.cs
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
/*
pdf scribe virtual pdf printer
all credits to Sherman Chan (https://github.com/stchan/PdfScribe)
this code is licensed under LGPL v3 (https://www.gnu.org/licenses/lgpl-3.0.en.html)
changes from original code are surrounded by "myrtille" region tags
*/
using System;
using System.Runtime.InteropServices;
namespace Myrtille.Printer
{
internal class GhostScript64
{
/*
This code was adapted from Matthew Ephraim's Ghostscript.Net project
external dll definitions moved into NativeMethods to
satisfy FxCop requirements
https://github.com/mephraim/ghostscriptsharp
*/
/// <summary>
/// Calls the Ghostscript API with a collection of arguments to be passed to it
/// </summary>
public static void CallAPI(string[] args)
{
// Get a pointer to an instance of the Ghostscript API and run the API with the current arguments
IntPtr gsInstancePtr;
lock (resourceLock)
{
NativeMethods64.CreateAPIInstance(out gsInstancePtr, IntPtr.Zero);
try
{
int result = NativeMethods64.InitAPI(gsInstancePtr, args.Length, args);
if (result < 0)
{
throw new ExternalException("Ghostscript conversion error", result);
}
}
finally
{
Cleanup(gsInstancePtr);
}
}
}
/// <summary>
/// Frees up the memory used for the API arguments and clears the Ghostscript API instance
/// </summary>
private static void Cleanup(IntPtr gsInstancePtr)
{
NativeMethods64.ExitAPI(gsInstancePtr);
NativeMethods64.DeleteAPIInstance(gsInstancePtr);
}
/// <summary>
/// GS can only support a single instance, so we need to bottleneck any multi-threaded systems.
/// </summary>
private static object resourceLock = new object();
}
}