Skip to content

Commit

Permalink
The print redirection has been enhanced to be more resilient.
Browse files Browse the repository at this point in the history
  • Loading branch information
gerrit-amagno committed Aug 27, 2024
1 parent 09c3054 commit 700446f
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 9 deletions.
27 changes: 23 additions & 4 deletions Agent/AmagnoVirtualPrinter.Delivery/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +37,26 @@ private static void Main([CanBeNull]string[] args)
}
}

// ReSharper disable once UnusedMember.Local
/// <summary>
/// Can be used to debug the redirection
/// </summary>
/// <param name="printerName"></param>
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}'!");
Expand Down
6 changes: 5 additions & 1 deletion Agent/AmagnoVirtualPrinter.Delivery/Redirector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ public static void RedirectToPrinter([NotNull]string filePath, [NotNull]string p
ghostScriptRedirector.Redirect(file);
}

/// <summary>
/// Check if the printer exists
/// </summary>
/// <param name="printerName">e.g. "Dell C2665dnf Color MFP"</param>
/// <returns>True, if the printer exists</returns>
public static bool PrinterExists(string printerName)
{
// e.g. "Dell C2665dnf Color MFP"
using (var localSystem = new LocalPrintServer())
{
using (localSystem.GetPrintQueue(printerName))
Expand Down
14 changes: 14 additions & 0 deletions AmagnoVirtualPrinter.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 24 additions & 1 deletion UI/AmagnoVirtualPrinter.ProgressInfo/ProgressForm.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,7 +16,8 @@ public partial class ProgressForm : Form
public ProgressForm()
{
InitializeComponent();

LoadImages();

var client = new NamedPipeClient<Core.Message.Message>(PipeName)
{
AutoReconnect = true
Expand All @@ -23,6 +27,25 @@ public ProgressForm()
client.Start(new TimeSpan(TimeSpan.TicksPerMinute));
}

/// <summary>
/// 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.
/// </summary>
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<Core.Message.Message, Core.Message.Message> connection, [NotNull]Core.Message.Message message)
{
if (message == null)
Expand Down

0 comments on commit 700446f

Please sign in to comment.