在网络上共享条码打印机
原来写过一篇C#打印条码与ZPL, 是打印到本地的打印机, 但是往往会需要程序使用远程的打印机进行打印, 其实基本上做法是一致的, 只是把命令发送到打印机的地方修改一下. 具体发送指令的代码微软已经提供了完整的范本: http://support.microsoft.com/kb/322091, 其实只要看一个这篇文章, 基本上就不会有问题了, 不过为了防止以后链接失效, 还是演练一下吧:
在IP地址172.16.8.1 上安装一台打印机, 名为”Argox OS-214 Zip”, 并将其共享, 在开发机上打开\\172.16.8.1, 会看到共享的打印机, 右击—>然后open或connect, 都会自动安装此打印机的驱动到本地, 安装完成以后, 新建一个windows form application, 新增一个类, 然后删除这个新类的所有代码, 把以下代码复制过去:
using System;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;public class RawPrinterHelper
{// Structure and API declarions:[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]public class DOCINFOA{[MarshalAs(UnmanagedType.LPStr)]public string pDocName;[MarshalAs(UnmanagedType.LPStr)]public string pOutputFile;[MarshalAs(UnmanagedType.LPStr)]public string pDataType;}[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool ClosePrinter(IntPtr hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndDocPrinter(IntPtr hPrinter);[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool StartPagePrinter(IntPtr hPrinter);[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool EndPagePrinter(IntPtr hPrinter);[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);// SendBytesToPrinter()// When the function is given a printer name and an unmanaged array// of bytes, the function sends those bytes to the print queue.// Returns true on success, false on failure.public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount){Int32 dwError = 0, dwWritten = 0;IntPtr hPrinter = new IntPtr(0);DOCINFOA di = new DOCINFOA();bool bSuccess = false; // Assume failure unless you specifically succeed.di.pDocName = "My C#.NET RAW Document";di.pDataType = "RAW";// Open the printer.if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero)){// Start a document.if (StartDocPrinter(hPrinter, 1, di)){// Start a page.if (StartPagePrinter(hPrinter)){// Write your bytes.bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);EndPagePrinter(hPrinter);}EndDocPrinter(hPrinter);}ClosePrinter(hPrinter);}// If you did not succeed, GetLastError may give more information// about why not.if (bSuccess == false){dwError = Marshal.GetLastWin32Error();}return bSuccess;}public static bool SendFileToPrinter(string szPrinterName, string szFileName){// Open the file.FileStream fs = new FileStream(szFileName, FileMode.Open);// Create a BinaryReader on the file.BinaryReader br = new BinaryReader(fs);// Dim an array of bytes big enough to hold the file's contents.Byte[] bytes = new Byte[fs.Length];bool bSuccess = false;// Your unmanaged pointer.IntPtr pUnmanagedBytes = new IntPtr(0);int nLength;nLength = Convert.ToInt32(fs.Length);// Read the contents of the file into the array.bytes = br.ReadBytes(nLength);// Allocate some unmanaged memory for those bytes.pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);// Copy the managed byte array into the unmanaged array.Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);// Send the unmanaged bytes to the printer.bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);// Free the unmanaged memory that you allocated earlier.Marshal.FreeCoTaskMem(pUnmanagedBytes);return bSuccess;}public static bool SendStringToPrinter(string szPrinterName, string szString){IntPtr pBytes;Int32 dwCount;// How many characters are in the string?dwCount = szString.Length;// Assume that the printer is expecting ANSI text, and then convert// the string to ANSI text.pBytes = Marshal.StringToCoTaskMemAnsi(szString);// Send the converted ANSI string to the printer.SendBytesToPrinter(szPrinterName, pBytes, dwCount);Marshal.FreeCoTaskMem(pBytes);return true;}
}
然后在我们的form1上拖一个按钮, 双击此按钮进行点击事件, 贴入以下代码:
private void button1_Click(object sender, EventArgs e){var sb = new System.Text.StringBuilder();sb.AppendLine("^XA ");sb.AppendLine("^FO30,30");sb.AppendLine("^FDHello World!^FS");sb.AppendLine("^XZ ");string printerName = @"\\172.16.8.1\Argox OS-214 Zip";RawPrinterHelper.SendStringToPrinter(printerName, sb.ToString());}
F5运行, 点击按钮, 会看到打印机已经打印了一个Hello world出来, 打完收工.
转载于:https://www.cnblogs.com/Moosdau/archive/2011/04/21/2023517.html
在网络上共享条码打印机相关推荐
- 得到本机或者网络上共享打印机的状态和打印任务
//得到本机或者网络上共享打印机的状态和打印任务 //author:ginsonic //zdcnow download from E-E use winspool; function Printer ...
- 将书籍扫描做成电子版在网络上共享,这算不算侵权?
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 将书籍扫描做成 ...
- 使用Webrtc和React Js在网络上共享跨平台的点对点文件
正文字数:3764 阅读时长:9分钟 我们希望实现一个零思想的文件传输机制,即在两个设备或个人之间共享文件,不需要考虑如何.在哪里.为什么和什么. 文 / Dev 原文链接:https://medi ...
- linux 连接两个异构网,如何在Linux(或异构)网络上共享计算机?
在有关共享计算机的这两篇文章中的第 1 部分中,我描述了我的异构本地网络以及如何使用它来比较和测试不同操作系统和体系结构上的应用程序.有几种技术使一台工作站上的用户可以运行位于另一台工作站上的应用程序 ...
- 文件共享服务器imac,iMac怎么在网络上共享设备windows文件夹和服务 | MOS86
本章通过向您展示如何在网络和Mac和Windows计算机之间共享文件,文件夹和设备,帮助您充分利用您的iMac网络连接. →使用Macs共享文件和文件夹使用AirDrop和文件共享 →与Windows ...
- linux 网络部分,在 Linux(或异构)网络上共享计算机,第 1 部分
级别: 初级 David Mertz,博士 (mertz@gnosis.cx), 程序员和作家, Gnosis Software,Inc. 2001 年 12 月 01 日 在 这两篇文章的第一篇中, ...
- linux 异构 计算_在Linux(或异构)网络上共享计算机,第1部分
linux 异构 计算 为了有效地测试和编写各种软件程序,我在本地网络上保留了相当多的计算机. 这些机器运行各种操作系统,并使用各种硬件配置. 有时我正在评估各种平台上的工具: 其他时候我正在测试和调 ...
- linux系统怎么共享网络,在Linux操作系统的网络上共享计算机
比较"安全 shell(SSH)"和"虚拟网络计算(VNC)" 在这两篇文章的第一篇中,David 比较和对照了"安全 shell(SSH)" ...
- 访问网络上共享的打印机每次都需要重新输入用户名密码解决方案
原因没有设置guest权限: 首先打开电脑的控制面板,然后找到"管理工具"选项,点击进入: 2.然后我们需要在管理工具界面找到"本地安全策略"选项,打开: 3. ...
最新文章
- mariadb 内存占用优化
- 线性代数笔记: Cholesky分解
- 速卖通2022新开店入驻流程及费用
- 浙江巨丰管业有限公司网站
- 汉寿县智慧城市建设PPP项目成功签约
- 2016 China Joy抢先看,文末有彩蛋!
- 闲鱼如何高效承接并处理用户纠纷
- Pandas使用DataFrame进行数据分析比赛进阶之路(一)
- 无锡东亭计算机培训班,锡山区东亭办公自动化培训、电脑培训班有哪些?
- 关于行业的浅析以及未来工作的前景初判
- ISP之色差增益抑制(Chroma Gain Suppression)
- 【CMake】CMakeList编写整理
- 淘宝新店如何提升店铺排名
- 从零开始的MySQL数据库三部曲(二、MySQL数据库的创库创表增删改查篇)
- Python与Matlab算法学习一文通(快速排序算法)(更新中)
- 当面试问到自己有哪些缺点应该怎么回答
- google不同步书签
- NYOJ 304 节能(DP)
- 2级c语言高级应用程序,全国计算机等级考试二级教程-C语言程序设计.pdf
- 十大远古神秘失落文明