protected static void SetDeviceInfo4(string name, PRINTER_INFO_4 newInfo) { IntPtr printerHandle; PrinterDefaults defaults = new PrinterDefaults { DesiredAccess = PRINTER_ACCESS.PrinterAllAccess }; if (!PrintPort.OpenPrinter(name, out printerHandle, ref defaults)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } try { IntPtr infoHandle = Marshal.AllocHGlobal(Marshal.SizeOf(newInfo)); Marshal.StructureToPtr(newInfo, infoHandle, false); try { if (!SetPrinter(printerHandle, 4, infoHandle, 0)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } catch { throw; } finally { Marshal.FreeHGlobal(infoHandle); } } catch { throw; } finally { PrintPort.ClosePrinter(printerHandle); } }
protected static PRINTER_INFO_4[] enumDeviceInfos4(PRINTER_ENUM_FLAGS flags) { uint cbNeeded = 0; uint cReturned = 0; if (EnumPrinters(flags, null, 4, IntPtr.Zero, 0, ref cbNeeded, ref cReturned)) { return(null); } int lastWin32Error = Marshal.GetLastWin32Error(); if (lastWin32Error == ERROR_INSUFFICIENT_BUFFER) { IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded); if (EnumPrinters(flags, null, 4, pAddr, cbNeeded, ref cbNeeded, ref cReturned)) { PRINTER_INFO_4[] printerInfo4 = new PRINTER_INFO_4[cReturned]; int offset = pAddr.ToInt32(); Type type = typeof(PRINTER_INFO_4); int increment = Marshal.SizeOf(type); for (int i = 0; i < cReturned; i++) { printerInfo4[i] = (PRINTER_INFO_4)Marshal.PtrToStructure(new IntPtr(offset), type); offset += increment; } return(printerInfo4); } Marshal.FreeHGlobal(pAddr); lastWin32Error = Marshal.GetLastWin32Error(); } throw new Win32Exception(lastWin32Error); }
protected PRINTER_INFO_4 ToInfo4() { PRINTER_INFO_4 info = new PRINTER_INFO_4(); info.pPrinterName = Name; info.pServerName = ServerName; info.Attributes = Attributes; return(info); }
protected static void RenameDevice4(string oldName, string newName) { try { PRINTER_INFO_4 pi = GetDeviceInfo4(oldName); pi.pPrinterName = newName; PrintDevice.SetDeviceInfo4(oldName, pi); } catch { throw; } }
protected virtual void FromInfo4(PRINTER_INFO_4 info) { Name = info.pPrinterName; ServerName = info.pServerName; Attributes = info.Attributes; }