locked
Printer Spooler - registry issue RRS feed

  • Question

  • I have an application that dynamically create/delete printers. It does that by using Printer Spooler API functions
    AddPrinter and DeletePrinter. This works fine except printers are not always deleted from registry (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Print\Printers).
    There are key's left (<printer name>,<some_number>) in the registry after delete printer.

    What cause this issue? How to solve this?

    Note that restart print spooler does not help.

    Tuesday, November 12, 2019 9:09 AM

Answers

  • Finally reproduced it with 3rd party application. Seems like 3rd party application does not close some handles properly.
    Return value form DeletePrinter is True and there is no pending jobs. So when I print from that application, end task application, then call DeletePrinter the key (<printer name>,<some_number>) is left in the registry.
    Tuesday, November 12, 2019 3:32 PM

All replies

  • Does your code check the return from DeletePrinter? Does it succeed in these cases?

    Is there any pattern as to when this occurs - i.e. is it a specific printer driver, are there any print jobs pending for the printer?

    Tuesday, November 12, 2019 9:50 AM
  • Yes, in my log file DeletePrinter sometimes finish with error code 170, but the printer is deleted. This could be the case when printer name is left in registry. It happened for different drivers.
    Not sure yet for pending jobs. I'm trying to reproduce.
    Tuesday, November 12, 2019 10:43 AM
  • Yes, in my log file DeletePrinter sometimes finish with error code 170, but the printer is deleted.

    170 would be "The requested resource is in use. "

    Can you clarify that you are checking the return value from DeletePrinter before calling GetLastError?

    Tuesday, November 12, 2019 10:49 AM
  • The return value is not checked. I'll add that and try again.
    Tuesday, November 12, 2019 11:11 AM
  • Just to clarify, you should always do this:

    RetValFromApi = AWindowsAPI()
    if ( RetValFromApi indicates an error )
    {
      GetLastError();

    Rather than:

    RetValFromApi = AWindowsAPI();
    GetLastError();

    Tuesday, November 12, 2019 11:35 AM
  • And it never hurts to check the documentation for the Windows API function being used.  Some functions do not set the error code upon failure.
    • Edited by RLWA32 Tuesday, November 12, 2019 11:43 AM
    Tuesday, November 12, 2019 11:43 AM
  • Finally reproduced it with 3rd party application. Seems like 3rd party application does not close some handles properly.
    Return value form DeletePrinter is True and there is no pending jobs. So when I print from that application, end task application, then call DeletePrinter the key (<printer name>,<some_number>) is left in the registry.
    Tuesday, November 12, 2019 3:32 PM
  • Seems like 3rd party application does not close some handles properly.

    Is that speculation, or can you reproduce the problem if you write a test program that does something?

    If you're confident its down to this other application, try contacting the vendor and see what they say.

    Tuesday, November 12, 2019 3:37 PM
  • So when I print from that application, end task application, ....
    Do you mean you are terminating the 3rd party application from the task manager instead of allowing it to terminate normally?
    Tuesday, November 12, 2019 3:41 PM
  • Yes, in case when terminate normally, it works fine..
    Tuesday, November 12, 2019 3:43 PM