/// <summary> /// Load appropriate DLL. Check Environment for 32 bit or 64 bit and load appropriate module. /// Get all the API function address from the DLL and store that in the appropriate /// Delegate function global variable. Must be called first thing before attempting any /// other function calls. /// </summary> /// <returns>true if the module is loaded. Else false.</returns> protected bool InitialiseDLL() { if (!SetDllLib()) { return(false); } StdIOIn = new StdioMessageEventHandler(OnStdIoInput); StdIOOut = new StdioMessageEventHandler(OnStdIoOutput); StdIOErr = new StdioMessageEventHandler(OnStdIoError); GsMessage = ""; GsErrMessage = ""; gsNewInstance = GetDelegateFunction <gsapi_new_instance>("gsapi_new_instance"); gsDeleteInstance = GetDelegateFunction <gsapi_delete_instance>("gsapi_delete_instance"); gsSetStdio = GetDelegateFunction <gsapi_set_stdio>("gsapi_set_stdio"); gsSetPoll = GetDelegateFunction <gsapi_set_poll>("gsapi_set_poll"); gsSetDisplayCallback = GetDelegateFunction <gsapi_set_display_callback>("gsapi_set_display_callback"); gsSetArgEncoding = GetDelegateFunction <gsapi_set_arg_encoding>("gsapi_set_arg_encoding"); gsRunStringBegin = GetDelegateFunction <gsapi_run_string_begin>("gsapi_run_string_begin"); gsRunStringContinue = GetDelegateFunction <gsapi_run_string_continue>("gsapi_run_string_continue"); gsRunStringEnd = GetDelegateFunction <gsapi_run_string_end>("gsapi_run_string_end"); gsRunStringLength = GetDelegateFunction <gsapi_run_string_with_length>("gsapi_run_string_with_length"); gsRunString = GetDelegateFunction <gsapi_run_string>("gsapi_run_string"); gsInitArgs = GetDelegateFunction <gsapi_init_with_args>("gsapi_init_with_args"); gsRunFile = GetDelegateFunction <gsapi_run_file>("gsapi_run_file"); gsExit = GetDelegateFunction <gsapi_exit>("gsapi_exit"); gsAddfs = GetDelegateFunction <gsapi_add_fs>("gsapi_add_fs"); gsRemovefs = GetDelegateFunction <gsapi_remove_fs>("gsapi_remove_fs"); return(true); }
/// <summary> /// Simplified instantiation of the class. Message event handler and GSDisplay event handler /// can be passed as parameter which are properly handled. Any of this pointer can be null /// if there is no need to handle them /// </summary> /// <param name="stdIn">Callback StdioMessageEventHandler function pointer for StdIo-Input</param> /// <param name="stdOut">Callback StdioMessageEventHandler function pointer for StdIo-Output</param> /// <param name="stdErr">Callback StdioMessageEventHandler function pointer for StdIo-Error</param> /// <param name="dh">Callback DisplayEventHandler function pointer for GSDisplay Event</param> /// <returns>True if successful</returns> public bool GSInitialise(StdioMessageEventHandler stdIn, StdioMessageEventHandler stdOut, StdioMessageEventHandler stdErr, DisplayEventHandler dh) { if (InitialiseDLL()) { SetStdIOHandlers(stdIn, stdOut, stdErr); SetConsole(); _gsDisplay.SetDisplayEventHandler(dh); return(true); } return(false); }
/// <summary> /// Main Window /// </summary> public MainWindow() { InitializeComponent(); _stdIn = new StdioMessageEventHandler(OnStdIoInput); _stdOut = new StdioMessageEventHandler(OnStdIoOutput); _stdErr = new StdioMessageEventHandler(OnStdIoError); _displayHandle = new DisplayEventHandler(GsInteractive_displayEvent); _gsInteractive = new GSInteractive(); if (!_gsInteractive.GSInitialise(_stdIn, _stdOut, _stdErr, _displayHandle)) { MessageBox.Show("Ghostscript DLL not found or not of the right version. Make sure latest version of Ghostscript is installed.\nExiting the Application"); this.Close(); return; } _gsVersion = _gsInteractive.GetRevisionInfo(); tbkStatus.Text = string.Format("Ghostscript Version Found: {0} Dated: {1}", _gsVersion.revision, _gsVersion.revisiondate); SetDevelopPanel(Settings.Default.DevelopPanel); }
/// <summary> /// ExecuteCommand is the easier interface to gs_init_args, which takes care of all the /// background requirements for running this function and then cleans the environment /// after running the file. If you do not want to handle the stdIO then you can always /// call GetLastMessage and GetLastErrorMessage to get the messages. /// </summary> /// <param name="args">Args that need to be passed</param> /// <param name="stdIn">If you want to handle Input locally set this. Generally leave this null for this call</param> /// <param name="stdOut">If you want to handle the messages then set this</param> /// <param name="stdErr">If you want to handle the error messages then set this</param> /// <returns></returns> public int ExecuteCommand(string[] args, StdioMessageEventHandler stdIn = null, StdioMessageEventHandler stdOut = null, StdioMessageEventHandler stdErr = null) { // Get a pointer to an instance of the Ghostscript API and run the API with the current arguments IntPtr pInstance = IntPtr.Zero; int result; GsMessage = ""; GsErrMessage = ""; lock (objLock) { result = gsNewInstance(out pInstance, IntPtr.Zero); StatusUpdate(string.Format("Sent Command: 'gsNewInstance(out pInstance, IntPtr.Zero)'\n"), string.Format("pInstance returned: 0x{0:X16}\n", pInstance.ToInt64())); if (result != 0) { return(result); } try { SetStdIOHandlers(stdIn, stdOut, stdErr); result = gsSetStdio(pInstance, StdIOIn, StdIOOut, StdIOErr); StatusUpdate(string.Format("Sent Command: 'gsSetStdio(0x{0:X16}, new StdioMessageEventHandler(OnStdIoInput), new StdioMessageEventHandler(OnStdIoOutput), new StdioMessageEventHandler(OnStdIoError))'\n", pInstance.ToInt64())); result = gsSetArgEncoding(pInstance, GS_ARG_ENCODING.UTF8); StatusUpdate(string.Format("Sent Command: 'gsSetArgEncoding(0x{0:X16}, GS_ARG_ENCODING.UTF8)'\n", pInstance.ToInt64())); result = gsInitArgs(pInstance, args.Length, args); StatusUpdate(string.Format("Sent Command: 'gsInitArgs(0x{0:X16}, {1}, {2})'\n", pInstance.ToInt64(), args.Length, _strArgs)); } finally { Cleanup(pInstance); StatusUpdate(string.Format("Sent Command: 'gsExit(0x{0:X16})'\n", pInstance.ToInt64())); StatusUpdate(string.Format("Sent Command: 'gsDeleteInstance(0x{0:X16})'\n", pInstance.ToInt64())); } } return(0); }
private static extern int gsapi_set_stdio(IntPtr pInstance, StdioMessageEventHandler gsdll_stdin, StdioMessageEventHandler gsdll_stdout, StdioMessageEventHandler gsdll_stderr);
/// <summary> /// Sets the Callback StdioMessageEventHandler functions of the remote program for handling StdIO messages /// </summary> /// <param name="stdIn">Callback StdioMessageEventHandler function pointer for StdIo-Input</param> /// <param name="stdOut">Callback StdioMessageEventHandler function pointer for StdIo-Ouput</param> /// <param name="stdErr">Callback StdioMessageEventHandler function pointer for StdIo-Error</param> public void SetIOHandlers(StdioMessageEventHandler stdIn, StdioMessageEventHandler stdOut, StdioMessageEventHandler stdErr) { SetStdIOHandlers(stdIn, stdOut, stdErr); }
/// <summary> /// Set the callback functions for stdio The stdin callback function should return the number of characters read, 0 for EOF, /// or -1 for error. The stdout and stderr callback functions should return the number of characters written. See KSPdfView for /// implementation of this /// </summary> /// <param name="pInstance">Ghostscript Instance handle obtained by calling GSNewInstance</param> /// <param name="stdIn">StdioMessageEventHandler pointer for Input</param> /// <param name="stdOut">StdioMessageEventHandler pointer for Output Message</param> /// <param name="stdErr">StdioMessageEventHandler pointer for Error Output</param> /// <returns>returns zero if successful</returns> public int GSSetStdio(IntPtr pInstance, StdioMessageEventHandler stdIn, StdioMessageEventHandler stdOut, StdioMessageEventHandler stdErr) { SetStdIOHandlers(stdIn, stdOut, stdErr); return(gsSetStdio(pInstance, StdIOIn, StdIOOut, StdIOErr)); }
/// <summary> /// Set the private handler variables to remote StdioMessageEventHandler methods. /// If set the local method will pass the values to the remote handler /// for further processing /// </summary> /// <param name="stdIn">Remote Input handler for StdioMessageEventHandler method</param> /// <param name="stdOut">Remote Output handler for StdioMessageEventHandler method</param> /// <param name="stdErr">Remote Error handler for StdioMessageEventHandler method</param> protected void SetStdIOHandlers(StdioMessageEventHandler stdIn, StdioMessageEventHandler stdOut, StdioMessageEventHandler stdErr) { _stdIn = stdIn; _stdOut = stdOut; _stdErr = stdErr; }