public static GetCvPort.PRINTER_INFO_2 GetPrinterInfo(string printerName) { IntPtr hPrinter = new IntPtr(); if (!GetCvPort.OpenPrinter(printerName, ref hPrinter, (long)IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr num = IntPtr.Zero; try { int pcbNeeded1 = new int(); GetCvPort.GetPrinter(hPrinter, 2, IntPtr.Zero, 0, ref pcbNeeded1); if (pcbNeeded1 <= 0) { throw new Exception("Fail!!"); } num = Marshal.AllocHGlobal(pcbNeeded1); int pcbNeeded2 = new int(); if (!GetCvPort.GetPrinter(hPrinter, 2, num, pcbNeeded1, ref pcbNeeded2)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } object structure = Marshal.PtrToStructure(num, typeof(GetCvPort.PRINTER_INFO_2)); GetCvPort.PRINTER_INFO_2 printerInfo2 = new PRINTER_INFO_2(); return(structure != null ? (GetCvPort.PRINTER_INFO_2)structure : printerInfo2); } finally { GetCvPort.ClosePrinter(hPrinter); Marshal.FreeHGlobal(num); } }
public static string GetPortName(ref string strPrinterDeviceName) { int dwLevel = 5; IntPtr hPrinter = new IntPtr(); if (!GetCvPort.OpenPrinter(strPrinterDeviceName, ref hPrinter, (long)IntPtr.Zero)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } IntPtr zero = IntPtr.Zero; try { int pcbNeeded1 = new int(); GetCvPort.GetPrinter(hPrinter, dwLevel, (IntPtr)Conversions.ToLong((string)null), 0, ref pcbNeeded1); if (pcbNeeded1 <= 0) { throw new Exception("error!"); } IntPtr pPrinter = Marshal.AllocHGlobal(pcbNeeded1); int pcbNeeded2 = new int(); if (!GetCvPort.GetPrinter(hPrinter, dwLevel, pPrinter, pcbNeeded1, ref pcbNeeded2)) { throw new Win32Exception(Marshal.GetLastWin32Error()); } GetCvPort.PRINTER_INFO_2 printerInfo = GetCvPort.GetPrinterInfo(strPrinterDeviceName); if (((ulong)printerInfo.Attributes & 2048UL & (ulong)-(((long)printerInfo.Attributes & 1024L) == 0L ? 1 : 0)) > 0UL && printerInfo.pDriverName.IndexOf("DS-RX1") != -1) { return(printerInfo.pPortName); } return(""); } finally { GetCvPort.ClosePrinter(hPrinter); } }