Link to home
Start Free TrialLog in
Avatar of trizzag
trizzag

asked on

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.
Avatar of jkr
jkr
Flag of Germany image

If you have the .inf file already,

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

should do it.
Avatar of trizzag
trizzag

ASKER

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

chensu,
This code is for network printers, this printer needs to be added as a local printer
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.
Avatar of trizzag

ASKER

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.
Avatar of trizzag

ASKER

gsetup,

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?
ASKER CERTIFIED SOLUTION
Avatar of gsteup
gsteup

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start 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

thanks