Пример #1
0
 private void SendBytes(IntPtr pointer, int byteCount)
 {
     if (!RawPrinterAccess.WritePrinter(this.PrinterHandle, pointer, byteCount, out _))
     {
         int    errorCode    = WindowsApi.GetLastErrorCode();
         string errorMessage = WindowsApi.GetErrorMessage(errorCode);
         throw new Win32Exception(errorCode, errorMessage);
     }
 }
Пример #2
0
 private void Dispose(bool disposing)
 {
     if (this.PrinterHandle != IntPtr.Zero)
     {
         RawPrinterAccess.EndPagePrinter(this.PrinterHandle);
         RawPrinterAccess.EndDocPrinter(this.PrinterHandle);
         RawPrinterAccess.ClosePrinter(this.PrinterHandle);
         this.PrinterHandle = IntPtr.Zero;
     }
 }
Пример #3
0
        /// <summary>
        ///     Opens the raw access to the printer and starts printing a new document.
        /// </summary>
        /// <param name="documentName"> The name of the document to print. </param>
        /// <remarks>
        ///     <para>
        ///         <paramref name="documentName" /> is only used for informational purposes, e.g. it is displayed in the Windows
        ///         printer window which lists the document being printed.
        ///     </para>
        ///     <para>
        ///         Printing does not start until <see cref="WriteString(string)" />, <see cref="WriteString(string,Encoding)" />,
        ///         or <see cref="WriteBytes" /> is called.
        ///     </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException"> <paramref name="documentName" /> is null. </exception>
        /// <exception cref="ArgumentException"> <paramref name="documentName" /> is an empty string. </exception>
        /// <exception cref="InvalidOperationException"> The printer is already open. </exception>
        /// <exception cref="Win32Exception"> A system error ocurred while opening the printer or starting a new document. </exception>
        public void Open(string documentName)
        {
            if (documentName == null)
            {
                throw new ArgumentNullException(nameof(documentName));
            }

            if (string.IsNullOrWhiteSpace(documentName))
            {
                throw new ArgumentException("The string is empty.", nameof(documentName));
            }

            if (this.IsOpen)
            {
                throw new InvalidOperationException("The printer is already open.");
            }

            GC.ReRegisterForFinalize(this);

            bool success = false;

            try
            {
                IntPtr hPrinter;

                if (RawPrinterAccess.OpenPrinter(this.PrinterDevice.PrinterName.Normalize(), out hPrinter, IntPtr.Zero))
                {
                    this.PrinterHandle = hPrinter;

                    DOCINFOA di = new DOCINFOA();
                    di.pDocName  = documentName;
                    di.pDataType = RawPrinterAccess.RawDataType;

                    if (RawPrinterAccess.StartDocPrinter(hPrinter, 1, di))
                    {
                        if (RawPrinterAccess.StartPagePrinter(hPrinter))
                        {
                            success = true;
                            return;
                        }
                    }
                }

                int    errorCode    = WindowsApi.GetLastErrorCode();
                string errorMessage = WindowsApi.GetErrorMessage(errorCode);
                throw new Win32Exception(errorCode, errorMessage);
            }
            finally
            {
                if (!success)
                {
                    this.Close();
                }
            }
        }