Hugo Balthazar Hugo Balthazar - 5 months ago 24
Java Question

Configuring a printer's redirect port via prompt

I've been trying to create an installer for a program I wrote in Java, implementing GhostScript, that creates a virtual printer where the file is sent to. My program then reads the files and manages it accordingly. However, I had to manually configure the RedMon Redirect Port (RPT1:) and manually created a new printer, using that port, taking as arguments the .jar file:

Arguments configured on the printer's port:

Arguments configured on the printer's port

I was able to create a new printer via NSIS (the program I'm using to create the installer) like so:

Exec 'cscript "C:\Windows\System32\Printing_Admin_Scripts\pt-BR\prnport.vbs" ^ -a -r RPT1:'
ExecWait 'rundll32 printui.dll,PrintUIEntry /if /f "$INSTDIR\ghostpdf.inf"^ /r "RPT1:" /m "Ghostscript PDF" /b "Impressora SPE" /u /Y'

Is there a way to do the same, via commands, for configuring ports? If possible, I'd like to create a separate port for my program (currently I'm using the default RedMon port), but also pass the Java arguments to it on installation, so the end user doesn't have to manually include these arguments. I've looked everywhere, but every guide and article I found had it done manually.

Thanks for any help!

Answer Source

It took me a bit of digging, but I finally found the command required to setup a new printer port using RedMon, and figured I'd share it here so less people have a hard time finding it.

rundll32 redmon32.dll, RedMonConfigurePort Port="PORT:" Command="JAVA" Arguments="-jar ""JAR"" \""C:\Program Files (x86)\gs\gs9.21\bin\gswin32c -l \""C:\Program Files (x86)\gs\gs9.21\lib\"" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sPAPERSIZE=a4 -r300"""

Where PORT is your port's name, JAVA is your path to the java.exe, and JAR is the path to your .jar file. DO keep the double quotes, it's how we pass quotes via commands. You must run this command on the RedMon installation folder, though. Also, you MUST run as an administrator, otherwise you might get an error and wrongly assume you did something wrong. In my case, using NSIS, this is the full command line:

ExecWait '"$INSTDIR\redmon" rundll32 redmon32.dll, RedMonConfigurePort Port="SPE:" Command="C:\Program Files (x86)\Impressora SPE\java.exe" Arguments="-jar ""C:\Program Files\Impressora SPE\ImpressoraSPE.jar"" \""C:\Program Files (x86)\gs\gs9.21\bin\gswin32c -l \""C:\Program Files (x86)\gs\gs9.21\lib\"" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sPAPERSIZE=a4 -r300"""'

After that, you can install your new printer using the new port normally:

ExecWait 'cscript "C:\Windows\System32\Printing_Admin_Scripts\en-US\prnport.vbs" ^ -a -r PORT:'
ExecWait 'rundll32 printui.dll,PrintUIEntry /if /f "$INSTDIR\ghostpdf.inf"^ /r "PORT:" /m "Ghostscript PDF" /b "PRINTER_NAME" /u /Y'`

Notice that your path to prnport.vbs might not be "en-US" depending on your language (in my case, it was "pt-BR"). If you're trying to run this from the command prompt and not NSIS, all you gotta do is remove the "ExecWait" bit and the single quotes around the command. Also replace "$INSTDIR\something" by the corresponding full path.

If anyone else needs further help with this subject, seeing that I personally had quite a hard time with it, feel free to ask them here and I'll try my best to help!