[Last Call] Learn how to a build a cloud-first strategyRegister Now


Adding a printer automatically

Posted on 2003-11-07
Medium Priority
Last Modified: 2013-12-03
I am trying to write an application that, given an inf file, driver, and necessary info, would install the driver and add a printer automatically to any windows system without going through the Add Printer Wizard and without any user intervention.  I have scoured the web for any applications, scripts or windows commands that could do this but have found nothing.  I am able to do this in Windows XP/2k by using the shell command:

rundll32 printui.dll,PrintUIEntry /if /b "VirtualPrinter" /f ".\lj456p5.inf" /q /r "FILE:" /m "HP Laserjet 5" /z /u

but it wont work in NT, 95, and 98.

  I mostly program in VB, VB.NET and Java, but if anyone knows a way to do this please let me know.
Question by:trizzag
LVL 86

Expert Comment

ID: 9703350
If you have the .inf file already,

ShellExecute ( NULL, "open", "lj456p5.inf", NULL, NULL, SW_HIDE); // C code, sorry :o)

should do it.
LVL 23

Expert Comment

ID: 9703406
HOWTO: Add a Network Printer Connection

Author Comment

ID: 9703783
It seems that this command only installs the driver on the system, but does not add the actual printer

This code is for network printers, this printer needs to be added as a local printer
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments


Expert Comment

ID: 9714575
Hmm, why don't you want to use the PnP event? I give you a good reason, why you should want to do so:

Even if you install the printer driver somehow on the system, you will still get a PnP event if: you are using USB connection, Bluetooth connection, ... Almost no printer (except for some lasers) uses the LPT port anymore, but either USB, Bluetooth (in conjuction with USB) or are networked.

In these cases you will always get at least one PnP event, probably two, one for the USB printing support, one for the printer, if the printer has multiple interfaces (like scanner, printer, HID) you get five events (Composite device, HID, scan, usb printing, printer). Even with LPT you will end up with some of them. They are needed to install the necessary system drivers and there is no way you can install all this by hand because the mechanisms are not documented. On top Windows 98 does not even come with those preinstalled. You would have to write and kick off an inf file prior to attaching the device, otherwise it would not even communicate.

You would have to write your own printer driver installer (the ones from printer manufacturers usally use the PnP event themselves)  and it would probably be very limited to one port and possibly even just one printer model/class. (many printer manufactures put special information in the inf file that does not get picked up by the PnP install but is used later on by a custom installer to do some tweaking or customizing of printer settings, you would miss all that with a self written installer). Considering that printer man. have whole teams of full time programmers working on those things it is probably not worth the effort for you.

There is one cheating way around this if you want to put up with PnP: Every time Windows detects a new piece of hardware it check if a driver for this one is present somewhere on the system. Where? The path to the locations is saved in the registry [HKLM\Software\Microsoft\Windows\CurrentVersion] DevicePath. It is semicolon delimited list of locations. Save your driver to a standard location  and add this location to the registry value. It has to point to the inf file. It works great in 2k/XP because it lets Windows do all the work .
It may work ok in Win98/ME (depends on the printer inf file, if it handles the port installation or not). You cannot create a new location in Win9x but have to copy the inf files into the existing one. Even then it may not work, Win98 does not come with USB files (hidclass.sys, usbprint.sys, ...) preinstalled, they are either on the Win98 CD or on the printer CD. But as I said you could create a USB preinstaller and kick that one off before you plug in your printer. (if it is a USB printer)

So to me this is the cleanest way, because it lets Windows create and install the drivers and settings it needs, that are device dependend and port dependend and that you are not able to provide by using printui.dll (which is just a remote control of the APW gui). It is used by all major PC makers (HP,Dell, Gateway, ...) and has been tested thoroughly for years.

If you want to go through the trouble and write a custom printer installer yourself you will have to use a huge amount of API calls (and you won't be able to do this in VB) A barrage of SetupAPI calls, in conjunction with special spooler/print provider calls (described in the DDK) is needed. It would be way too much to describe in this forum and would take you a considerable time to sort through.

Sorry for being so long winded but this is a very complicated topic. Let me know if you have any more questions.

Author Comment

ID: 9715620
I actually need to add a virtual printer to the system.  There will not be a physical printer connected to the system.  It will use either the ":FILE"  port or another port that I will create myself.  The purpose of this is the virtual printer will capture the pcl data  when a user prints from any windows app and write it to a file.  Sorry, I should have mentioned that in the first post, but in this case it seems it might not make a difference.  Does anyone know of a way to make this work?

Expert Comment

ID: 9716173
Sorry for the confusion. I am not aware of a way to remotely control the APW functions in Windows 98.

Unless someone else knows one you have two choices:

1. Use a script generator to create a GUI automation script (like AutoIT or VisualTest). This is a quick and dirty way to do.

2. As I mentioned earlier, write an installer that process the inf file. the apropriate API's are SetupDi... and AddPrintProcessor, AddPrinterDriverEx, AddPrinterEx.

I am not sure if you can link all these calls into VB, I wouldn't recommend it anyways. There is a large chapter about the SetupDi calls in the Windows DDK and a long chapter about the AddPrinter... calls inside the SDK. It is too much to paste in here and there is quite an amount that can differ from driver to driver. So you would have to read up on it.

The basic outline is this:
- Install and copy all the driver files by using the inf file (using SetupDi... calls, don't worry you won't have to write a complete inf parser, most of it is done by Windows)
- Register PrintProcessor, PrinterDriver with the system (using AddPrinterDriverEx and AddPrintProcessor calls)
- Create the printer object using AddPrinter and assign it the correct settings, port and all that good stuff.
- If needed call DocumentProperties to customize further settings.

It is basically writing your own custom APW that you could control without user intervention
Quite a lengthy process, but if it is worth for you going down that path I would recommend this way.

Author Comment

ID: 9764576

Thanks for all the good input!  I got this to work using the automation script, but I'd rather not do this the quick and dirty way.  I tried to find some information on using the api calls online and couldn't find too much.  I think I will have to use C++ for this because VB doesnt support many of these calls.  Could you please point me in the right direction to find some more referenece material and info on how to write an APW?

Accepted Solution

gsteup earned 2000 total points
ID: 9765483
Sure, the best way to go is by reading the SDK. Look at the section called "Graphics and Multimedia Services/Windows GDI/Printing and Print Spooler"
This section will tell you about AddPrinter, AddPrinterDriverEx, ... all those printer APIs.

Another source you need is the DDK. If you dont' have it, you can order it from MS for $15 S+H. If you are a MDSN subscriber you can just download it there of course. The DDK explains the usage of the general installation interface (called the SetupAPI). Those calls all start with SetupDi (there is also a ConfigManager API, but most of those calls are now obsolete). those calls handle the inf file and do the primary install processes. This whole subject can be very confusing and overwhelming in the beginning, but once you get the hang of it it is quite simple, just a lot of code writing involved before you get where you want to be.

The best way to go is doing some trial and error programming, start with something easy (like listing all the devices in your PC), a project like an APW is not the right place to start, but that is just me.

Also the SDK and the DDK are not tutorials, so don't expect some howto's, it is more like MS mumbojumbo that you need to translate into common language first :)

And finally, yep you definitely need to write this in C/C++. VB is okay for whipping up some quick GUI, but that is about it. You are talking about system programming now.

Let me know if you need more help with the APIs (like opeing another question if needed) and I am sure there are some people here that can help you.

Expert Comment

ID: 11218521

 what if i want to install network printer with a driver installation wizard
 i find this command in 2000

 RUNDLL32 PRINTUI.DLL,PrintUIEntry /in /n\\server\printer

 but how to do in 98?

Expert Comment

ID: 11441932
I have the same problem to, trying to automatically install a modem without showing the Add Modem dialog, in  the end, i have been pointed to the Windows DDK, i am not a C++ programmer, but I need this to work, otherwise it is installing over 70 modems manually

Good luck, but i will keep an eye on this thread, hope u do not mind


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question