问题
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:
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!
回答1:
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 "REDMONPATH\redmon32.dll", RedMonConfigurePort Port="PORT:" Command="JAVA" Arguments="-jar ""JAR"" ""C:\Program Files (x86)\gs\gs9.21\bin\gswin32c -I\""C:\Program Files (x86)\gs\gs9.21\lib\"" -dSAFER -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sPAPERSIZE=a4 -r300"""
Where REDMONPATH is the path to your RedMon installation folder, 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 'rundll32 "$INSTDIR\redmon\redmon32.dll", RedMonConfigurePort Port="SPE:" Command="C:\Program Files (x86)\Java\jre1.8.0_144\bin\java.exe" Arguments="-jar ""C:\Program Files (x86)\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 'rundll32 printui.dll,PrintUIEntry /if /f "$INSTDIR\ghostpdf.inf" /r "PORT:" /m "Ghostscript PDF" /b "PRINTER_NAME" /u /Y'`
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!
EDIT: I apologize for the constant edits, just making sure to leave the instructions that help the most people as I test these in different version of Windows.
来源:https://stackoverflow.com/questions/45598535/configuring-a-printers-redirect-port-via-prompt