打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
如何通过使用 Visual C#.net 将原始数据发送到打印机
  1 public class RawPrinterHelper
  2 {
  3     // Structure and API declarions:
  4     [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
  5     public class DOCINFOA
  6     {
  7         [MarshalAs(UnmanagedType.LPStr)] public string pDocName;
  8         [MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
  9         [MarshalAs(UnmanagedType.LPStr)] public string pDataType;
 10     }
 11     [DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
 12     public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
 13 
 14     [DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
 15     public static extern bool ClosePrinter(IntPtr hPrinter);
 16 
 17     [DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
 18     public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level,  [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
 19 
 20     [DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
 21     public static extern bool EndDocPrinter(IntPtr hPrinter);
 22 
 23     [DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
 24     public static extern bool StartPagePrinter(IntPtr hPrinter);
 25 
 26     [DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
 27     public static extern bool EndPagePrinter(IntPtr hPrinter);
 28 
 29     [DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
 30     public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
 31 
 32     // SendBytesToPrinter()
 33     // When the function is given a printer name and an unmanaged array
 34     // of bytes, the function sends those bytes to the print queue.
 35     // Returns true on success, false on failure.
 36     public static bool SendBytesToPrinter( string szPrinterName, IntPtr pBytes, Int32 dwCount)
 37     {
 38         Int32    dwError = 0, dwWritten = 0;
 39         IntPtr    hPrinter = new IntPtr(0);
 40         DOCINFOA    di = new DOCINFOA();
 41         bool    bSuccess = false// Assume failure unless you specifically succeed.
 42 
 43         di.pDocName = "My C#.NET RAW Document";
 44         di.pDataType = "RAW";
 45 
 46         // Open the printer.
 47         if( OpenPrinter( szPrinterName.Normalize(), out hPrinter, IntPtr.Zero ) )
 48         {
 49             // Start a document.
 50             if( StartDocPrinter(hPrinter, 1, di) )
 51             {
 52                 // Start a page.
 53                 if( StartPagePrinter(hPrinter) )
 54                 {
 55                     // Write your bytes.
 56                     bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
 57                     EndPagePrinter(hPrinter);
 58                 }
 59                 EndDocPrinter(hPrinter);
 60             }
 61             ClosePrinter(hPrinter);
 62         }
 63         // If you did not succeed, GetLastError may give more information
 64         // about why not.
 65         if( bSuccess == false )
 66         {
 67                 dwError = Marshal.GetLastWin32Error();
 68         }
 69         return bSuccess;
 70     }
 71 
 72     public static bool SendFileToPrinter( string szPrinterName, string szFileName )
 73     {
 74         // Open the file.
 75         FileStream fs = new FileStream(szFileName, FileMode.Open);
 76         // Create a BinaryReader on the file.
 77         BinaryReader br = new BinaryReader(fs);
 78         // Dim an array of bytes big enough to hold the file's contents.
 79         Byte []bytes = new Byte[fs.Length];
 80         bool bSuccess = false;
 81         // Your unmanaged pointer.
 82         IntPtr pUnmanagedBytes = new IntPtr(0);
 83         int nLength;
 84 
 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 }
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#托管代码与C++非托管代码互相调用一(C#调用C++代码&.net 代码安全)
C#中使用OpenGL:(二)C#调用C/C++的dll
C#中DllImport用法和路径问题
C#开发cs结构的软件配合短信猫群发短信! - .NET技术 / C#
C#DllImport的用法
C#下usb条码扫描枪的钩子实现的改进
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服