/// <summary> /// Sends the specified raw string to the printer. /// </summary> /// <param name="printerName"> /// The name of the printer which receives the string. /// </param> /// <param name="valueToSend"> /// The string to send. /// </param> /// <param name="documentName"> /// The document name to use, if any. /// </param> /// <exception cref="ArgumentNullException"> /// <para><paramref name="printerName"/> is <see langword="null"/> or <see cref="string.Empty"/>.</para> /// <para>-or-</para> /// <para><paramref name="valueToSend"/> is <see langword="null"/> or <see cref="string.Empty"/>.</para> /// </exception> /// <exception cref="ArgumentException"> /// <para>The specified printer was not found.</para> /// </exception> /// <exception cref="Win32Exception"> /// There was an error sending the data to the printer. /// </exception> public static void SendStringToPrinter(string printerName, string valueToSend, string documentName) { if (string.IsNullOrWhiteSpace(printerName)) { throw new ArgumentNullException(nameof(printerName)); } if (string.IsNullOrEmpty(valueToSend)) { throw new ArgumentNullException(nameof(valueToSend)); } using (var printer = SafePrinterHandle.Open(printerName)) using (printer.StartDocument(documentName)) using (printer.StartPage()) { printer.Write(valueToSend); } }
/// <summary> /// Initializes all fields in the print job monitor. /// </summary> private void Initialize() { // In the event that the monitor is started while the spooler is offline, // keep retrying until the spooler becomes available. SafePrinterHandle hPrinter = null; while (true) { try { void action() => hPrinter = OpenPrinter(null); Retry.WhileThrowing <Win32Exception>(action, 5, TimeSpan.FromSeconds(30)); break; } catch (Win32Exception ex) { switch (ex.NativeErrorCode) { // Ignore these errors. case RPC_S_SERVER_UNAVAILABLE: case RPC_S_CALL_FAILED: case ERROR_INVALID_PRINTER_NAME: Thread.Sleep(TimeSpan.FromSeconds(1)); break; default: throw; } } } _options = CreatePrinterNotifyOptions(); SafePrinterChangeNotificationHandle hChange = FindFirstPrinterChangeNotification(hPrinter, PrinterChanges.Job, _options); _hPrinters.Add(hPrinter); _hChanges.Add(hChange); }
internal static extern SafePrinterChangeNotificationHandle FindFirstPrinterChangeNotification(SafePrinterHandle hPrinter, uint printerChanges, uint fdwOptions, ref PrinterNotifyOptions pPrinterNotifyOptions);
internal static extern bool OpenPrinter(string pPrinterName, out SafePrinterHandle phPrinter, IntPtr pDefault);
/// <summary> /// Creates a change notification object and returns a handle to the object. /// </summary> /// <param name="hPrinter">A handle to the printer or print server to monitor.</param> /// <param name="filter">The <see cref="PrinterChanges" /> that should trigger new events.</param> /// <param name="options">The <see cref="PrinterNotifyOptions" /> that dictate which notifications should be sent.</param> /// <returns>A handle to a change notification object associated with the specified printer or print server.</returns> /// <exception cref="Win32Exception">The underlying operation failed.</exception> private static SafePrinterChangeNotificationHandle FindFirstPrinterChangeNotification(SafePrinterHandle hPrinter, PrinterChanges filter, PrinterNotifyOptions options) { uint fdwOptions = 0; // Must always be zero for 2D printers. SafePrinterChangeNotificationHandle result = NativeMethods.FindFirstPrinterChangeNotification(hPrinter, (uint)filter, fdwOptions, ref options); if (Marshal.GetLastWin32Error() > 0) { throw new Win32Exception(); } return(result); }
private static extern bool WritePrinter(SafePrinterHandle hPrinter, [MarshalAs(UnmanagedType.LPStr)] string data, int dwCount, out int dwWritten);
private static extern bool EndPagePrinter(SafePrinterHandle hPrinter);
private static extern bool StartPagePrinter(SafePrinterHandle hPrinter);
private static extern bool StartDocPrinter(SafePrinterHandle hPrinter, int level, ref DOCINFO di);
private static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPWStr)] string szPrinter, out SafePrinterHandle hPrinter, IntPtr pd);
public PrinterPage(SafePrinterHandle printer) { _printer = printer; }
public PrinterDocument(SafePrinterHandle printer) { _printer = printer; }