[英]SafeFileHandle is invalid, Print to Parallel port C# ASP.NET
我正在尝试将数据发送到连接到Zebra打印机的并行端口1。 我拥有的代码基于使用C#.net将打印命令直接发送到LPT并行端口 。 一切正常,直到出现“ if(!fh.IsInvalid)”(返回IsInvalid为true)为止,都没有错误。 所有代码都在类文件中,并通过Print2LPT.Print()从主Web应用程序引用。 我正在尝试将Zebra命令发送到连接到并行端口并分配给LPT1的Zebra打印机。
谁能帮助我弄清楚问题出在哪里?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Win32.SafeHandles;
using System.IO;
using System.Runtime.InteropServices;
namespace Mynamespace
{
public static class Print2LPT
{
[DllImport("kernel32.dll", SetLastError = true)]
static extern SafeFileHandle CreateFile(string lpFileName, FileAccess dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, FileMode dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile);
public static bool Print()
{
bool IsConnected = false;
String Line1 = "^XA";
String Line2 = "^LH9,0";
String Line3= "^FO0,0";
String Line4 = "^GB780,367,10^FS";
String Line5 = "^FO0,152";
String Line6 = "^GB780,0,5^FS";
String Line7 = "^FO456,0";
String Line8 = "^GB0,367,5^FS";
String Line9 = "^FO456,257";
String Line10 = "^GB318,0,5^FS";
String Line11= "^FX add a description";
String Line12 = "^FO25,40";
String Line13 = "^CFD,35,15";
try
{
Byte[] buffer1 = new byte[Line1.Length];
Byte[] buffer2 = new byte[Line2.Length];
Byte[] buffer3 = new byte[Line3.Length];
Byte[] buffer4 = new byte[Line4.Length];
Byte[] buffer5 = new byte[Line5.Length];
Byte[] buffer6 = new byte[Line6.Length];
Byte[] buffer7 = new byte[Line7.Length];
Byte[] buffer8 = new byte[Line8.Length];
Byte[] buffer9 = new byte[Line9.Length];
Byte[] buffer10 = new byte[Line10.Length];
Byte[] buffer11 = new byte[Line11.Length];
Byte[] buffer12 = new byte[Line12.Length];
Byte[] buffer13 = new byte[Line13.Length];
buffer1 = System.Text.Encoding.ASCII.GetBytes("Line1");
buffer2 = System.Text.Encoding.ASCII.GetBytes("Line2");
buffer3 = System.Text.Encoding.ASCII.GetBytes("Line3");
buffer4 = System.Text.Encoding.ASCII.GetBytes("Line4");
buffer5 = System.Text.Encoding.ASCII.GetBytes("Line5");
buffer6 = System.Text.Encoding.ASCII.GetBytes("Line6");
buffer7 = System.Text.Encoding.ASCII.GetBytes("Line7");
buffer8 = System.Text.Encoding.ASCII.GetBytes("Line8");
buffer9 = System.Text.Encoding.ASCII.GetBytes("Line9");
buffer10 = System.Text.Encoding.ASCII.GetBytes("Line10");
buffer11 = System.Text.Encoding.ASCII.GetBytes("Line11");
buffer12 = System.Text.Encoding.ASCII.GetBytes("Line12");
buffer13 = System.Text.Encoding.ASCII.GetBytes("Line13");
SafeFileHandle fh = CreateFile("LPT1:", FileAccess.Write, 0, IntPtr.Zero, FileMode.OpenOrCreate, 0, IntPtr.Zero);
if (!fh.IsInvalid)
{
IsConnected = true;
FileStream lpt1 = new FileStream(fh, FileAccess.ReadWrite);
lpt1.Write(buffer1, 0, buffer1.Length);
lpt1.Write(buffer2, 0, buffer2.Length);
lpt1.Write(buffer3, 0, buffer3.Length);
lpt1.Write(buffer4, 0, buffer4.Length);
lpt1.Write(buffer5, 0, buffer5.Length);
lpt1.Write(buffer6, 0, buffer6.Length);
lpt1.Write(buffer7, 0, buffer7.Length);
lpt1.Write(buffer8, 0, buffer8.Length);
lpt1.Write(buffer9, 0, buffer9.Length);
lpt1.Write(buffer10, 0, buffer10.Length);
lpt1.Write(buffer11, 0, buffer11.Length);
lpt1.Write(buffer12, 0, buffer12.Length);
lpt1.Write(buffer13, 0, buffer13.Length);
lpt1.Close();
}
}
catch (Exception ex)
{
string message = ex.Message;
}
return IsConnected;
}
}
}
“从Windows 2000开始,不再支持对lpts端口的写入和读取。
您可以使用第三方dll来实现此目的。 “inpout32.dll”
也被引用; 无法在Win7(64位)上打开LPT1(打印机端口)。 相同的应用程序适用于Win XP
提供一些工作原理。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.