Cannot delete printer from control panel via Win32 API on Windows 8.1 and Windows 10 RRS feed

  • Question

  • I'm trying to delete the printer entry from Windows programmatically and it seems to work with TCP/IP printers but not with assigned to USB port. Actually, the printer entry disappears from Win32_Printer collection but it still exists in "Control Panel - Devices and Printers". It has only name there without driver and additional info assigned. I was trying to do this via ManagementScope and via "DeletePrinter" Print Spooler API method. It works on Windows 7, but does not work on Windows 8.1/10

    My code snippets:


    ManagementScope scope = new ManagementScope(ManagementPath.DefaultPath);
    SelectQuery query = new SelectQuery("select * from Win32_Printer WHERE Name LIKE '" + printerName + "'");
    ManagementObjectSearcher search = new ManagementObjectSearcher(scope, query);
    ManagementObjectCollection printersCollection = search.Get();
    foreach (ManagementObject printer in printersCollection)


    [DllImport("winspool.drv", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    static extern bool DeletePrinter(IntPtr hPrinter);
    [DllImport("winspool.drv", SetLastError = true)]
    static extern int ClosePrinter(IntPtr hPrinter);
    [DllImport("winspool.drv", CharSet = CharSet.Unicode, ExactSpelling = false, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    public static extern int OpenPrinter(string pPrinterName, out IntPtr phPrinter, IntPtr pDefault);
    private static bool DeletePrinter(string printerName)
       var pd = new PRINTER_DEFAULTS { DesiredAccess = 8 | 4 | 0x000F0000 };
       var rawsize = Marshal.SizeOf(pd);
       var pdPtr = Marshal.AllocHGlobal(rawsize);
       Marshal.StructureToPtr(pd, pdPtr, true);
       IntPtr hPrinter;
       if (OpenPrinter(printerName, out hPrinter, pdPtr) != 0)
          if (hPrinter != IntPtr.Zero)
             var result = DeletePrinter(hPrinter);
             return result;
       return false;

    Thursday, April 7, 2016 10:51 AM

All replies

  • Can anyone confirm if Microsoft has fixed this problem on Windows 8.1 and Windows 10?   Thanks.
    Thursday, March 23, 2017 6:48 PM