如何用C#向LPT1送一条切纸指令给打印机,假设我有EC

本人菜鸟一只,请各位大虾前来相助,多谢~能有源代码和解释就最好了
当前问题共有如下(1)个解决方案
  • 沉默术士
    沉默术士

    1. 定义LPT打印类
    public class PrintFactory
    {
    public const short FILE_ATTRIBUTE_NORMAL = 0x80;
    public const short INVALID_HANDLE_VALUE = -1;
    public const uint GENERIC_READ = 0x80000000;
    public const uint GENERIC_WRITE = 0x40000000;
    public const uint CREATE_NEW = 1;
    public const uint CREATE_ALWAYS = 2;
    public const uint OPEN_EXISTING = 3;

    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess,
    uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,
    uint dwFlagsAndAttributes, IntPtr hTemplateFile);

    public static void SendCMDToLPT1( String receiptText )
    {
    IntPtr ptr = CreateFile("LPT1", GENERIC_WRITE, 0,
    IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

    if (ptr.ToInt32() == -1)
    {
    Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
    }
    else
    {
    FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite);
    Byte[] buffer = System.Text.Encoding.Default.GetBytes(receiptText);
    lpt.Write(buffer, 0, buffer.Length);
    lpt.Close();
    }
    }
    }

    2. 调用
    PrintFactory.SendCMDToLPT1("ECS指令");

上一篇:ecs是什么专业美国大学
下一篇:ATECS是什么牌子