title: SafeFileHandle is invalid, Print to Parallel port C# ASP.NET
body: <p>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 <a href="https://stackoverflow.com/questions/4692937/send-print-commands-directly-to-lpt-parallel-port-using-c-net/30647048">Send print commands directly to LPT parallel port using C#.net</a>. 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.</p>
  
  <p>could anyone assist in helping me figure out where the issue is?</p>
  
  <pre><code>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;
          }
      }
  }
  </code></pre>