DeletePrinter (winspool)
Last changed: Señor CMasMas-38.104.125.90

.
Summary
The DeletePrinter function deletes the specified printer object.

C# Signature:

[DllImport("winspool.drv", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
    static extern bool DeletePrinter(IntPtr hPrinter);

VB Signature:

Declare Function DeletePrinter Lib "winspool.dll" (TODO) As TODO

User-Defined Types:

None.

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Alternative using unmanaged printui.PrintUIEntryW

Sample Code:

private void UninstallPrinter(String printerName)

    {
        IntPtr hPrinter;
        PRINTER_DEFAULTS defaults = new PRINTER_DEFAULTS
        {
        //PRINTER_ALL_ACCESS
        DesiredAccess = 0x000F000C,
        pDatatype = IntPtr.Zero,
        pDevMode = IntPtr.Zero
        };

        if (OpenPrinter(printerName, out hPrinter, ref defaults) == false)
        {
        int errno = Marshal.GetLastWin32Error();
        throw new Win32Exception(errno);
        }

        if (hPrinter == IntPtr.Zero)
        {
        int errno = Marshal.GetLastWin32Error();
        throw new Win32Exception(errno);
        }

        if (DeletePrinter(hPrinter) == false)
        {
        int errno = Marshal.GetLastWin32Error();
        throw new Win32Exception(errno);
        }

        if (ClosePrinter(hPrinter) == false)
        {
        int errno = Marshal.GetLastWin32Error();
        throw new Win32Exception(errno);
        }
    }

Documentation