예제 #1
0
        // SendBytesToPrinter()
        // 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.
        public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
            Int32  dwError = 0, dwWritten = 0;
            IntPtr hPrinter = new IntPtr(0);

            NativeMethods.DOCINFOA di = new NativeMethods.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);
                }

                // If you did not succeed, GetLastError may give more information
                // about why not.
                if (bSuccess == false)
                {
                    dwError = Marshal.GetLastWin32Error();
                }
                ClosePrinter(hPrinter);
            }
            return(bSuccess);
        }
예제 #2
0
 public static bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] NativeMethods.DOCINFOA di)
 {
     return(NativeMethods.StartDocPrinter(hPrinter, level, di));
 }