Text line order is changed when printed through parallel printer

0

Text line order is changed when printed to the parallel printer This is for Windows 10, running C#2.0

//PortType enum
//struct for PORT_INFO_2
[StructLayout(LayoutKind.Sequential)]
public struct PORT_INFO_2
{
    public string pPortName;
    public string pMonitorName;
    public string pDescription;
    public PortType fPortType;
    internal int Reserved;
}

[Flags]
public enum PortType : int
{
    write = 0x1,
    read = 0x2,
    redirected = 0x4,
    net_attached = 0x8
}

//Win32 API
[DllImport("winspool.drv", EntryPoint = "EnumPortsA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int EnumPorts(string pName, int Level, IntPtr lpbPorts, int cbBuf, ref int pcbNeeded, ref int pcReturned);

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 int PrintText(string parallelport, string receiptText, Int16 nLen, Encoding encoding)
{
    IntPtr ptr = CreateFile(parallelport, GENERIC_WRITE, 0,
             IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);
    /* Is bad handle? INVALID_HANDLE_VALUE */
    if (ptr.ToInt32() == -1)
    {
        /* ask the framework to marshall the win32 error code to an exception */
        //                Marshal.ThrowExceptionForHR(Marshal.GetHRForLastWin32Error());
        return (-1);
    }

    FileStream lpt = new FileStream(ptr, FileAccess.ReadWrite);
    Byte[] buffer = new Byte[2048];

    //Check to see if your printer support ASCII encoding or Unicode.
    //If unicode is supported, use the following:
    if (encoding == Encoding.Unicode)
    {
        buffer = System.Text.Encoding.Unicode.GetBytes(receiptText);
    }
    else if (encoding == Encoding.ASCII)
    {
        buffer = System.Text.Encoding.ASCII.GetBytes(receiptText);
    }
    lpt.Write(buffer, 0, buffer.Length);
    lpt.Close();
    return (0);
}

(Calling part)

ParallelOutput po = new ParallelOutput();
po.PrintText("LPT1", strPrintText, Convert.ToInt16(strPrintText.Length), Encoding.ASCII);

This module works fine when save as a file.
However the line order is changed when print to a parallel printer.
For example, if following string is passed to the parallelport,

AAAA
BBBBBBBB
CCCCCCC
DDD

Then the output result is always like following.

BBBBBBBB
CCCCCCC
DDDAAAA

c#
printing
parallel-processing
asked on Stack Overflow Apr 9, 2019 by Don • edited Dec 24, 2019 by Ravi Makwana

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0