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

Sending data to USB printer in C#?的更多相关文章

  1. 1125MySQL Sending data导致查询很慢的问题详细分析

    -- 问题1 tablename使用主键索引反而比idx_ref_id慢的原因EXPLAIN SELECT SQL_NO_CACHE COUNT(id) FROM dbname.tbname FORC ...

  2. mysql索引无效且sending data耗时巨大原因分析

    一朋友最近新上线一个项目,本地测试环境跑得好好的,部署到线上却慢得像蜗牛一样.后来查询了一下发现一个sql执行了16秒,有些长的甚至80秒.本地运行都是毫秒级别的查询.下面记录一下困扰了两天的,其中一 ...

  3. mysql 查询开销 sending data

    1.执行一个查询,发现时间开销都在sending data,为什么?2.sending data容易误导,让人以为只是发送数据给客户端,实际上sending data包含两个过程:读取数据并处理,发送 ...

  4. C# Sending data using GET or POST ZZ

    In this short article, I'll show you how to send data to a website from a C# application using GET o ...

  5. MySQL Sending data导致查询很慢的问题详细分析【转载】

    转自http://blog.csdn.net/yunhua_lee/article/details/8573621 [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的时 ...

  6. mysql查询sending data占用大量时间的问题处理

    问题描述:某条sql语句在测试环境执行只需要1秒不到,到了生产环境执行需要8秒以上 在phpmyadmin里面执行性能分析,发现sending data占用了差不多90%以上的时间 查询一下“Send ...

  7. 实战:MySQL Sending data导致查询很慢的问题详细分析(转)

    这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有代表性,分享给大家作为新年礼物:) [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的 ...

  8. 实战:MySQL Sending data导致查询很慢的问题详细分析(转)

    出处:http://blog.csdn.net/yunhua_lee/article/details/8573621 这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有 ...

  9. MySQL Sending data导致查询很慢的问题详细分析

    这两天帮忙定位一个MySQL查询很慢的问题,定位过程综合各种方法.理论.工具,很有代表性,分享给大家作为新年礼物:) [问题现象] 使用sphinx支持倒排索引,但sphinx从mysql查询源数据的 ...

随机推荐

  1. busbox编译出错,arm-linux-未找到命令

    1.问题:/opt/FriendlyARM/mini6410/linux/busybox-1.17.2/scripts/gcc-version.sh: 行 11: arm-linux-gcc: 未找到 ...

  2. PHOTOSHOP 半透明方格

    1.新建60*60的透明文档,在左方和上方用直线工具画白边,存储为图案(编辑/定义图案) 2.新建图层,用油漆桶填充图案 3. 选择若干小方格,填充白色后设置不透明度50%

  3. RX学习笔记:在FreeCodeCamp的学习

    FreeCodeCamp https://www.freecodecamp.com 2016-07-03 前几日在Github浏览时,偶然看到一个叫FreeCodeCamp的开源项目,进去该网站之后感 ...

  4. WF4的数据库 表

    WF4的数据库 表 SQL 建表 SqlPersistenceProviderSchema.sql InstanceData 实例数据表 SqlPersistenceService_Schema.sq ...

  5. 客户端(android,ios)与服务器通信

    android,ios客户端与服务器通信为了便于理解,直接用PHP作为服务器端语言 其实就是一个 http请求响应的过程序,先从 B/S模式说起浏览器发起http请求,服务器响应请求,并把数据返回给浏 ...

  6. win7 64位。未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”提供程序

    win7或win8 64位调试程序,出现这样的错误提示:未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序 解决方法如下: 方法一:“设置应用程序池默认属性”/“常规”/” ...

  7. iOS局部刷新

    iOS: TableView如何刷新指定的cell 或section //一个section刷新 NSIndexSet *indexSet=[[NSIndexSet alloc]initWithInd ...

  8. yii2 model常用验证规则

    //字段必填[['username'],'required','message'=>'{attribute}不能为空!'][['username','password'], 'required' ...

  9. Ubuntu 下部署asp.net运行环境

    在Ubuntu下部署asp.net运行环境,网上教程很多,基本都是编译Mono源码,然后安装jexus.但是可能是我最近RP不太好,编译Mono源码一直都是失败,无奈之下只好找另外的方法安装了. 网上 ...

  10. windows store app search contract

    代码如下: html: <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...