Access denied when attempting to remove printer

好久不见. 提交于 2020-06-17 05:06:53

问题


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.)


回答1:


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)


来源:https://stackoverflow.com/questions/37578005/access-denied-when-attempting-to-remove-printer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!