1. using  System;   
  2. using  System.Collections.Generic;   
  3. using  System.Text;   
  4. using  System.Runtime.InteropServices;   

  5. namespace  Barcode_Print   
  6. {
  7.   /// <summary>
  8.   /// 本类使用说明:
  9.   /// 将一行ZPL指令作为string参数传给write函数即可
  10.   /// </summary>
  11.   class LPTControl
  12.   {
  13.       [StructLayout(LayoutKind.Sequential)]
  14.       private struct OVERLAPPED
  15.       {
  16.           int Internal;
  17.           int InternalHigh;
  18.           int Offset;
  19.           int OffSetHigh;
  20.           int hEvent;
  21.       }

  22.       [DllImport("kernel32.dll")]
  23.       private static extern int CreateFile(
  24.       string lpFileName,
  25.       uint dwDesiredAccess,
  26.       int dwShareMode,
  27.       int lpSecurityAttributes,
  28.       int dwCreationDisposition,
  29.       int dwFlagsAndAttributes,
  30.       int hTemplateFile
  31.       );


  32.       [DllImport("kernel32.dll")]
  33.       private static extern bool WriteFile(
  34.       int hFile,
  35.       byte[] lpBuffer,
  36.       int nNumberOfBytesToWrite,
  37.       out   int lpNumberOfBytesWritten,
  38.       out   OVERLAPPED lpOverlapped
  39.       );


  40.       [DllImport("kernel32.dll")]
  41.       private static extern bool CloseHandle(
  42.       int hObject
  43.       );



  44.       private int iHandle;
  45.       public bool Open()
  46.       {
  47.           iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
  48.           if (iHandle != -1)
  49.           {
  50.               return true;
  51.           }
  52.           else
  53.           {
  54.               return false;
  55.           }

  56.       }


  57.       public bool Write(String Mystring)
  58.       {


  59.           if (iHandle != -1)
  60.           {
  61.               int i;
  62.               OVERLAPPED x;
  63.               byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
  64.               return WriteFile(iHandle, mybyte, mybyte.Length, out   i, out   x);
  65.           }
  66.           else
  67.           {
  68.               throw new Exception("端口未打开!");
  69.           }
  70.       }


  71.       public bool Close()
  72.       {
  73.           return CloseHandle(iHandle);
  74.       }
  75.   }

  76. }  

  77.       [DllImport("kernel32.dll")]
  78.       private static extern int CreateFile(
  79.       string lpFileName,
  80.       uint dwDesiredAccess,
  81.       int dwShareMode,
  82.       int lpSecurityAttributes,
  83.       int dwCreationDisposition,
  84.       int dwFlagsAndAttributes,
  85.       int hTemplateFile
  86.       );


  87.       [DllImport("kernel32.dll")]
  88.       private static extern bool WriteFile(
  89.       int hFile,
  90.       byte[] lpBuffer,
  91.       int nNumberOfBytesToWrite,
  92.       out   int lpNumberOfBytesWritten,
  93.       out   OVERLAPPED lpOverlapped
  94.       );


  95.       [DllImport("kernel32.dll")]
  96.       private static extern bool CloseHandle(
  97.       int hObject
  98.       );



  99.       private int iHandle;
  100.       public bool Open()
  101.       {
  102.           iHandle = CreateFile("lpt1", 0x40000000, 0, 0, 3, 0, 0);
  103.           if (iHandle != -1)
  104.           {
  105.               return true;
  106.           }
  107.           else
  108.           {
  109.               return false;
  110.           }

  111.       }


  112.       public bool Write(String Mystring)
  113.       {


  114.           if (iHandle != -1)
  115.           {
  116.               int i;
  117.               OVERLAPPED x;
  118.               byte[] mybyte = System.Text.Encoding.Default.GetBytes(Mystring);
  119.               return WriteFile(iHandle, mybyte, mybyte.Length, out   i, out   x);
  120.           }
  121.           else
  122.           {
  123.               throw new Exception("端口未打开!");
  124.           }
  125.       }


  126.       public bool Close()
  127.       {
  128.           return CloseHandle(iHandle);
  129.       }
  130.   }

  131. }  
  132. 暂时无法判断打印机状态,如缺纸,打印机未开。