Beispiel #1
0
        public static bool ReadFromPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
        {
            // Leia os dados da impressora.
            Int32  dwError = 0, dwBytesRead = 0;
            IntPtr hPrinter = new IntPtr(0);

            NativeMethods.DOCINFOA         di = new NativeMethods.DOCINFOA();
            NativeMethods.PRINTER_DEFAULTS pd = new NativeMethods.PRINTER_DEFAULTS();
            pd.DesiredAccess = 0x00000020;

            bool bSuccess = false;

            // Abra a impressora.
            if (NativeMethods.OpenPrinter2(szPrinterName.Normalize(), out hPrinter, pd))
            {
                // Start a document.
                if (NativeMethods.StartDocPrinter(hPrinter, 1, di))
                {
                    // Iniciar um documento.
                    if (NativeMethods.StartPagePrinter(hPrinter))
                    {
                        // leia seus bytes.
                        bSuccess = NativeMethods.ReadPrinter(hPrinter, out pBytes, dwCount, out dwBytesRead);

                        // Se você não teve sucesso, GetLastError pode fornecer mais informações
                        // sobre por que não.
                        if (bSuccess == false)
                        {
                            dwError = Marshal.GetLastWin32Error();
                        }

                        NativeMethods.EndPagePrinter(hPrinter);
                    }
                    NativeMethods.EndDocPrinter(hPrinter);
                }
                NativeMethods.ClosePrinter(hPrinter);
            }

            return(bSuccess);
        }
Beispiel #2
0
        /// <summary>
        ///  Envie dados não gerenciados para a impressora de destino.
        ///  Quando a função recebe um nome de impressora e uma matriz não gerenciada de bytes,
        ///  a função envia esses bytes para a fila de impressão. Retorna verdadeiro em sucesso, falso em falha.
        /// </summary>
        /// <param name="szPrinterName">Nome da impressora</param>
        /// <param name="pBytes">Pointer to data</param>
        /// <param name="dwCount">Comprimento de dados em bytes</param>
        /// <returns>bool</returns>
        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; // Suponha falha, a menos que você tenha sucesso especificamente.

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

            // Abra a impressora.
            if (NativeMethods.OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
            {
                // Iniciar um documento.
                if (NativeMethods.StartDocPrinter(hPrinter, 1, di))
                {
                    // Inicia uma página.
                    if (NativeMethods.StartPagePrinter(hPrinter))
                    {
                        bSuccess = NativeMethods.WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
                        NativeMethods.EndPagePrinter(hPrinter);
                    }
                    NativeMethods.EndDocPrinter(hPrinter);
                }
                NativeMethods.ClosePrinter(hPrinter);
            }

            // Se você não teve sucesso, GetLastError pode fornecer mais informações
            // sobre por que não.
            if (bSuccess == false)
            {
                dwError = Marshal.GetLastWin32Error();
            }
            return(bSuccess);
        }