2
def on_printer_button_clicked(self, button):
    for i in range(len(self.printer_buttons)):
        if button == self.printer_buttons[i]:
            pHandle = win32print.OpenPrinter(self.printers[i]['pPrinterName'])
    win32print.DeletePrinter(pHandle)
    return

So all I'm doing is opening the printer handle and calling the function Delete Printer, as you can see. Here's what I get in the console when I run the function:

uninstall_windowGUI.py", line 57, in on_printer_button_clicked
win32print.DeletePrinter(pHandle)
pywintypes.error: (5, 'DeletePrinter', 'Access is denied.')

I've tried running the IDE (Pycharm in Administrator mode, and still get the same issue. Any idea on how to move on? I'm kind of stuck until I can figure this out. (Also: I'm using Gtk and Gdk to create the interface, if that makes a differece.)

Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54
TheEggSample
  • 333
  • 4
  • 5
  • 18

1 Answers1

5

The documentation states that Printer handle must be opened for PRINTER_ACCESS_ADMINISTER. Something like this might work:

PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ACCESS_ADMINISTER} 
win32print.OpenPrinter(self.printers[i]['pPrinterName'], PRINTER_DEFAULTS)
Jussi Kukkonen
  • 13,857
  • 1
  • 37
  • 54
  • 2
    Thanks, that helped a ton. I actually had to replace PRINTER_ACCESS_ADMINISTER to PRINTER_ALL_ACCESS, because for whatever reason I still was getting the "Access denied". – TheEggSample Jun 02 '16 at 18:41