andrew4582

RawPrinterHelper

Mar 13th, 2013
982
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7.  
  8. namespace Core.UI.Controls
  9. {
  10.     public class RawPrinterHelper
  11.     {
  12.         // Structure and API declarions:
  13.  
  14.         [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
  15.             ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  16.         public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter,
  17.                                               IntPtr pd);
  18.  
  19.         [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true,
  20.             CallingConvention = CallingConvention.StdCall)]
  21.         public static extern bool ClosePrinter(IntPtr hPrinter);
  22.  
  23.         [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi,
  24.             ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  25.         public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level,
  26.                                                   [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
  27.  
  28.         [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true,
  29.             CallingConvention = CallingConvention.StdCall)]
  30.         public static extern bool EndDocPrinter(IntPtr hPrinter);
  31.  
  32.         [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true,
  33.             CallingConvention = CallingConvention.StdCall)]
  34.         public static extern bool StartPagePrinter(IntPtr hPrinter);
  35.  
  36.         [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true,
  37.             CallingConvention = CallingConvention.StdCall)]
  38.         public static extern bool EndPagePrinter(IntPtr hPrinter);
  39.  
  40.         [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true,
  41.             CallingConvention = CallingConvention.StdCall)]
  42.         public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
  43.  
  44.         // SendBytesToPrinter()
  45.         // When the function is given a printer name and an unmanaged array
  46.         // of bytes, the function sends those bytes to the print queue.
  47.         // Returns true on success, false on failure.
  48.         public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
  49.         {
  50.             Int32 dwError = 0, dwWritten = 0;
  51.             var hPrinter = new IntPtr(0);
  52.             var di = new DOCINFOA();
  53.             bool bSuccess = false; // Assume failure unless you specifically succeed.
  54.  
  55.             di.pDocName = "My C#.NET RAW Document";
  56.             di.pDataType = "RAW";
  57.  
  58.             // Open the printer.
  59.             if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
  60.             {
  61.                 // Start a document.
  62.                 if (StartDocPrinter(hPrinter, 1, di))
  63.                 {
  64.                     // Start a page.
  65.                     if (StartPagePrinter(hPrinter))
  66.                     {
  67.                         // Write your bytes.
  68.                         bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
  69.                         EndPagePrinter(hPrinter);
  70.                     }
  71.                     EndDocPrinter(hPrinter);
  72.                 }
  73.                 ClosePrinter(hPrinter);
  74.             }
  75.             // If you did not succeed, GetLastError may give more information
  76.             // about why not.
  77.             if (bSuccess == false)
  78.             {
  79.                 dwError = Marshal.GetLastWin32Error();
  80.             }
  81.             return bSuccess;
  82.         }
  83.  
  84.         public static bool SendFileToPrinter(string szPrinterName, string szFileName)
  85.         {
  86.             // Open the file.
  87.             var fs = new FileStream(szFileName, FileMode.Open);
  88.             // Create a BinaryReader on the file.
  89.             var br = new BinaryReader(fs);
  90.             // Dim an array of bytes big enough to hold the file's contents.
  91.             var bytes = new Byte[fs.Length];
  92.             bool bSuccess = false;
  93.             // Your unmanaged pointer.
  94.             var pUnmanagedBytes = new IntPtr(0);
  95.             int nLength;
  96.  
  97.             nLength = Convert.ToInt32(fs.Length);
  98.             // Read the contents of the file into the array.
  99.             bytes = br.ReadBytes(nLength);
  100.             // Allocate some unmanaged memory for those bytes.
  101.             pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
  102.             // Copy the managed byte array into the unmanaged array.
  103.             Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
  104.             // Send the unmanaged bytes to the printer.
  105.             bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
  106.             // Free the unmanaged memory that you allocated earlier.
  107.             Marshal.FreeCoTaskMem(pUnmanagedBytes);
  108.             return bSuccess;
  109.         }
  110.  
  111.         public static bool SendStringToPrinter(string szPrinterName, string szString)
  112.         {
  113.             IntPtr pBytes;
  114.             Int32 dwCount;
  115.             // How many characters are in the string?
  116.             dwCount = szString.Length;
  117.             // Assume that the printer is expecting ANSI text, and then convert
  118.             // the string to ANSI text.
  119.             pBytes = Marshal.StringToCoTaskMemAnsi(szString);
  120.             // Send the converted ANSI string to the printer.
  121.             SendBytesToPrinter(szPrinterName, pBytes, dwCount);
  122.             Marshal.FreeCoTaskMem(pBytes);
  123.             return true;
  124.         }
  125.  
  126.         [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  127.         public class DOCINFOA
  128.         {
  129.             [MarshalAs(UnmanagedType.LPStr)]
  130.             public string pDocName;
  131.             [MarshalAs(UnmanagedType.LPStr)]
  132.             public string pOutputFile;
  133.             [MarshalAs(UnmanagedType.LPStr)]
  134.             public string pDataType;
  135.         }
  136.     }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment