/Samba.Services/Implementations/PrinterModule/Tools/RawPrinterHelper.cs

https://github.com/emreeren/SambaPOS-3 · C# · 125 lines · 89 code · 12 blank · 24 comment · 5 complexity · 93e877733ecd070dc3c67cb140951310 MD5 · raw file

  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. namespace Samba.Services.Implementations.PrinterModule.Tools
  8. {
  9. public class RawPrinterHelper
  10. {
  11. // Structure and API declarions:
  12. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
  13. public class DOCINFOA
  14. {
  15. [MarshalAs(UnmanagedType.LPStr)]
  16. public string pDocName;
  17. [MarshalAs(UnmanagedType.LPStr)]
  18. public string pOutputFile;
  19. [MarshalAs(UnmanagedType.LPStr)]
  20. public string pDataType;
  21. }
  22. [DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  23. public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
  24. [DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  25. public static extern bool ClosePrinter(IntPtr hPrinter);
  26. [DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  27. public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
  28. [DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  29. public static extern bool EndDocPrinter(IntPtr hPrinter);
  30. [DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  31. public static extern bool StartPagePrinter(IntPtr hPrinter);
  32. [DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  33. public static extern bool EndPagePrinter(IntPtr hPrinter);
  34. [DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
  35. public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);
  36. // SendBytesToPrinter()
  37. // When the function is given a printer name and an unmanaged array
  38. // of bytes, the function sends those bytes to the print queue.
  39. // Returns true on success, false on failure.
  40. public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
  41. {
  42. Int32 dwError = 0, dwWritten = 0;
  43. IntPtr hPrinter = new IntPtr(0);
  44. DOCINFOA di = new DOCINFOA();
  45. bool bSuccess = false; // Assume failure unless you specifically succeed.
  46. di.pDocName = "My C#.NET RAW Document";
  47. di.pDataType = "RAW";
  48. // Open the printer.
  49. if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
  50. {
  51. // Start a document.
  52. if (StartDocPrinter(hPrinter, 1, di))
  53. {
  54. // Start a page.
  55. if (StartPagePrinter(hPrinter))
  56. {
  57. // Write your bytes.
  58. bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
  59. EndPagePrinter(hPrinter);
  60. }
  61. EndDocPrinter(hPrinter);
  62. }
  63. ClosePrinter(hPrinter);
  64. }
  65. // If you did not succeed, GetLastError may give more information
  66. // about why not.
  67. if (bSuccess == false)
  68. {
  69. dwError = Marshal.GetLastWin32Error();
  70. }
  71. return bSuccess;
  72. }
  73. public static bool SendFileToPrinter(string szPrinterName, string szFileName)
  74. {
  75. // Open the file.
  76. FileStream fs = new FileStream(szFileName, FileMode.Open);
  77. // Create a BinaryReader on the file.
  78. BinaryReader br = new BinaryReader(fs);
  79. // Dim an array of bytes big enough to hold the file's contents.
  80. Byte[] bytes = new Byte[fs.Length];
  81. bool bSuccess = false;
  82. // Your unmanaged pointer.
  83. IntPtr pUnmanagedBytes = new IntPtr(0);
  84. int nLength;
  85. nLength = Convert.ToInt32(fs.Length);
  86. // Read the contents of the file into the array.
  87. bytes = br.ReadBytes(nLength);
  88. // Allocate some unmanaged memory for those bytes.
  89. pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
  90. // Copy the managed byte array into the unmanaged array.
  91. Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
  92. // Send the unmanaged bytes to the printer.
  93. bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
  94. // Free the unmanaged memory that you allocated earlier.
  95. Marshal.FreeCoTaskMem(pUnmanagedBytes);
  96. return bSuccess;
  97. }
  98. public static bool SendStringToPrinter(string szPrinterName, string szString)
  99. {
  100. IntPtr pBytes;
  101. Int32 dwCount;
  102. // How many characters are in the string?
  103. dwCount = szString.Length;
  104. // Assume that the printer is expecting ANSI text, and then convert
  105. // the string to ANSI text.
  106. pBytes = Marshal.StringToCoTaskMemAnsi(szString);
  107. // Send the converted ANSI string to the printer.
  108. SendBytesToPrinter(szPrinterName, pBytes, dwCount);
  109. Marshal.FreeCoTaskMem(pBytes);
  110. return true;
  111. }
  112. }
  113. }