West Wind Hero Image

Rick Strahl's Weblog

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All things Web
Contact   •   Articles   •   Products   •   Support   •  
Sponsored by:
Markdown Monster - The Markdown Editor for Windows
On this page:
As part of West Wind Web Connection I have a few samples that generate PDF documents automatically. Although there are a number of different ways to generate PDF output, the one most people use ends up using either Distiller or Ghostscript from a Post script file.

In order to do this though it's a requirement that you actually have a PostScript printer installed on your machine and this becomes an installation hassle. So, it would be nice to automate this process and install a printer driver programmatically. While programmatically is stretching this solution a bit, it turns out that Windows has a utility that allows automating many of the printer installation tasks through the PrintUi.dll utility.

You can run:

rundll32 printui.dll,PrintUIEntry /? 

To get the full functionality listing which includes adding and deleting printers both through the Wizards or through non-interactive installs. You can install printers locally or over the network, display status, print test pages, bring up property pages and more.

I ran into a Web board post here that describes some of the options and provides a few examples that make for a great quick start:
http://www.codeguru.com/forum/showthread.php?t=220033&goto=nextoldest

There's also a TechNet entry here:
http://support.microsoft.com/kb/q189105/

For my purpose I want to install a generic PostScript driver for which I've used the Apple Color LW 12/660 PS because it's one that's available on most Windows versions as one of the built in drivers. I was actually thrown by the examples provided in the first article because it takes two steps to register a printer: Load the Printer driver and then install the driver to the local Printers installation.

To register a new printer with the name of Apple Postscript Driver you can use the following (in a batch file or individually):

rundll32 printui.dll,PrintUIEntry /ia /m "Apple Color LW 12/660 PS" /h "Intel" /v "Windows 2000 or XP" /f %windir%\inf\ntprint.inf /u
rundll32 printui.dll,PrintUIEntry /if /b "Apple Postscript Driver" /f "%windir%\inf\ntprint.inf" /r "lpt1:" /m "Apple Color LW 12/660 PS"

Works great and provides minimal fuss. To do this programmatically all you need to do is run a Shell process to execute the above commands or execute a batch file. I created a simple batch file called installprinter.bat that does basically this:

rundll32 printui.dll,PrintUIEntry /ia /m "%1" /h "Intel" /v "Windows 2000 or XP" /f %windir%\inf\ntprint.inf /u
rundll32 printui.dll,PrintUIEntry /if /b "%2" /f "%windir%\inf\ntprint.inf" /r "lpt1:" /m "%1"

 Note you need to change the server type for Windows NT or Windows 98 systems with the /v flag. The CodeGuru link has a few examples of this and the /? flag describes the various options available when you run it.

 

Very cool! I'm just bummed it took me so long to find this.


The Voices of Reason


February 05, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

You can also do some of this programmatically with the prnAdmin.dll from the Windows 2003 Server Resource Kit. This DLL must be installed and I'm not sure whether it's redistributable or whether it works.

There are some basic examples of this here:

http://www.jsiinc.com/SUBK/tip5300/rh5379.htm
Steve Roche
March 15, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Hi,
The printer I want to install uses the local port "NUL". It prints to files, not to a printer... Do you know of a way with the printui.dll to create the NUL port if it doesn't already exist?

Please email me at: srochenh@bigfoot.com if you have any recommendations for me.

Thanks,
Steve
Ali
March 21, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Hi,
Its really cool solution for installing the printer,let me check your specified commands and then i'll let you know what happend next:) BTW is it valid for all NT pro systems e.g. w2K , XP , server 2003.

Great solution with great trick!
andrew
June 18, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

I have been working with this utility to install an IP printer remotely on 50

XP machines. My Syntax is...just like MS Article Q314486

rundll32 printui.dll,PrintUIEntry /if /b "DART" /f %windir%\inf\ntprint.inf

/r "IP_10.0.1.254" /m "HP Laserjet 4000 Series PCL" /Z

I have this statement in a BAT file...when I run it...I get the following

error.

"The arguments are invalid"

Any ideas?
JC
July 01, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Your "HP laserjet 4000 Series PCL" must be typed exactly like it is in the inf file. I had this problem too but found that I wasn't typing it like it is in the inf.
Leo
July 14, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

This is definetly a neat trick...but I'm facing problems deleting the printer I installed. I get an errorslike 'Printer is currently in use'(but it isn't!)
Luna
October 17, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Stop the print service and you will not get these errors. Then restart at the end of script
K
October 26, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

I'm trying to push out the Adobe PDF printer so some folks here can save to prn formats and toss these into a Distiller watched folder. Any suggestions since I only have the ppd??
francisco
December 05, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Why doesn't work rundll32 printui.dll ..... in w98
Ramesh kasi
December 22, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Hi

I want to add a fresh printer driver(hp bussiness inkjet
1000 series) not existing .

successfully adding the existing printer driver like hp Lseserjet 2100 By using the printui command .but it is not working in the latest printer

eg:
rundll32 printui.dll,PrintUIEntry /ia /c\\Elnet /m "HP Business Inkjet 1000 Series" /h
"Intel" /v "Windows 2000 or XP" /f %windir%\inf\ntprint.inf /l "C:\hp1000k.inf"


reg
Ramesh.k
k_ramesh_7@sify.com




rancio
December 22, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

What about windows 98??????
rundll32 prinui.dll,PrintUIEntry doesn't work on win98????

please help!!!
jhusted
December 28, 2005

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

I have had success setting up the printer on a remote system for ALL users using
rundll32 printui.dll,PrintUIEntry /in /ga /c\\remotePC /n"\\Server\printer"

BUT

How can you force it to be the default? When I run
rundll32 printui.dll,PrintUIEntry /y /n \\server\printer
from the local pc it will set the default as expected but I am attemping to do it remote. Any ideas?
Thanks!
Zeppo
March 03, 2006

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Hi
This works fine on my local PC but I need to add a virtual printer to my webhosts server. Can I do this in ASP.NET so that it executes the batch file server side?

Regards
Mark Lea
March 03, 2006

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

You can if you have Admin permissions on the server.

To do so you can maybe use Impersonation with teh ASPX in a special folder that is Windows Security protected.

http://west-wind.com/weblog/posts/2153.aspx

AChilli
July 19, 2006

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Karen Tiong
September 18, 2006

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

rundll32 printui.dll,PrintUIEntry /ia /c\\%computername% /m "HP LaserJet 4M Plus" /h "Intel" /v "Windows 2000" /f %windir%\inf\ntprint.inf
rundll32 printui.dll,PrintUIEntry /if /b "HP LaserJet 4M(IT)" /f %windir%\inf\ntprint.inf /r "IP_143.82.141.243" /m "HP LaserJet 4M Plus" /z

I'm using the above commands to automatically install printer driver onto user machine. My question is which file can I edit or configure the "paper size" or printer properties?? Which file can I use or append a command line to set paper size to A4 instead?

eMail me @ karen_tiong@hotmail.com. Thanks for your help.


April 06, 2007

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Thanks Rick this was very helpful. I used this to install the Ghostscript driver and it worked perfectly.
Paulio
April 19, 2007

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

What about win98? Is there anyway to do this with win98. The printui.dll doesn't exist in win98
noname
May 23, 2007

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Can't remove the printer too. If i stop the service with "net stop spooler" than i become the error "the printer can't be deleted. The spooler service is not running now" (or similar, i have translated it from german). If i don't stop the service i became other error about either the printer name is wrong or the printer is not connected to server. Both is not the case - the name is the same from the batch file i have installed the driver with and there is no server either because it is a local printer.
Ric Nagy
July 20, 2007

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Have a question we are about to do a file server rollout. Now the file servers are going to be print servers. What we want to do is take a development box that has no network connection and no printers attached. It is loaded with Windows Server 2003. People here want to use printUI and create a bat so when they get to a site and build the print server all they have to do is click on the batch and it will install any and all necessary print drivers. I have the zip files for all the different printers can printUI be used?

My questions
1) There are going to be network printers, currently don't have names or ip's, does this matter?
2) If printUI. cannot be used is there a way to at least create something to get the proper inf's and print drivers over to folder so at least all the have do is point to the folder
Ajay TS
August 16, 2007

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

What if the driver is not installed on the machine?
In my case the driver for LaserJet 2420 PCL 6 is not installed. I have the drivers in a share. Can i still use this?
Paul Garth
October 13, 2007

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

I'm having a mind block on this...

During which process do you actually download the printdriver to the computer?

The commands appear to reference the printer name.

Thanks.

Paul
Muns
November 22, 2007

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Garth,

The first statement uses the switch "/ia" to install the printer driver, then follow with another (similar) statement using the "/if? switch to actually add the logical printer itself.

By the way, I'm trying to install a no non-WHQL print driver and trying to suppress the prompt that warns me, etc. I've tried using the "/Gw" switch to no avail. Anyone else tried this?

Cheers!
Paul Garth
December 18, 2007

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Hi All:

Please respond to paulgarth@att.net.

OK.... I'm going through some mentalness here.

Scenario:

-Want to use the two RUNDLL32 lines to install a new printer
-Brand new printer and need to download print driver

Question:
-Where do you put the print driver?
-How does /f "%windir%\inf\ntprint.inf" relate to the print driver name?

The objective is to create Kaseya scripts to install a new printer where they also download the new print driver from the server.

Thanks.

Paul
arkiboys
September 09, 2008

# re: Installing a Printer Driver programmatically - sort of - using PrintUi.dll

Hi,
I run this code on myServerName machine without error but no printer(printerName) gets added to the remotePCname.
Any thoughts please?
rundll32 printui.dll,PrintUIEntry /in /ga /c\\reportPCname /n\"\\myServerName\printerName"