Adding a printer programatically

Giganews Newsgroups
Subject: Adding a printer programatically
Posted by:  Lucian (thanks@nospam.com)
Date: 4 Sep 2006

Hi,

I've been looking for a long time for a solution to this task. So far,
it seems you should be able to do this, following this steps:

1. Call GetPrinterDriverDirectory to retrieve the path of the
printer-driver directory

2. Copy all driver required files to the above path.

3. Setup the parameters and call AddPrinterDriver or
AddPrinterDriverEx. This should install a local or remote printer
driver and link the configuration, data, and driver files.

4. If needed, setup a new port, using AddPortExA

5. Finally, when all the above worked, call AddPrinter

There are various records that need to be set when calling those APIs.
However, my problem is ... (2). How do I know what files to copy?

Practically, I install several new OS-es on few computers and I need to
stress a printer monitoring application. My biggest time wasting
operation seems to be installing the printer drivers. I know I can
install them from batch files, using "rundll32 printui.dll...". That
works perfectly fine for me. However, even that is wasting me a lot of
time because I have to manually edit a huge batch file (I am talking
about installing up to 1000 printers).

So I have a database with all the printers I need and with simple
queryies I would want to install say only the HPs.

I've googled for this problem and only found hardcoded samples (I mean
pieces of code where file names and paths are hardcoded.)

The only known things are:
* printer name (i.e. "HP LaserJet 4Si")
* path to where the windows files are (the setup CD is copied on a hard
drive)
* GetSystemDirectory
* GetWindowsDirectory
* GetPrinterDriverDirectory

In short, I need to write equivalent Delphi code for this command:

rundll32 printui.dll, PrintUIEntry /if /b "PRNT001" /Z /cPRNT001 /m "HP
LaserJet 4Si" /h "Intel" /v "Windows 2000" /f "W:\2000 Advanced
Server\I386\
tprint.inf" /l "W:\2000 Advanced Server\I386" /r "NUL1"

Any help appreciated.

Lucian

Replies