SafeFileHandle无效,打印到并行端口C#ASP.NET



我试图将数据发送到连接到斑马打印机的并行端口1。我拥有的代码基于直接使用C#.NET直接发送给LPT并行端口的代码。一切似乎都可以正常工作而没有错误,直到"如果(!fh.isinvalid("为true返回isInvalid。所有代码都在类文件中,并通过print2lpt.print((从主Web应用程序引用。我试图将斑马命令发送到连接到并行端口并分配给LPT1的斑马打印机。

任何人都可以帮助我弄清楚问题在哪里?

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;
        }
    }
}

根据https://social.msdn.microsoft.com/forums/forums/vstudio/en-us/d19392c9-69f2-417d-a137d-a137-0a137-0a3e0a3e0d5777751b/readfilewritefilefilefilefilefilefilefilefilefilefilefilefilefilefilefilefilefilefor-for-for-lptx-isnt-Working?forum = vcgeneral

"从Windows 2000开始,向LPT端口写作和阅读的支持不再存在。

有一个第三方DLL您可以用于此目的。" inpout32.dll"

也引用了;无法在Win7(64位(上打开LPT1(打印机端口(。win xp

的同一应用程序作品

提供了一些Workaronds。

最新更新