簡體   English   中英

SafeFileHandle無效,打印到並行端口C#ASP.NET

[英]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;
        }
    }
}

根據https://social.msdn.microsoft.com/Forums/vstudio/en-US/d19392c9-69f2-417d-a137-0a3e0d57751b/readfilewritefile-for-lptx-isnt-working?forum=vcgeneral

“從Windows 2000開始,不再支持對lpts端口的寫入和讀取。

您可以使用第三方dll來實現此目的。 “inpout32.dll”

也被引用; 無法在Win7(64位)上打開LPT1(打印機端口)。 相同的應用程序適用於Win XP

提供一些工作原理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM