From 700446fbff4caf8741f5aa3c567d4045da8948ca Mon Sep 17 00:00:00 2001 From: Gerrit Edzards Date: Tue, 27 Aug 2024 12:52:15 +0200 Subject: [PATCH] The print redirection has been enhanced to be more resilient. --- .../AmagnoVirtualPrinter.Delivery/Program.cs | 27 ++++++++++++++++--- .../Redirector.cs | 6 ++++- AmagnoVirtualPrinter.sln | 14 ++++++++++ .../ProgressForm.Designer.cs | 3 --- .../ProgressForm.cs | 25 ++++++++++++++++- 5 files changed, 66 insertions(+), 9 deletions(-) diff --git a/Agent/AmagnoVirtualPrinter.Delivery/Program.cs b/Agent/AmagnoVirtualPrinter.Delivery/Program.cs index b5a895f..1d1c5ba 100644 --- a/Agent/AmagnoVirtualPrinter.Delivery/Program.cs +++ b/Agent/AmagnoVirtualPrinter.Delivery/Program.cs @@ -23,10 +23,9 @@ private static void Main([CanBeNull]string[] args) var filePath = args[1]; var printerName = args[2]; - if (!PrinterExists(printerName)) - { - Console.WriteLine($"Printer {printerName} does not exist!"); - } +#if DEBUG + CheckPrinter(printerName); +#endif RedirectToPrinter(filePath, printerName); break; @@ -38,6 +37,26 @@ private static void Main([CanBeNull]string[] args) } } + // ReSharper disable once UnusedMember.Local + /// + /// Can be used to debug the redirection + /// + /// + private static void CheckPrinter(string printerName) + { + try + { + if (!PrinterExists(printerName)) + { + Console.WriteLine($"Printer {printerName} does not exist!"); + } + } + catch (Exception e) + { + Console.WriteLine("Error checking printer:" + e); + } + } + private static void NotUseful() { throw new ArgumentNullException($"Use '{RedirectCmd}'!"); diff --git a/Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs b/Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs index 2262801..27527d1 100644 --- a/Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs +++ b/Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs @@ -26,9 +26,13 @@ public static void RedirectToPrinter([NotNull]string filePath, [NotNull]string p ghostScriptRedirector.Redirect(file); } + /// + /// Check if the printer exists + /// + /// e.g. "Dell C2665dnf Color MFP" + /// True, if the printer exists public static bool PrinterExists(string printerName) { - // e.g. "Dell C2665dnf Color MFP" using (var localSystem = new LocalPrintServer()) { using (localSystem.GetPrintQueue(printerName)) diff --git a/AmagnoVirtualPrinter.sln b/AmagnoVirtualPrinter.sln index cbf599f..87d5f92 100644 --- a/AmagnoVirtualPrinter.sln +++ b/AmagnoVirtualPrinter.sln @@ -46,6 +46,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AmagnoVirtualPrinter.Utils. EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AmagnoVirtualPrinter.Agent.Lib.Tests", "Tests\AmagnoVirtualPrinter.Agent.Lib.Tests\AmagnoVirtualPrinter.Agent.Lib.Tests.csproj", "{36CD25FD-B1E2-4C87-970F-A40CCC961390}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PrinterDebug.Console", "PrinterDebug.Console\PrinterDebug.Console.csproj", "{74AAF6F3-E538-4295-81A0-57634C547B07}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -248,6 +250,18 @@ Global {36CD25FD-B1E2-4C87-970F-A40CCC961390}.Release|x64.Build.0 = Release|Any CPU {36CD25FD-B1E2-4C87-970F-A40CCC961390}.Release|x86.ActiveCfg = Release|Any CPU {36CD25FD-B1E2-4C87-970F-A40CCC961390}.Release|x86.Build.0 = Release|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Debug|x64.ActiveCfg = Debug|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Debug|x64.Build.0 = Debug|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Debug|x86.ActiveCfg = Debug|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Debug|x86.Build.0 = Debug|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Release|Any CPU.Build.0 = Release|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Release|x64.ActiveCfg = Release|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Release|x64.Build.0 = Release|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Release|x86.ActiveCfg = Release|Any CPU + {74AAF6F3-E538-4295-81A0-57634C547B07}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.Designer.cs b/UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.Designer.cs index 3950f27..cd4cf0e 100644 --- a/UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.Designer.cs +++ b/UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.Designer.cs @@ -28,7 +28,6 @@ protected override void Dispose(bool disposing) /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ProgressForm)); this.lbProgress = new System.Windows.Forms.Label(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit(); @@ -46,7 +45,6 @@ private void InitializeComponent() // // pictureBox1 // - this.pictureBox1.Image = global::AmagnoVirtualPrinter.ProgressInfo.Properties.Resources.waiting; this.pictureBox1.Location = new System.Drawing.Point(50, 12); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(32, 35); @@ -62,7 +60,6 @@ private void InitializeComponent() this.Controls.Add(this.pictureBox1); this.Controls.Add(this.lbProgress); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; - this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "ProgressForm"; diff --git a/UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.cs b/UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.cs index 44d4db7..175174b 100644 --- a/UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.cs +++ b/UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.cs @@ -1,7 +1,10 @@ using NamedPipeWrapper; using System; +using System.ComponentModel; +using System.Drawing; using System.Windows.Forms; using AmagnoVirtualPrinter.ProgressInfo.Core.Message; +using AmagnoVirtualPrinter.ProgressInfo.Properties; using JetBrains.Annotations; namespace AmagnoVirtualPrinter.ProgressInfo @@ -13,7 +16,8 @@ public partial class ProgressForm : Form public ProgressForm() { InitializeComponent(); - + LoadImages(); + var client = new NamedPipeClient(PipeName) { AutoReconnect = true @@ -23,6 +27,25 @@ public ProgressForm() client.Start(new TimeSpan(TimeSpan.TicksPerMinute)); } + /// + /// On some devices, the images cannot be loaded ( probably due to missing permissions). Because + /// the dialog is only displayed for a short time and the important information are displayed as + /// text, the problem can be ignored. + /// + private void LoadImages() + { + try + { + var resources = new ComponentResourceManager(typeof(ProgressForm)); + pictureBox1.Image = Resources.waiting; + Icon = (Icon)resources.GetObject("$this.Icon"); + } + catch (Exception e) + { + Console.WriteLine(e); + } + } + private void ServerMessage(NamedPipeConnection connection, [NotNull]Core.Message.Message message) { if (message == null)