public static PrinterInfo2 GetPrinterProperty(string printerUncName) { var printerInfo2 = new PrinterInfo2(); var pHandle = new IntPtr(); var defaults = new PrinterDefaults(); try { //Open a handle to the printer bool ok = OpenPrinter(printerUncName, out pHandle, defaults); if (!ok) { //OpenPrinter failed, get the last known error and thrown it throw new Win32Exception(Marshal.GetLastWin32Error()); } //Here we determine the size of the data we to be returned //Passing in 0 for the size will force the function to return the size of the data requested int actualDataSize = 0; GetPrinter(pHandle, 2, IntPtr.Zero, 0, ref actualDataSize); int err = Marshal.GetLastWin32Error(); if (err == 122) { if (actualDataSize > 0) { //Allocate memory to the size of the data requested IntPtr printerData = Marshal.AllocHGlobal(actualDataSize); //Retrieve the actual information this time GetPrinter(pHandle, 2, printerData, actualDataSize, ref actualDataSize); //Marshal to our structure printerInfo2 = (PrinterInfo2)Marshal.PtrToStructure(printerData, typeof(PrinterInfo2)); //We've made the conversion, now free up that memory Marshal.FreeHGlobal(printerData); } } else { throw new Win32Exception(err); } return(printerInfo2); } finally { //Always close the handle to the printer ClosePrinter(pHandle); } }
// Static method static void mythread() { var printerInfo2 = new PrinterInfo2(); var pHandle = new IntPtr(); //If the function succeeds, the return value is a nonzero value Console.WriteLine(OpenPrinter("Brother DCP-1610NW series Printer".Normalize(), ref pHandle, null)); int actualDataSize = 0; GetPrinter(pHandle, 2, IntPtr.Zero, 0, ref actualDataSize); Console.WriteLine(pHandle); int err = Marshal.GetLastWin32Error(); if (err == 122) { if (actualDataSize > 0) { //Allocate memory to the size of the data requested IntPtr printerData = Marshal.AllocHGlobal(actualDataSize); //Retrieve the actual information this time GetPrinter(pHandle, 2, printerData, actualDataSize, ref actualDataSize); //Marshal to our structure printerInfo2 = (PrinterInfo2)Marshal.PtrToStructure(printerData, typeof(PrinterInfo2)); //We've made the conversion, now free up that memory Marshal.FreeHGlobal(printerData); } } else { Console.WriteLine(" printer is not connected "); } Console.WriteLine(" MY LOCAL NETWORK PRINTER DATA : "); foreach (var field in typeof(PrinterInfo2).GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)) { Console.WriteLine("{0} = {1}", field.Name, field.GetValue(printerInfo2)); } }
/// <summary> /// /// </summary> /// <returns> /// 0:禁用;1:正常;2:忙;3:离线; /// 10:空闲未装载纸;11:空闲已装载纸,准备就绪 /// </returns> public virtual int GetStatus() { log.Debug("begin"); if (!enabled) { log.DebugFormat("end, return = {0}", StatusCode.Disabled); return(StatusCode.Disabled); } int result = StatusCode.Offline; IntPtr hPrinter; StructPrinterDefaults defaults = new StructPrinterDefaults(); if (!OpenPrinter(name, out hPrinter, ref defaults)) { log.InfoFormat("end, return = {0}", StatusCode.Offline); return(StatusCode.Offline); } uint s = 0x00001000; int cbNeeded = 0; bool bolRet = GetPrinter(hPrinter, 2, IntPtr.Zero, 0, out cbNeeded); if (cbNeeded > 0) { IntPtr pAddr = Marshal.AllocHGlobal((int)cbNeeded); bolRet = GetPrinter(hPrinter, 2, pAddr, cbNeeded, out cbNeeded); if (bolRet) { PrinterInfo2 Info2 = (PrinterInfo2)Marshal.PtrToStructure(pAddr, typeof(PrinterInfo2)); s = Info2.Status; } Marshal.FreeHGlobal(pAddr); } ClosePrinter(hPrinter); log.InfoFormat("s = {0}", s); if ((uint)PrinterStatus.PRINTER_STATUS_PAPER_OUT == (s & (uint)PrinterStatus.PRINTER_STATUS_PAPER_OUT) || (uint)PrinterStatus.PRINTER_STATUS_DOOR_OPEN == (s & (uint)PrinterStatus.PRINTER_STATUS_DOOR_OPEN) || ((uint)PrinterStatus.PRINTER_STATUS_PAPER_PROBLEM == (s & (uint)PrinterStatus.PRINTER_STATUS_PAPER_PROBLEM))) { result = 10; } else if ((uint)PrinterStatus.PRINTER_STATUS_PRINTING == (s & (uint)PrinterStatus.PRINTER_STATUS_PRINTING)) { result = 2; } else if (0 == s) { result = 11; } else { result = 3; } log.InfoFormat("result = {0}", result); log.Debug("end"); return(result); }