Advertisement
Guest User

Untitled

a guest
May 22nd, 2022
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.26 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Printing;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. public class RawPrinterHelper
  8. {
  9.     public static void Main()
  10.     {
  11.         byte[] Array = ConvertToByteArray("Druck mich bitte bitte Ich hab keine Lust nicht gedruckt zu werden!\n", Encoding.ASCII);
  12.         string stuff = Formatio(Array);
  13.         byte[] Binary = ConvertToByteArray(stuff, Encoding.ASCII);
  14.         IntPtr unmanagedPointer = Marshal.AllocHGlobal(Binary.Length);
  15.         Marshal.Copy(Binary, 0, unmanagedPointer, Binary.Length);
  16.         // Call unmanaged code
  17.         Int32 Counter = Binary.Length;
  18.         /*
  19.         if(SendBytesToPrinter("MyPrinter",unmanagedPointer, Counter) != false)
  20.         {
  21.             Console.WriteLine("HOORAY!");
  22.         }
  23.         else
  24.         {
  25.             Console.WriteLine("BOOOOH");
  26.         }
  27.         Marshal.FreeHGlobal(unmanagedPointer);
  28.         */
  29.    
  30.     }
  31.     // Structure and API declarions:
  32.     [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  33.     public class DOCINFOA
  34.     {
  35.         [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
  36.         [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
  37.         [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
  38.     }
  39.     [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  40.     public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
  41.  
  42.     [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  43.     public static extern bool ClosePrinter(IntPtr hPrinter);
  44.  
  45.     [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  46.     public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
  47.  
  48.     [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  49.     public static extern bool EndDocPrinter(IntPtr hPrinter);
  50.  
  51.     [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  52.     public static extern bool StartPagePrinter(IntPtr hPrinter);
  53.  
  54.     [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  55.     public static extern bool EndPagePrinter(IntPtr hPrinter);
  56.  
  57.     [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  58.     public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
  59.  
  60.     // SendBytesToPrinter()
  61.     // When the function is given a printer name and an unmanaged array
  62.     // of bytes, the function sends those bytes to the print queue.
  63.     // Returns true on success, false on failure.
  64.  
  65.     public static byte[] ConvertToByteArray(string str, Encoding encoding)
  66.     {
  67.         return encoding.GetBytes(str);
  68.     }
  69.     //Formats a byte[] into a binary string (010010010010100101010)
  70.     public static string Formatio(byte[] data)
  71.     {
  72.         //storage for the resulting string
  73.         string result = string.Empty;
  74.         //iterate through the byte[]
  75.         foreach (byte value in data)
  76.         {
  77.             //storage for the individual byte
  78.             string binarybyte = Convert.ToString(value, 2);
  79.             //if the binarybyte is not 8 characters long, its not a proper result
  80.             while (binarybyte.Length < 8)
  81.             {
  82.                 //prepend the value with a 0
  83.                 binarybyte = "0" + binarybyte;
  84.             }
  85.             //append the binarybyte to the result
  86.             result += binarybyte;
  87.         }
  88.         //return the result
  89.         return result;
  90.     }
  91.     public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
  92.     {
  93.         Int32 dwError = 0, dwWritten = 0;
  94.         IntPtr hPrinter = new IntPtr(0);
  95.         DOCINFOA di = new DOCINFOA();
  96.         bool bSuccess = false; // Assume failure unless you specifically succeed.
  97.         di.pDocName = "My C#.NET RAW Document";
  98.         di.pDataType = "RAW";
  99.  
  100.         // Open the printer.
  101.         if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
  102.         {
  103.             // Start a document.
  104.             if (StartDocPrinter(hPrinter, 1, di))
  105.             {
  106.                 // Start a page.
  107.                 if (StartPagePrinter(hPrinter))
  108.                 {
  109.                     // Write your bytes.
  110.                     bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
  111.                     EndPagePrinter(hPrinter);
  112.                 }
  113.                 EndDocPrinter(hPrinter);
  114.             }
  115.             ClosePrinter(hPrinter);
  116.         }
  117.         // If you did not succeed, GetLastError may give more information
  118.         // about why not.
  119.         if (bSuccess == false)
  120.         {
  121.             dwError = Marshal.GetLastWin32Error();
  122.         }
  123.         return bSuccess;
  124.     }
  125.  
  126.     public static bool SendFileToPrinter(string szPrinterName, string szFileName)
  127.     {
  128.         // Open the file.
  129.         FileStream fs = new FileStream(szFileName, FileMode.Open);
  130.         // Create a BinaryReader on the file.
  131.         BinaryReader br = new BinaryReader(fs);
  132.         // Dim an array of bytes big enough to hold the file's contents.
  133.         Byte[] bytes = new Byte[fs.Length];
  134.         bool bSuccess = false;
  135.         // Your unmanaged pointer.
  136.         IntPtr pUnmanagedBytes = new IntPtr(0);
  137.         int nLength;
  138.  
  139.         nLength = Convert.ToInt32(fs.Length);
  140.         // Read the contents of the file into the array.
  141.         bytes = br.ReadBytes(nLength);
  142.         // Allocate some unmanaged memory for those bytes.
  143.         pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
  144.         // Copy the managed byte array into the unmanaged array.
  145.         Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
  146.         // Send the unmanaged bytes to the printer.
  147.         bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
  148.         // Free the unmanaged memory that you allocated earlier.
  149.         Marshal.FreeCoTaskMem(pUnmanagedBytes);
  150.         return bSuccess;
  151.     }
  152.  
  153.     public static bool SendStringToPrinter(string szPrinterName, string szString)
  154.     {
  155.         IntPtr pBytes;
  156.         Int32 dwCount;
  157.  
  158.         // How many characters are in the string?
  159.         // Fix from Nicholas Piasecki:
  160.         // dwCount = szString.Length;
  161.         dwCount = (szString.Length + 1) * Marshal.SystemMaxDBCSCharSize;
  162.  
  163.         // Assume that the printer is expecting ANSI text, and then convert
  164.         // the string to ANSI text.
  165.         pBytes = Marshal.StringToCoTaskMemAnsi(szString);
  166.         // Send the converted ANSI string to the printer.
  167.         SendBytesToPrinter(szPrinterName, pBytes, dwCount);
  168.         Marshal.FreeCoTaskMem(pBytes);
  169.         return true;
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement