简体   繁体   中英

WPF app sending raw data to a printer exception-handling

Using this KB Article as an example: http://support.microsoft.com/kb/322091/en-us ,
I'm trying to send raw data (byte array) to a thermal printer. For this I use the following method from the above article:

public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
    {
        Int32 dwError = 0, dwWritten = 0;
        IntPtr hPrinter = new IntPtr(0);
        DOCINFOA di = new DOCINFOA();
        bool bSuccess = false; // Assume failure unless you specifically succeed.

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

        // Open the printer.
        if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
        {
            // Start a document.
            if (StartDocPrinter(hPrinter, 1, di))
            {
                // Start a page.
                if (StartPagePrinter(hPrinter))
                {
                    // Write your bytes.
                    bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                    EndPagePrinter(hPrinter);
                }
                EndDocPrinter(hPrinter);
            }
            ClosePrinter(hPrinter);
        }
        // If you did not succeed, GetLastError may give more information
        // about why not.
        if (bSuccess == false)
        {
            dwError = Marshal.GetLastWin32Error();
        }
        return bSuccess;
    }

So I send an array:

        bool bSuccess = false;
        ...
        try
        {
            bSuccess = RawPrinterHelper.SendBytesToPrinter(printerName, pUnmanagedBytes, nLength);
        }
        catch (Exception ex)
        {
            failprint = ex.Message;
        }
        if (bSuccess == true)
        {
            MessageBox.Show("Text printed");
        }
        else
        {
            MessageBox.Show("Printing failed: " + failprint);
        }

If the printer is available, all goes ok and I get MessageBox.Show("Text printed"); together with actual printing. But if I turn off the printer (before the attempt to print), I dont get Printing failed MessageBox. Instead the application gets frozen and waits until the printer is turned on, then the queued print job successfully gets printed, application gets unfrozen and I get MessageBox.Show("Text printed") .

Where am I wrong in my attempt to catch the failed print job, without the application getting in frozen, waiting state?

You should run WritePrinter Method asynchronously And then check Marshal.GetLastWin32Error();

This is old school. You might want to use this

// Create the printer server and print queue objects
LocalPrintServer localPrintServer = new LocalPrintServer();
PrintQueue defaultPrintQueue = LocalPrintServer.GetDefaultPrintQueue();

// Call AddJob
PrintSystemJobInfo myPrintJob = defaultPrintQueue.AddJob();

// Write a Byte buffer to the JobStream and close the stream
Stream myStream = myPrintJob.JobStream;
Byte[] myByteBuffer = UnicodeEncoding.Unicode.GetBytes("This is a test string for the print job stream.");
myStream.Write(myByteBuffer, 0, myByteBuffer.Length);
myStream.Close();

source msdn

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM