Search code examples
c#pdfghostscriptpostscript

GhostScript PS to PDF with images only work during debug mode in Visual Studio


I'm trying to convert a ps file (word file with image) to pdf using Ghostscript.

Everything works fine when I'm debugging my code and just stepping thru it, It generates the pdf with the text,images and whatnot. But when I deploy the app using Visual Studio Setup Project, It does not work and gives me this error "An error occured when call to 'gsapi_new_instance' is made: -100."

Here's my command line arguments

var args = string.Format("-q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=\"{1}\" -c save pop -f \"{0}\"", inputFile, @"C:\MedirefPrinter\converted\out.pdf");

Any idea why this isn't working? Thanks

Actual Code :

File Changed Handler

ShellCommand

Please excuse my noobness :)


Solution

  • Moved to an answer to allow more text.

    There are three possible reasons for the error:

    1) The 'instance' pointer is NULL. I can't see how this is ever possible with our executable as its a globally defined variable and the executable passes its address. This is a sanity check for people writing code against the Ghostscript API.

    2) The application was unable to allocate sufficient memory for some internal structures. Again this seems unlikely as your system would have to be unreasonably short on memory.

    3) The DLL instance count is already 1 or greater. This can happen if the DLL is shared between multiple processes. Unless you build the library with GS_THREADSAFE it isn't thread safe, and so you can't have multiple processes using the same instance of the DLL. I'd guess that this is your problem but obviously you haven't supplied a full set of code, so I don't know. If you are trying to run more than one copy of Ghostscript simultaneously, from the same directory, then you will get this error.

    error -100 means 'something really bad happened so early on that I can't even tell you what it is'.

    I very much doubt that the presence of images in the PostScript has any real impact, except that possibly it may slow the interpretation down enough to cause you to attempt to launch two processes.