0

I am trying to send data to the parallel port 1 that is hooked up to a Zebra printer. the code i have is based off of Send print commands directly to LPT parallel port using C#.net. Everything seems to be working fine with no errors until it gets to "if (!fh.IsInvalid)" which is returning IsInvalid as true. all code is in a class file and is referenced from the main web app via Print2LPT.Print(). I am attempting to send Zebra commands to a Zebra printer that is hooked up to a Parallel port and assigned to LPT1.

could anyone assist in helping me figure out where the issue is?

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;
        }
    }
}
Ingram Yates
  • 89
  • 1
  • 10
  • Try using GetLastError . You will come to know more specific details. – Pavan Chandaka Feb 14 '18 at 18:07
  • According to the [docs](https://msdn.microsoft.com/en-us/library/microsoft.win32.safehandles.safefilehandle%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396), you can marshal the last win32 error to get more info as to why it's invalid. – Chris Feb 14 '18 at 18:07

1 Answers1

1

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

"From Windows 2000 onwards support for writing and reading to the lpts ports no longer exists.

There is a third party dll that you can use for this purpose. "inpout32.dll"

Also referenced; Cannot open LPT1(Printer Port) on win7 (64bit). The same applicaion works on win XP

which offers some workaronds.

PhillipH
  • 6,182
  • 1
  • 15
  • 25