Exemplo n.º 1
0
        /* Direct call on gsapi to get the version of the DLL we are using */
        public String GetVersion()
        {
            gsapi_revision_t vers;

            vers.copyright    = IntPtr.Zero;
            vers.product      = IntPtr.Zero;
            vers.revision     = 0;
            vers.revisiondate = 0;
            int size = System.Runtime.InteropServices.Marshal.SizeOf(vers);

            try
            {
                if (GSAPI.gsapi_revision(ref vers, size) == 0)
                {
                    String product = Marshal.PtrToStringAnsi(vers.product);
                    String output;
                    int    major   = vers.revision / 100;
                    int    minor   = vers.revision - major * 100;
                    String versnum = major + "." + minor;
                    output = product + " " + versnum;
                    return(output);
                }
                else
                {
                    return(null);
                }
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            return(null);
        }
Exemplo n.º 2
0
        /* Close the display device and delete the instance */
        public gsParamState_t DisplayDeviceClose()
        {
            int            code       = 0;
            gsParamState_t out_params = new gsParamState_t();

            out_params.result = GS_Result_t.gsOK;

            try
            {
                int code1 = GSAPI.gsapi_exit(dispInstance);
                if ((code == 0) || (code == gsConstants.E_QUIT))
                {
                    code = code1;
                }

                GSAPI.gsapi_delete_instance(dispInstance);
                dispInstance = IntPtr.Zero;
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
                out_params.result = GS_Result_t.gsFAILED;
            }
            return(out_params);
        }
Exemplo n.º 3
0
        /* Worker task for using display device */
        private void DisplayDeviceAsync(object data)
        {
            int            code           = 0;
            List <object>  genericlist    = data as List <object>;
            gsParamState_t gsparams       = (gsParamState_t)genericlist[0];
            gsParamState_t Result         = gsparams;
            GCHandle       argPtrsStable  = new GCHandle();
            int            num_params     = gsparams.args.Count;
            var            argParam       = new GCHandle[num_params];
            var            argPtrs        = new IntPtr[num_params];
            List <byte[]>  CharacterArray = new List <byte[]>(num_params);
            bool           cleanup        = true;

            gsparams.result = GS_Result_t.gsOK;

            try
            {
                code = GSAPI.gsapi_new_instance(out dispInstance, IntPtr.Zero);
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_new_instance error");
                }

                code = GSAPI.gsapi_set_stdio(dispInstance, stdin_callback, stdout_callback, stderr_callback);
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_set_stdio error");
                }

                code = GSAPI.gsapi_set_arg_encoding(dispInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_set_arg_encoding error");
                }

                code = GSAPI.gsapi_set_display_callback(dispInstance, ptr_display_struct);
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_set_display_callback error");
                }

                String fullcommand = "";
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes((gsparams.args[k] + "\0").ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    fullcommand = fullcommand + " " + gsparams.args[k];
                }
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                fullcommand = "Command Line: " + fullcommand + "\n";
                StdIOCallBack(fullcommand, fullcommand.Length);
                code = GSAPI.gsapi_init_with_args(dispInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                if (code < 0)
                {
                    throw new GhostscriptException("DisplayDeviceAsync: gsapi_init_with_args error");
                }
            }

            catch (DllNotFoundException except)
            {
                gsErrorReport("Exception: " + except.Message);
                gsparams.result = GS_Result_t.gsFAILED;
                cleanup         = false;
                Result          = gsparams;
            }
            catch (BadImageFormatException except)
            {
                gsErrorReport("Exception: " + except.Message);
                gsparams.result = GS_Result_t.gsFAILED;
                cleanup         = false;
                Result          = gsparams;
            }
            catch (GhostscriptException except)
            {
                gsErrorReport("Exception: " + except.Message);
                gsparams.result = GS_Result_t.gsFAILED;
                if (dispInstance != IntPtr.Zero)
                {
                    GSAPI.gsapi_delete_instance(dispInstance);
                }
                dispInstance = IntPtr.Zero;
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
                gsparams.result = GS_Result_t.gsFAILED;
                if (dispInstance != IntPtr.Zero)
                {
                    GSAPI.gsapi_delete_instance(dispInstance);
                }
                dispInstance = IntPtr.Zero;
            }
            finally
            {
                if (cleanup)
                {
                    for (int k = 0; k < num_params; k++)
                    {
                        argParam[k].Free();
                    }
                    argPtrsStable.Free();
                    Result = gsparams;

                    if (gsparams.result == GS_Result_t.gsOK && (gsparams.task == GS_Task_t.DISPLAY_DEV_NON_PDF ||
                                                                gsparams.task == GS_Task_t.DISPLAY_DEV_THUMBS_NON_PDF))
                    {
                        gsParamState_t result = DisplayDeviceClose();
                        if (gsparams.result == 0)
                        {
                            gsparams.result = result.result;
                        }
                    }
                }
            }
            gsCompleted(Result);
            return;
        }
Exemplo n.º 4
0
        /* Processing with gsapi_run_string for callback progress */
        private void gsBytesAsync(object data)
        {
            List <object>  genericlist    = data as List <object>;
            gsParamState_t Params         = (gsParamState_t)genericlist[0];
            gsParamState_t Result         = Params;
            int            num_params     = Params.args.Count;
            var            argParam       = new GCHandle[num_params];
            var            argPtrs        = new IntPtr[num_params];
            List <byte[]>  CharacterArray = new List <byte[]>(num_params);
            GCHandle       argPtrsStable  = new GCHandle();

            Byte[] Buffer = new Byte[gsConstants.GS_READ_BUFFER];

            int        code     = 0;
            int        exitcode = 0;
            var        Feed     = new GCHandle();
            var        FeedPtr  = new IntPtr();
            FileStream fs       = null;
            bool       cleanup  = true;

            try
            {
                /* Open the file */
                fs = new FileStream(Params.inputfile, FileMode.Open);
                var len = (int)fs.Length;

                code = GSAPI.gsapi_new_instance(out gsInstance, IntPtr.Zero);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_new_instance error");
                }
                code = GSAPI.gsapi_set_stdio(gsInstance, stdin_callback, stdout_callback, stderr_callback);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_set_stdio error");
                }
                code = GSAPI.gsapi_set_arg_encoding(gsInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_set_arg_encoding error");
                }

                /* Now convert our Strings to char* and get pinned handles to these.
                 * This keeps the c# GC from moving stuff around on us */
                String fullcommand = "";
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes((Params.args[k] + "\0").ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    fullcommand = fullcommand + " " + Params.args[k];
                }

                /* Also stick the array of pointers into memory that will not be GCd */
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                fullcommand = "Command Line: " + fullcommand + "\n";
                StdIOCallBack(fullcommand, fullcommand.Length);
                code = GSAPI.gsapi_init_with_args(gsInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                if (code < 0)
                {
                    throw new GhostscriptException("gsBytesAsync: gsapi_init_with_args error");
                }

                /* Pin data buffer */
                Feed    = GCHandle.Alloc(Buffer, GCHandleType.Pinned);
                FeedPtr = Feed.AddrOfPinnedObject();

                /* Now start feeding the input piece meal and do a call back
                 * with our progress */
                if (code == 0)
                {
                    int    count;
                    double perc;
                    int    total = 0;

                    GSAPI.gsapi_run_string_begin(gsInstance, 0, ref exitcode);
                    if (exitcode < 0)
                    {
                        code = exitcode;
                        throw new GhostscriptException("gsBytesAsync: gsapi_run_string_begin error");
                    }

                    while ((count = fs.Read(Buffer, 0, gsConstants.GS_READ_BUFFER)) > 0)
                    {
                        GSAPI.gsapi_run_string_continue(gsInstance, FeedPtr, count, 0, ref exitcode);
                        if (exitcode < 0)
                        {
                            code = exitcode;
                            throw new GhostscriptException("gsBytesAsync: gsapi_run_string_continue error");
                        }

                        total = total + count;
                        perc  = 100.0 * (double)total / (double)len;
                        gsProgressChanged(Params, (int)perc);
                    }
                    GSAPI.gsapi_run_string_end(gsInstance, 0, ref exitcode);
                    if (exitcode < 0)
                    {
                        code = exitcode;
                        throw new GhostscriptException("gsBytesAsync: gsapi_run_string_end error");
                    }
                }
            }
            catch (DllNotFoundException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (BadImageFormatException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (GhostscriptException except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            catch (Exception except)
            {
                /* Could be a file io issue */
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            finally
            {
                if (cleanup)
                {
                    fs.Close();

                    /* Free pinned items */
                    for (int k = 0; k < num_params; k++)
                    {
                        argParam[k].Free();
                    }
                    argPtrsStable.Free();
                    Feed.Free();

                    /* gs clean up */
                    int code1 = GSAPI.gsapi_exit(gsInstance);
                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        code = code1;
                    }

                    GSAPI.gsapi_delete_instance(gsInstance);
                    Params.return_code = code;

                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        Params.result = GS_Result_t.gsOK;
                        Result        = Params;
                    }
                    else
                    {
                        Params.result = GS_Result_t.gsFAILED;
                        Result        = Params;
                    }
                    gsInstance = IntPtr.Zero;
                }
            }
            gsCompleted(Result);
            return;
        }
Exemplo n.º 5
0
        /* Process command line with gsapi_init_with_args */
        private void gsFileAsync(object data)
        {
            List <object>  genericlist    = data as List <object>;
            gsParamState_t Params         = (gsParamState_t)genericlist[0];
            gsParamState_t Result         = Params;
            int            num_params     = Params.args.Count;
            var            argParam       = new GCHandle[num_params];
            var            argPtrs        = new IntPtr[num_params];
            List <byte[]>  CharacterArray = new List <byte[]>(num_params);
            GCHandle       argPtrsStable  = new GCHandle();
            int            code           = 0;
            bool           cleanup        = true;

            try
            {
                code = GSAPI.gsapi_new_instance(out gsInstance, IntPtr.Zero);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_new_instance error");
                }
                code = GSAPI.gsapi_set_stdio(gsInstance, stdin_callback, stdout_callback, stderr_callback);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_set_stdio error");
                }
                code = GSAPI.gsapi_set_arg_encoding(gsInstance, (int)gsEncoding.GS_ARG_ENCODING_UTF8);
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_set_arg_encoding error");
                }

                /* Now convert our Strings to char* and get pinned handles to these.
                 * This keeps the c# GC from moving stuff around on us */
                String fullcommand = "";
                for (int k = 0; k < num_params; k++)
                {
                    CharacterArray.Add(System.Text.Encoding.UTF8.GetBytes((Params.args[k] + "\0").ToCharArray()));
                    argParam[k] = GCHandle.Alloc(CharacterArray[k], GCHandleType.Pinned);
                    argPtrs[k]  = argParam[k].AddrOfPinnedObject();
                    fullcommand = fullcommand + " " + Params.args[k];
                }

                /* Also stick the array of pointers into memory that will not be GCd */
                argPtrsStable = GCHandle.Alloc(argPtrs, GCHandleType.Pinned);

                fullcommand = "Command Line: " + fullcommand + "\n";
                StdIOCallBack(fullcommand, fullcommand.Length);
                code = GSAPI.gsapi_init_with_args(gsInstance, num_params, argPtrsStable.AddrOfPinnedObject());
                if (code < 0)
                {
                    throw new GhostscriptException("gsFileAsync: gsapi_init_with_args error");
                }
            }
            catch (DllNotFoundException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (BadImageFormatException except)
            {
                gsErrorReport("Exception: " + except.Message);
                Params.result = GS_Result_t.gsFAILED;
                cleanup       = false;
                Result        = Params;
            }
            catch (GhostscriptException except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            catch (Exception except)
            {
                gsErrorReport("Exception: " + except.Message);
            }
            finally
            {
                if (cleanup)
                {
                    /* All the pinned items need to be freed so the GC can do its job */
                    for (int k = 0; k < num_params; k++)
                    {
                        argParam[k].Free();
                    }
                    argPtrsStable.Free();

                    int code1 = GSAPI.gsapi_exit(gsInstance);
                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        code = code1;
                    }

                    GSAPI.gsapi_delete_instance(gsInstance);
                    Params.return_code = code;

                    if ((code == 0) || (code == gsConstants.E_QUIT))
                    {
                        Params.result = GS_Result_t.gsOK;
                        Result        = Params;
                    }
                    else
                    {
                        Params.result = GS_Result_t.gsFAILED;
                        Result        = Params;
                    }
                    gsInstance = IntPtr.Zero;
                }
            }

            /* Completed. */
            gsCompleted(Result);
            return;
        }