0
votes

I want to remove a mapped printer and P/Invoking the following

[DllImport("winspool.drv", EntryPoint = "DeletePrinterConnection", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int DeletePrinterConnection(string pName);

When I use just the printer name or (PrinterName on server) I keep getting the printer name is invalid.

What name is this expecting as input?

Update 1

[DllImport("winspool.drv", EntryPoint = "DeletePrinterConnection")]
private static extern int DeletePrinterConnectionA(string pName);

DeletePrinterConnectionW (Unicode) and DeletePrinterConnectionA (ANSI)

int r = DeletePrinterConnectionA(printer);
if (!r.Equals(0))
{
   throw new Win32Exception(Marshal.GetLastWin32Error());
}

This throws back Operation completed successfully - but it didn't and the printer is still there.

Return value

If the function succeeds, the return value is a nonzero value. If the function fails, the return value is zero.

2

2 Answers

0
votes

From MSDN:

pName [in]

Pointer to a null-terminated string that specifies the name of the printer connection to delete.

DeletePrinterConnection function

0
votes

Figured it out, because it was a mapped printer the name it's expecting is

\\server\printer name