Adding a printer automatically

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.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you have the .inf file already,

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

should do it.
HOWTO: Add a Network Printer Connection;en-us;147202
trizzagAuthor Commented:
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
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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.
trizzagAuthor Commented:
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?
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.
trizzagAuthor Commented:

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?
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial

 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?
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

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.