Esempio n. 1
0
        private static bool SendBytesToPrinter(string printerName, IntPtr pBytes, Int32 dwCount)
        {
            var di = new DOCINFOW();

            di.pDocName  = "LABEL";
            di.pDataType = "RAW";

            bool bSuccess = false;

            try
            {
                IntPtr hPrinter = IntPtr.Zero;
                if (OpenPrinter(printerName, ref hPrinter, IntPtr.Zero))
                {
                    if (StartDocPrinter(hPrinter, 1, ref di))
                    {
                        if (StartPagePrinter(hPrinter))
                        {
                            Int32 dwWritten = 0;
                            bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
                            EndPagePrinter(hPrinter);
                        }
                        EndDocPrinter(hPrinter);
                    }
                    ClosePrinter(hPrinter);
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Print failed.", ex);
            }
            return(bSuccess);
        }
Esempio n. 2
0
        public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
            IntPtr   hPrinter = System.IntPtr.Zero;
            Int32    dwError;
            DOCINFOW di        = new DOCINFOW();
            Int32    dwWritten = 0;
            bool     bSuccess;

            di.pDocName  = "My Document";
            di.pDataType = "RAW";
            bSuccess     = false;
            if (OpenPrinter(szPrinterName, ref hPrinter, 0))
            {
                if (StartDocPrinter(hPrinter, 1, ref di))
                {
                    if (StartPagePrinter(hPrinter))
                    {
                        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref dwWritten);
                        EndPagePrinter(hPrinter);
                    }
                    EndDocPrinter(hPrinter);
                }
                ClosePrinter(hPrinter);
            }
            if (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
            }
            return(bSuccess);
        }
Esempio n. 3
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 or False on failure.
        public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
            IntPtr   hPrinter  = System.IntPtr.Zero; // The printer handle.
            Int32    dwError   = 0;                  // Last error - in case there was trouble.
            DOCINFOW di        = new DOCINFOW();     // Describes your document (name, port, data type).
            Int32    dwWritten = 0;                  // The number of bytes written by WritePrinter().
            bool     bSuccess  = false;              // Your success code.

            // Set up the DOCINFO structure.
            di.pDocName  = "LPN Label";
            di.pDataType = "RAW";
            // Assume failure unless you specifically succeed.
            if (OpenPrinter(szPrinterName, ref hPrinter, 0))
            {
                if (StartDocPrinter(hPrinter, 1, ref di))
                {
                    if (StartPagePrinter(hPrinter))
                    {
                        // Write your printer-specific bytes to the printer.
                        bSuccess = WritePrinter(hPrinter, pBytes, dwCount, ref 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);
        }         // SendBytesToPrinter()
Esempio n. 4
0
        public static bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, int dwCount)
        {
            DOCINFOW docinfow = new DOCINFOW();
            IntPtr   ptr      = new IntPtr();

            docinfow.pDocName  = "My Visual Basic .NET RAW Document";
            docinfow.pDataType = "RAW";
            bool flag = false;

            if (OpenPrinter(szPrinterName, ref ptr, 0))
            {
                if (StartDocPrinter(ptr, 1, ref docinfow))
                {
                    if (StartPagePrinter(ptr))
                    {
                        int num2 = 0;
                        flag = WritePrinter(ptr, pBytes, dwCount, ref num2);
                        EndPagePrinter(ptr);
                    }
                    EndDocPrinter(ptr);
                }
                ClosePrinter(ptr);
            }
            if (!flag)
            {
                int lastError = GetLastError();
            }
            return(flag);
        }
Esempio n. 5
0
 private static extern bool StartDocPrinter(IntPtr hPrinter, int level, ref DOCINFOW pDI);
Esempio n. 6
0
 public extern static bool StartDocPrinter(IntPtr hPrinter, Int32 level, ref DOCINFOW pDI);