/// <summary> /// Write raw data to printer through its Windows print handle /// </summary> /// <param name="data">buffer to send</param> /// <returns>Return code</returns> public static ReturnCode WritePrinter(string printerName, byte[] data) { // Gotta get a pointer on the local heap. Fun fact, the naming suggests that // this would be on the stack but it isn't. Windows no longer has a global heap // per se so these naming conventions are legacy cruft. IntPtr ptr = Marshal.AllocHGlobal(data.Length); Marshal.Copy(data, 0, ptr, data.Length); bool result = RawPrinterHelper.SendBytesToPrinter(printerName, ptr, data.Length); Marshal.FreeHGlobal(ptr); return(result ? ReturnCode.Success : ReturnCode.ExecutionFailure); }
/// <summary> /// Reads count bytes from printer /// </summary> /// <param name="printerName">Win32 printer name</param> /// <param name="count">Count of bytes to read</param> /// <returns>Response data</returns> public static byte[] ReadPrinter(string printerName, int count) { Int32 dwCount = count; IntPtr pBytes = new IntPtr(dwCount); byte[] returnbytes = new byte[dwCount]; pBytes = Marshal.AllocCoTaskMem(dwCount); bool success = RawPrinterHelper.ReadFromPrinter(printerName, pBytes, dwCount); if (success) { Marshal.Copy(returnbytes, 0, pBytes, dwCount); } else { returnbytes = new byte[0]; } return(returnbytes); }