0

I'm calling GhostScript(GS) command line tool from C++ code with WinAPI CreateProcess function. When I'm running my application as 'common' executable file, everything works fine, but when I'm running my app as service, something goes wrong - GS process creates and it's listed in task manager processes list but then nothing happens. If I run my application as a service on Windows 7 it works correctly, but on Windows 10 the above problem occurs. Code listed below. I will be grateful for any advice.

std::string cmd = ("gswin32c.exe -sDEVICE=mswinpr2 -dBATCH -dNOPAUSE -dNOSAFER -dNoCancel=true -sOutputFile=\"%printer%HPLJ2000\" \"1.pdf\"");
SECURITY_ATTRIBUTES sa;`sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.lpSecurityDescriptor = NULL;
sa.bInheritHandle = TRUE;
auto dwFlags = FILE_ATTRIBUTE_NORMAL;
STARTUPINFOW si;
GetStartupInfoW(&si);
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
ZeroMemory(&pi, sizeof(pi));

if (!CreateProcess(NULL, cmd.c_str(), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &si, &pi)) {
    std::err << "Something went wrong" << std::endl;
}

WaitForSingleObject(pi.hProcess, INFINITE);
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
Peter
  • 435
  • 1
  • 3
  • 11
  • The problem may be related to the user account (and its privileges) used to start the service (by default "System"). If GS provides a log or some other info written to file, you can analyze it to search for issues. – roalz Aug 28 '18 at 12:11
  • You are using current working directory relative file names. Don't do that. The current working directory is outside your control, and failure to open a file is just one of the many ways it can manifest itself. – IInspectable Aug 28 '18 at 12:17
  • begin from `CreateProcessAsUserW`, duplicate your process token, set here `WTSGetActiveConsoleSessionId()` and create new process with this token on `"Winsta0\\Default"` desktop. not hide window. and not pass additional parameters(enter it manually). look which will be effect and based on this move to some side. also in your current code `WaitForSingleObject(pi.hProcess, INFINITE);` senseless and bad. and for what you set `bInheritHandle = TRUE;` ? – RbMm Aug 28 '18 at 13:06

1 Answers1

0

Your process is probably running on the wrong desktop.

There's a bunch of stuff you need to do to fix this. Full example code here:

https://stackoverflow.com/a/50743993/5743288

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48