c++winapiprintingspooler

'net start spooler' fails with exit code 2 when trying to restart the printer spooler on Windows


My application installs a virtual printer driver.

On some users' systems I need to restart the printer spooler because functions like OpenPrinter return RPC_S_SERVER_UNAVAILABLE (text is "The RPC server is unavailable."), which means the spooler has stopped working.

I try to do that by calling "net start spooler" with CreateProcess:

PROCESS_INFORMATION pi = { 0 };
STARTUPINFO si = { sizeof(si) };
// later
CreateProcess(0, "net start spooler", 0, 0, 0, 0, 0, 0, &si, &pi );

On some users' systems this works, but on others it fails with exit code 2, and GetLastError returns ERROR_NO_MORE_FILES (text is "There are no more files.").

Does anyone know what ERROR_NO_MORE_FILES would mean in this case?


Solution

  • It seem that you interpret the ERRORLEVEL code 2 in the wrong way. It is not the code ERROR_NO_MORE_FILES. I started cmd.exe under the user account and received the following output

    C:>echo %errorlevel% 0

    C:\Users\Rita>net stop spooler System error 5 has occurred.

    Access is denied.

    C:>echo %errorlevel% 2

    Any exit code of "net.exe" larger as 0 is an error.

    I recommend you to use StartService to start the service, then you will have more error control.