Problem with stdout

Kevin Schmitz <[email protected]> Wed, 28 Mar 2018 10:30:09 +0000
Newsgroups gmane.comp.printing.ghostscript.devel
Message-ID <[email protected]>
Hello, everybody,

I hope that I am on the right mailing list, as otherwise I have not found a suitable "channel" to solve my problem.

I am writing an application in C++(Visual Studio 2017) for Windows 10 with Ghostscript *.23 and I am not able to write an output in stdout.

But if I choose""-sOutputFile=myfile.jpeg" instead of "-sOutputFile=%stdout", then everything works fine. I also used the following variants:

"-sOutputFile=%stdout"
"-sOutputFile=%%stdout"
"-sOutputFile=-"
"-o %stdout"
"-o %%stdout"
"-o -"

Depending on the variant, Ghostscript creates a file, which can have the following name:
%stdout
%%stdout
-

The bending of the normal output works and also the bending of the normal output to stderr works fine. It simply does not output the JPEG to stdout.
Anyone see the mistake? I've been desperate for three days on this problem.

#include <Windows.h>
#include <ierrors.h>
#include <iapi.h>
#include <cstdio>
#include <string>

std::string errormessage;

static int /*GSDLLCALL*/ gsdll_stdin (void *instance,       char *buf, int len)
{
       int count = 0;
       while (count < len)
       {
             const int ch = fgetc(stdin);
             if (ch == EOF)
                    return 0;
             *buf++ = ch;
             count++;
             if (ch == '\n')
                    break;
       }
       return count;
}
static int /*GSDLLCALL*/ gsdll_stdout(void *instance, const char *str, int len)
{
       errormessage.append(str, len);

       fwrite(str, 1, len, stdout);
       fflush(stdout);
       return len;
}
static int /*GSDLLCALL*/ gsdll_stderr(void *instance, const char *str, int len)
{
       errormessage.append(str, len);

       fwrite(str, 1, len, stderr);
       fflush(stderr);
       return len;
}

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow)
{
int gs_retcode;

       #pragma region gsapi_new_instance
       void* minst = nullptr;
       gs_retcode = gsapi_new_instance(&minst, GetCurrentProcess()); // nullptr
       if (gs_retcode < 0)
       {
             return 1;
       }
       #pragma endregion
       #pragma region gsapi_set_stdio
       gs_retcode = gsapi_set_stdio(minst, gsdll_stdin, gsdll_stdout, gsdll_stderr);
       if (gs_retcode < 0)
       {
             return 1;
       }
       #pragma endregion
       #pragma region Verarbeitung
       gs_retcode = gsapi_set_arg_encoding(minst, GS_ARG_ENCODING_LOCAL); //UTF8
       if (gs_retcode == 0)
       {
             // Works
             // gswin64c.exe -dSAFER -dBATCH -dNOPAUSE -dNumRenderingThreads=4 -sDEVICE=jpeg -r300 -sOutputFile=- "C:\Users\kschmitz.DUG\Documents\Visual Studio 2017\Projects\VW_Laufzettel_OCR\x64\Release\Laufzettel_20319527.pdf"
             // -dNOSAFER -dBATCH -sstdout=%%stderr -dQUIET -q -o %%stdout
             // const char* gsargv[gsargc] = {"-dNOSAFER", "-dDEBUG", "-dBATCH", "-dNOPAUSE", /*"-dQUIET", "-sstdout=%%stderr",*/ "-sDEVICE=jpeg", "-r300x300", "-dNumRenderingThreads=4", "-sOutputFile=-", "-q", "Laufzettel_20319527.pdf" };
             const int gsargc = 10;
             const char* gsargv[gsargc] = { "-dSAFER", "-dBATCH", "-dNOPAUSE", "-dNumRenderingThreads=4", "-sstdout=%%stderr", "-sDEVICE=jpeg", "-r300x300", "-sOutputFile=%%stdout", "-q", "Laufzettel_20319527.pdf" };

             gs_retcode = gsapi_init_with_args(minst, gsargc, const_cast<char**>(gsargv));

             MessageBoxA(nullptr, errormessage.c_str(), "MELDUNG", MB_OK);
       }
       else
       {
             return 1;
       }
       #pragma endregion
       #pragma region gsapi_exit
       gs_retcode = gsapi_exit(minst);
       if (gs_retcode == 0 || gs_retcode == gs_error_Quit)
       {
             return 1;
       }
       #pragma endregion
       #pragma region gsapi_delete_instance
       gsapi_delete_instance(minst);
       #pragma endregion

       return 0;
}