Here is some sample c# code to print directly to a ZPL or EPL printer
using System;
using System.IO;
using System.Runtime.InteropServices;
namespace CourierLib
{
/// <summary>
/// Summary description for ZPLPrint.
/// </summary>
public class ZPLPrint
{
public readonly int GENERIC_WRITE = 1073741824;
public readonly int OPEN_EXISTING = 3;
public readonly int FILE_SHARE_WRITE = 2;
public string LPTPORT;
[DllImport("kernel32.dll", SetLastError=true)]
public static extern IntPtr CreateFile(
string lpFileName,
EFileAccess dwDesiredAccess,
EFileShare dwShareMode,
IntPtr lpSecurityAttributes,
ECreationDisposition dwCreationDisposition,
EFileAttributes dwFlagsAndAttributes,
IntPtr hTemplateFile);
[System.Runtime.InteropServices.DllImport("kernel32",
EntryPoint="CloseHan
dle")]
public static extern int CloseHandle(int hObject);
int retval;
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES
{
private int nLength;
private int lpSecurityDescriptor;
private int bInheritHandle;
}
[Flags]
public enum EFileAccess : uint
{
GenericRead = 0x80000000,
GenericWrite = 0x40000000,
GenericExecute = 0x20000000,
GenericAll = 0x10000000,
}
[Flags]
public enum EFileShare : uint
{
None = 0x00000000,
Read = 0x00000001,
Write = 0x00000002,
Delete = 0x00000004,
}
public enum ECreationDisposition : uint
{
New = 1,
CreateAlways = 2,
OpenExisting = 3,
OpenAlways = 4,
TruncateExisting = 5,
}
[Flags]
public enum EFileAttributes : uint
{
Readonly = 0x00000001,
Hidden = 0x00000002,
System = 0x00000004,
Directory = 0x00000010,
Archive = 0x00000020,
Device = 0x00000040,
Normal = 0x00000080,
Temporary = 0x00000100,
SparseFile = 0x00000200,
ReparsePoint = 0x00000400,
Compressed = 0x00000800,
Offline= 0x00001000,
NotContentIndexed = 0x00002000,
Encrypted = 0x00004000,
Write_Through = 0x80000000,
Overlapped = 0x40000000,
NoBuffering = 0x20000000,
RandomAccess = 0x10000000,
SequentialScan = 0x08000000,
DeleteOnClose = 0x04000000,
BackupSemantics = 0x02000000,
PosixSemantics = 0x01000000,
OpenReparsePoint = 0x00200000,
OpenNoRecall = 0x00100000,
FirstPipeInstance = 0x00080000
}
public ZPLPrint()
{
//
// TODO: Add constructor logic here
//
}
public void print(string strPrnPath, string sText)
{
FileStream outFile;
IntPtr hPortP;
int hPort;
LPTPORT = strPrnPath;
hPortP = CreateFile(LPTPORT, EFileAccess.GenericWrite,
EFileShare.Write,
IntPtr.Zero, ECreationDisposition.OpenExisting, EFileAttributes.Normal,
IntP
tr.Zero);
hPort = (int)hPortP;
outFile = new FileStream(hPortP, FileAccess.Write, false);
StreamWriter fileWriter = new StreamWriter(outFile);
StreamReader fileReader = new
StreamReader("H:\\VSNet\\CourierLib\\Parcel
ForceLabelDef.txt");
fileWriter.Write("^XA^FO50,50^A030,30^FD " + sText + "^FS");
fileWriter.Write(Formatting.Chr(34));
fileWriter.Write("^FO50,100^A030,30^FD " + sText + "^FS^XZ");
fileWriter.Write(Formatting.Chr(13));
fileWriter.Write(Formatting.Chr(10));
fileWriter.Flush();
fileWriter.Close();
outFile.Close();
retval = CloseHandle(hPort);
}
}
}