Add Customized Printer and Launch Application Using Microsoft's prnadmin.dll

Added by Citrix CDN , last edited by Citrix CDN on Nov 06, 2007  (view change)
Tags: 

Description

This is an example of how to connect a networked print queue, set it as the default, set printer settings, launch Notepad then remove the printer connection upon application exit.

Script Version

1.0

How to Use

This script requires that prnadmin.dll, from the Windows 2003 Resource kit, be registered on the machine you run the script on. For more examples and further information, see the prnadmin.doc which is included with the Windows 2003 Resource kit.

Windows Server 2003 Resource Kit Tools

The Echo statements provide an easy way to step through the script and can be commented out when the script is complete.

Paste the script into Notepad and save as fileName .vbs. Double-click the file. You can also run the file from the command prompt with the following command:
Cscript filename .vbs

Code Snippet

dim oMaster,filename,Flags,PrinterName
Set objShell = CreateObject("Wscript.Shell")
Set objNetwork = Wscript.CreateObject("WScript.Network")
set oMaster = CreateObject("PrintMaster.PrintMaster.1")
'PRNadmin.dll Whitepaper page 12 outlines the different flags available.
Flags=16

'Previously saved dat file containing the printer settings.
Filename="c:\rotated.dat"

PrinterName="\\printsvr\AGFA-AccuSetSF v52.3" 'Necessary for the restore

'Adds a printer connection to a print server not a local printer
Wscript.Echo "Adding Printer"
objNetwork.AddWindowsPrinterConnection ("\\printsvr\agfa")

'Valid for local or remote printers
Wscript.Echo "setting the default printer"
objNetwork.SetDefaultPrinter("\\printsvr\agfa")

' The driver name, not share name, needed for the restore to function correctly
Wscript.Echo "Restoring printer settings from c:\rotated.dat"
oMaster.PrinterPersistRestore PrinterName, FileName, Flags


Wscript.echo "Launching the application"
objShell.Run ("notepad.exe c:\labs\L4test.txt"),1,True

Wscript.Echo "now removing printer"
objNetwork.RemovePrinterConnection ("\\printsvr\AGFA")

Disclaimer

These software applications are provided to you as is with no representations, warranties or conditions of any kind. You may use and distribute it at your own risk. CITRIX DISCLAIMS ALL WARRANTIES WHATSOEVER, EXPRESS, IMPLIED, WRITTEN, ORAL OR STATUTORY, INCLUDING WITHOUT LIMITATION WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NONINFRINGEMENT. Without limiting the generality of the foregoing, you acknowledge and agree that (a) the software application may exhibit errors, design flaws or other problems, possibly resulting in loss of data or damage to property; (b) it may not be possible to make the software application fully functional; and (c) Citrix may, without notice or liability to you, cease to make available the current version and/or any future versions of the software application. In no event should the code be used to support of ultra-hazardous activities, including but not limited to life support or blasting activities. NEITHER CITRIX NOR ITS AFFILIATES OR AGENTS WILL BE LIABLE, UNDER BREACH OF CONTRACT OR ANY OTHER THEORY OF LIABILITY, FOR ANY DAMAGES WHATSOEVER ARISING FROM USE OF THE SOFTWARE APPLICATION, INCLUDING WITHOUT LIMITATION DIRECT, SPECIAL, INCIDENTAL, PUNITIVE, CONSEQUENTIAL OR OTHER DAMAGES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. You agree to indemnify and defend Citrix against any and all claims arising from your use, modification or distribution of the code.