Running SumatraPDF from windows service

I can use Sumatra to print PDF from a windows service but the printer must be installed for the user the service is running as. If the user running the service does not have access to the printer, nothing will print.
To test you may log into the machine and go browse to the printer on the network, double click it to install the printer, now start the Sumatra service again, it will now print.
Dim printer_name As String = $"""{oPick.GetPickPDFPrinterName()}"""
Dim startInfo As System.Diagnostics.ProcessStartInfo = New ProcessStartInfo()
startInfo.FileName = IO.Path.Combine(My.Application.Info.DirectoryPath, "SumatraPDF.exe")
startInfo.Arguments = $"-print-to {printer_name} {FilenameToPrint} -exit-on-print -silent"

startInfo.RedirectStandardError = True
startInfo.RedirectStandardOutput = True
startInfo.UseShellExecute = False

startInfo.CreateNoWindow = True
startInfo.WindowStyle = ProcessWindowStyle.Hidden
Using proc As System.Diagnostics.Process = Process.Start(startInfo)
Dim soutput As String = proc.StandardOutput.ReadToEnd()
Dim eoutput As String = proc.StandardError.ReadToEnd()

Dim ReturnCode As Integer = 0
If proc.WaitForExit(15000) Then
If proc.ExitCode = 0 Then
Throw New Exception($"SumatraPDF.exe returned a non-zero return code,…
End If
Debug.Write($"Sending pick PDF to printer possibly failed,SumatraPDF.exe timed out..
End If
End Using

Prevent “Service has been marked for deletion” while installing windows services

A reminder, if the above message is encountered when installing a windows service after removing a previous version, then

  • Close all instances of Event Log viewer
  • Close all instances of Services MMC (Microsoft Management Console) for all users on server
  • Use task manager to ensure the old service process is not running


  • Sign other users out of server if above fails

Since understanding the above the message no longer haunts me.


Stackoverflow post: How to solve “The specified service has been marked for deletion” error

Exception.ToString() and capturing inner exceptions

It is often worth going back to basics, it turns out quite a few people don’t realise that calling “.ToString” on an exception object in .NET, will provide a nicely formatted output suitable for logging. It will also recursively extract the inner exceptions, if there are any too.  It includes the stack trace. I’ve seen quite a few times now code that replicates the same functionality as this built in function. Here is the description from MSDN.

The default implementation of ToString obtains the name of the class that threw the
current exception, the message, the result of calling ToString on the inner exception,
and the result of calling Environment.StackTrace.
If any of these members is null, its value is not included in the returned string.

[MSDNException.ToString Method ()] 

This is quite helpful as it means no traversing the exception objects extracting the inner exceptions, if they exist as its already implemented in this method.

Fix sending graphics to Zebra Thermal Printer with GW command -corrupts images

Attempting to print from the RPi over serial RS232 interface using the GW command, I was getting image corruption. Some of the image would appear to wrap, or the image would just not print unless some extra line feeds were added to the end of the command. This was even though the number of bytes sent was correct.

After blaming my code for dithering the image for three hours, I finally found the issue. I had checked through each printer software switch and setting, wasting much time tinkering with form size, thinking it was the cause of picture wrapping. I finally looked at Windows Mode (notice how W is towards back of the book). This was set to Y (YES), looking at the description in the EPL programming guide, it said it all…

The Windows mode escape sequences are only used by the optional Windows printer driver. When working with a main frame or other non-Windows host, this mode can be disabled to prevent erratic operation.

As I was using RS232 on Windows IoT core – I think this counts as requiring a setting of “NO”.

So sending the EPL command “WN” did the trick. No more odd behaviour sending the binary for the PCX image. I’ve seen on many forums other people struggling and giving up on sending images via GW, hopefully this post may give some another thing to check. Do comment if it helped you!