/// <summary> /// Deletes a printer from the designated server. Must have administrator access to it. /// </summary> /// <param name="sServer"></param> /// <param name="sPrinter"></param> public static void DeletePrinter(CredentialEntry ce, string sServer, string sPrinter) { Session.EnsureNullSession(sServer, ce); // first, open the printer string s = Canon(sServer) + @"\" + sPrinter; int hPrinter; PRINTER_DEFAULTS pd = new PRINTER_DEFAULTS(); pd.pDatatype = IntPtr.Zero; pd.pDevMode = IntPtr.Zero; pd.DesiredAccess = PRINTER_ALL_ACCESS; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(pd)); Marshal.StructureToPtr(pd, p, false); if (!OpenPrinter(s, out hPrinter, p)) { // free memory Marshal.DestroyStructure(p, typeof(PRINTER_DEFAULTS)); Marshal.FreeHGlobal(p); int nError = Marshal.GetLastWin32Error(); Win32Exception we = new Win32Exception(nError); if (nError == nErrorAccessDenied) { throw new AuthException(we); } else { throw we; } } Marshal.DestroyStructure(p, typeof(PRINTER_DEFAULTS)); Marshal.FreeHGlobal(p); // now remove it from AD PRINTER_INFO_7 pi7 = new PRINTER_INFO_7(); pi7.pszObjectGUID = IntPtr.Zero; pi7.dwAction = (uint) DSPRINT_OPTIONS.DSPRINT_UNPUBLISH; p = Marshal.AllocHGlobal(Marshal.SizeOf(pi7)); Marshal.StructureToPtr(pi7, p, false); SetPrinter(hPrinter, 7, p, 0); // NOTE: we swallow any error since the operation may be left in a "pending" state rather // than returning success or failure right away. // free memory Marshal.DestroyStructure(p, typeof(PRINTER_INFO_7)); Marshal.FreeHGlobal(p); // finally, remove the printer if (!DeletePrinter(hPrinter)) { int nError = Marshal.GetLastWin32Error(); Win32Exception we = new Win32Exception(nError); if (nError == nErrorAccessDenied) { throw new AuthException(we); } else { throw we; } } }
/// <summary> /// Deletes a printer from the designated server. Must have administrator access to it. /// </summary> /// <param name="sServer"></param> /// <param name="sPrinter"></param> public static void DeletePrinter(CredentialEntry ce, string sServer, string sPrinter) { Session.EnsureNullSession(sServer, ce); // first, open the printer string s = Canon(sServer) + @"\" + sPrinter; int hPrinter; PRINTER_DEFAULTS pd = new PRINTER_DEFAULTS(); pd.pDatatype = IntPtr.Zero; pd.pDevMode = IntPtr.Zero; pd.DesiredAccess = PRINTER_ALL_ACCESS; IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(pd)); Marshal.StructureToPtr(pd, p, false); if (!OpenPrinter(s, out hPrinter, p)) { // free memory Marshal.DestroyStructure(p, typeof(PRINTER_DEFAULTS)); Marshal.FreeHGlobal(p); int nError = Marshal.GetLastWin32Error(); Win32Exception we = new Win32Exception(nError); if (nError == nErrorAccessDenied) { throw new AuthException(we); } else { throw we; } } Marshal.DestroyStructure(p, typeof(PRINTER_DEFAULTS)); Marshal.FreeHGlobal(p); // now remove it from AD PRINTER_INFO_7 pi7 = new PRINTER_INFO_7(); pi7.pszObjectGUID = IntPtr.Zero; pi7.dwAction = (uint)DSPRINT_OPTIONS.DSPRINT_UNPUBLISH; p = Marshal.AllocHGlobal(Marshal.SizeOf(pi7)); Marshal.StructureToPtr(pi7, p, false); SetPrinter(hPrinter, 7, p, 0); // NOTE: we swallow any error since the operation may be left in a "pending" state rather // than returning success or failure right away. // free memory Marshal.DestroyStructure(p, typeof(PRINTER_INFO_7)); Marshal.FreeHGlobal(p); // finally, remove the printer if (!DeletePrinter(hPrinter)) { int nError = Marshal.GetLastWin32Error(); Win32Exception we = new Win32Exception(nError); if (nError == nErrorAccessDenied) { throw new AuthException(we); } else { throw we; } } }