Solved

Adding a printer automatically

Posted on 2003-11-07
10
25,374 Views
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.
0
Comment
Question by:trizzag
10 Comments
 
LVL 86

Expert Comment

by:jkr
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.
0
 
LVL 23

Expert Comment

by:chensu
ID: 9703406
HOWTO: Add a Network Printer Connection
http://support.microsoft.com/default.aspx?scid=kb;en-us;147202
0
 

Author Comment

by:trizzag
ID: 9703783
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
0
 
LVL 2

Expert Comment

by:gsteup
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.
0
 

Author Comment

by:trizzag
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?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 2

Expert Comment

by:gsteup
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.
0
 

Author Comment

by:trizzag
ID: 9764576
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?
0
 
LVL 2

Accepted Solution

by:
gsteup earned 500 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.
0
 

Expert Comment

by:stephen79217
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?
0
 

Expert Comment

by:bisola_4
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

thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article surveys and compares options for encoding and decoding base64 data.  It includes source code in C++ as well as examples of how to use standard Windows API functions for these tasks. We'll look at the algorithms — how encoding and decodi…
As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now