Rick Strahl's Weblog  

Wind, waves, code and everything in between...
.NET • C# • Markdown • WPF • All Things Web
Contact   •   Articles   •   Products   •   Support   •   Advertise
Sponsored by:
West Wind WebSurge - Rest Client and Http Load Testing for Windows

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


:P
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


 

Rick Strahl
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

Rick Strahl
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.



Rick Strahl's Web Log
September 28, 2006

# Understanding ASP.NET Impersonation Security - Rick Strahl's Web Log

Understanding how ASP.NET's internal security works is important if your application needs to access resources on the local machine. Specifically it's important to know exactly which account your ASP.NET application is running under. This entry reviews different ways of how this account is affected by different versions of Windows, and ASP.NET configuration.

Rick Strahl
October 05, 2006

# Understanding ASP.NET Impersonation Security - Rick Strahl

Understanding how ASP.NET's internal security works is important if your application needs to access resources on the local machine. Specifically it's important to know exactly which account your ASP.NET application is running under. This entry reviews different ways of how this account is affected by different versions of Windows, and ASP.NET configuration.


November 13, 2006

# SourceForge.net:

The world's largest development and download repository of Open Source code and applications

Gary Cox
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.

Rick Strahl's Web Log
July 06, 2007

# Programmatic Printer Driver Installation re-visited for Vista - Rick Strahl's Web Log

A long while back I posted an entry regarding how to install a printer driver programmatically by shelling out and using rundll32.exe to execute the PrintUi.dll which can handle a variety of print tasks including installing a printer driver. This code's been working fine, but it's been giving...

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

clone dvd
April 18, 2008

# clone dvd

Load balancing solutions for servers optimize the operations that fall within the design and parameters of the server. The solutions ensure the availability of quality application twenty four hours a day, seven days a week. The solutions ensure efficient and timely web traffic distribution. They also take care of prioritization and user specific intrusion prevention that is multi- gigabit in nature.

# dvd decrypter free download

One thing that people in internet publishing need to watch is Google and it’ s interest in the world of mobile technology. Google recently purchased Jaiku. Jaiku is a social platform very similar to Twitter, but even more focused on the cell phones. Google’ s purchase of Jaiku, as well as talk of the upcoming Gphone, all point to the fact that Google is showing strong interest in entering the mobile market. Why? Because of mobile advertising. Google gives a lot of stuff away because they can make a...

decrypt a dvd
April 30, 2008

# decrypt a dvd

A Trojan is a malicious and destructive code that is embedded in what appears to be legitimate or harmless software, but then it hijacks your computer and quickly spreads to do it’ s damage, such as ruining the files on your hard disk. A common and most treacherous type of Trojan is a program that claims to remove viruses and spyware from your computer but instead installs the spyware onto your computer. A Trojan is similar to a Backdoor and may be widely redistributed as part of a computer virus.

dvd burning software
May 01, 2008

# dvd burning software

Wubi lets you install a complete, fully working Ubuntu system into a standard file held inside your Windows file system. After installing and rebooting it will add a new‘ Ubuntu’ entry to the Windows boot menu, choosing this will boot your new Ubuntu system. No partitions to setup, no Grub or messing with the windows boot loader. If you decide you want to remove Ubuntu you simply run the uninstaller in Windows and away it goes!

dvd decoders downloads
May 01, 2008

# dvd decoders downloads

1. Make sure your PC is compatible with the hardware you’ re considering. This is perhaps the most critical part of buying new hardware. If you’ re in the market for a new video card, know if your computer has AGP or PCI- Express when you shop for your card. Nothing is worse than getting home with a nice new video card only to find out you have a different slot. Also keep in mind power requirements. If you didn’ t build your PC, you probably aren’ t aware of your power supply outage unless you’ ve had to...

shrink dvd
May 02, 2008

# shrink dvd

If you use your PC for work, don’ t use it for play, or allow family members to use it for play. Personal experiences show that the most common ways viruses, spyware, and other bad stuff gets on people’ s PC’ s is through“ play” stuff. This includes downloaded screensavers. Screensavers are NOT SAFE. This includes MP3 downloading programs, like Kazaa, Gator, and ALMOST ALL OTHERS. This includes“ Joke” animations that friends appear to send to you. This basically includes almost anything you might consider...

dvd x copy
June 10, 2008

# dvd x copy

My husband’ s computer wouldn’ t boot: Blue Screen of Death. The error was in a file SR. SYS but I couldn’ t boot to get to it to copy a good one over the bad one. Enter Bart’ s Preinstalled Environment Bootable Windows CD/ DVD. I didn’ t have a bootable Windows CD (stupid Dell) but I followed the simple instructions to make a bootable CD. I booted his PC from the new CD. The desktop was uber weird , but I was able to get to his c:\\ drive and copy the bad file from his i386 folder to\\ windows\\ system32\\ drivers.

record from dvd
June 27, 2008

# record from dvd

“ There are a few things to look at when your computer is running slow. some can be hardware and some can be software, and some can be the person who is sitting at your desk right now. Let’ s go ahead and check a few things. I find it easier to start with the cheapest and most simple checks, so we are not doing a bunch of useless work and not paying bundles for something that could have really been inexpensive.

Registry Cleaner
July 16, 2008

# Registry Cleaner

To address this issue on a machine running IE 6 SP1 with our most recent security update, locate and delete this entire key from the registry of the affected machine: HKEY_ CLASSES_ ROOT\\ CLSID\\{ c90250f3- 4d7d- 4991- 9b69- a5c5bc1c2ae6}. If you are running IE7 Beta1 in a side- by- side scenario with another version of IE, this is not a supported scenario. Please uninstall and reinstall IE7 Beta1 in the recommended manner.

dvd coping software
July 17, 2008

# dvd coping software

I can\'t post pictures of it right now (computer problems), hee hee, so it will truly be a Surprise and you\'ll just have to hope you like it.

dvd ripper confronto
July 17, 2008

# dvd ripper confronto

Further, the default installs take considerably different amounts of RAM to run. XP takes 43. 15MB to run, Vista takes 235MB ( five times as much). And that’ s not including the system cache. In total, on a default install, XP consumes 207MB and Vista consumes 770MB (nearly four times as much). I realize that Vista is considerably newer, and therefore designed for a different caliber of machine, but come on. Seriously, this is ridiculous. You can’ t justify taking more than a half gigabyte of RAM when just...

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"

West Wind  © Rick Strahl, West Wind Technologies, 2005 - 2024