Send raw data to printer using C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • harini19
    New Member
    • Apr 2012
    • 2

    Send raw data to printer using C#

    I have implemented the code to send raw data to printer using the below KB article.
    http://support.microso ft.com/?kbid=322091

    The receipt printer works for small strings but when I send large string data (250 lines), not all the information is printed correctly. Has anyone run into this issue before.

    Please help !!
  • RhysW
    New Member
    • Mar 2012
    • 70

    #2
    when you say not printed correctly what do you mean? is it not being printed? being printed but in the wrong place?, being printed off the edge of the paper? the more information you give the faster you can get help!

    Comment

    • harini19
      New Member
      • Apr 2012
      • 2

      #3
      I meant that not all the data is printed. Some information is missing. For eg. - If I am printing an array of 100 items, then the receipt prints first 30-40 lines then it jumps of to printing last few records.

      I am using bool SendStringToPri nter( string szPrinterName, string szString ) function.

      I had debugged to find if complete string is passed to the above procedure call and it is correct. How can I find if correct byte value is passed when the below function is called.

      pBytes = Marshal.StringT oCoTaskMemAnsi( szString);

      Thanks for your help!!

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        I use raw printer all the time without any trouble, but i made a few changes.
        [code=c#]
        // SendBytesToPrin ter()
        // When the function is given a printer name and an unmanaged array
        // of bytes, the function sends those bytes to the print queue.
        // Returns true on success, false on failure.
        private static bool SendBytesToPrin ter(string DocName, string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
        Int32 dwWritten = 0;
        IntPtr hPrinter = new IntPtr(0);
        DOCINFOA di = new DOCINFOA();
        bool bSuccess = false; // Assume failure unless you specifically succeed.

        LastErrorNumber = 0;

        di.pDocName = DocName;// "My C#.NET RAW Document";
        di.pDataType = "RAW";

        // Open the printer.
        if (OpenPrinter(sz PrinterName.Nor malize(), out hPrinter, IntPtr.Zero))
        {
        // Start a document.
        if (StartDocPrinte r(hPrinter, 1, di))
        {
        // Start a page.
        if (StartPagePrint er(hPrinter))
        {
        // Write your bytes.
        bSuccess = WritePrinter(hP rinter, pBytes, dwCount, out dwWritten);
        EndPagePrinter( hPrinter);
        }
        EndDocPrinter(h Printer);
        }
        ClosePrinter(hP rinter);
        }
        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (bSuccess == false)
        {
        LastErrorNumber = Marshal.GetLast Win32Error();
        }
        return bSuccess;
        }
        public static bool SendBytesToPrin ter(string DocName, string szPrinterName, byte[] data)
        {
        bool retval = false;
        IntPtr pUnmanagedBytes = Marshal.AllocCo TaskMem(data.Le ngth);//Allocate unmanaged memory
        Marshal.Copy(da ta, 0, pUnmanagedBytes , data.Length);//Copy bytes into unmanaged memory
        retval = SendBytesToPrin ter(DocName, szPrinterName, pUnmanagedBytes , data.Length);//Send bytes to printer
        Marshal.FreeCoT askMem(pUnmanag edBytes);// Free the allocated unmanaged memory
        return retval;
        }
        [/code]
        Using the sendbytes is my prefered way, but if you want a nice wrapper function for your strings:
        [code=C#]
        public static bool SendASCIStringT oPrinter(string DocName, string szPrinterName, string data)
        {//Using ASCII encoding might be wrong, you could try UTF8 if you're not happy with ASCII
        return SendBytesToPrin ter(DocName, szPrinterName, Encoding.ASCII. GetBytes(data)) ;
        }
        [/code]
        Last edited by Plater; Apr 27 '12, 03:26 PM.

        Comment

        Working...