Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programatically Add Printer

Status
Not open for further replies.

ChrisRChamberlain

Programmer
Mar 23, 2000
3,392
GB
The theory -

If you knew all the registry keys and values for a specific printer, (common to all Win O/S), and were to write all those keys and values to the relevant registries, would you be programatically adding/installing the afore mentioned printer?

Anyone been there and got the tee-shirt? HTH

Chris [pc2]
 
Yuk. Sounds like a better project would be to launch a script to run the printers own install.
Dave S.
 
Dave

A programatic install would be preferable to launching the unsuspecting user on a conventional install, even though this method could be supported from a documentation point of view.

Once the printer is installed - yes, you can verify the registry keys and values and correct any that are wrong.

The question remains - could it be done as originally described or is there something missing? HTH

Chris [pc2]
 
what I see as the main problem is that each printer install may have different register entries. It could have more or less then what is expected. How would you get all the possible settings, even if each printer setup used the exact same register entries? Attitude is Everything
 
danceman

You could divide the O/S into two camps theoretically, Win 9X/ME and NT/2000/XP.

There may be differences within the camps, but the Locations/Keys/Values should be similar, (locations being the wrong expression, I can't remember the correct one, but I'm sure you get my drift).

If it could work on one O/S, then logically it could work on any O/S, assuming you could locate the relevant data.

To obtain that data, you could 'Add Printer' in the conventional manner, use RegEdit or some other similar tool, note the relevant values and then delete the new printer.

Then you could write the registry keys and values to hopefully add the printer?

The theory sounds promising - I'm waiting for someone to say "It won't work because...", which, negative as it may sound, would be useful in that it stops one from chasing one's own tail as we say on this side of the pond. HTH

Chris [pc2]
 
ChrisRChamberlain

I did a litle research and came up with

AddPrinterDriver API call.

it requires 3 parameters

LPTSTR pName, // address of server name
DWORD Level, // printer info. structure level
LPBYTE pDriverInfo / address of printer info. structure

the structure to pass LPBYTE is
DWORD cVersion;
LPTSTR pName;
LPTSTR pEnvironment;
LPTSTR pDriverPath;
LPTSTR pDataFile;
LPTSTR pConfigFile;

I have passed structures before, if you can't get more detailed info in this I can email it to you.

You have gotten my curosity up, If I wasn't so busy
I would like to work this out.
Attitude is Everything
 
Correct me if I'm wrong, but I think a print driver needs to be installed locally in order to print, even if it's a network printer. So, although you might be able to duplicate the reg entries for the printer, the driver itself would still need to be installed on the client. Having said that, I assume that since you know the printer reg entries, you also know the required driver, and it probably wouldn't be a big deal to copy it over.
 
Well, if you already know the required registry entries, you could maybe put them into a .REG file and add it using command line REGEDIT, or WSH. Dave S.
 
As was suggested, one problem with this method, is the printer drivers. There are at least 3 potentially different drivers required for even a single printer to support "all" the OSs (95/98/98SE/ME/NT/2000/XP). That's why many "old" printers don't function (correctly) under newer OSs, and new printers won't work on older OSs - the manufacturers aren't interested in writing more drivers than they think they need to.

Rick
 
Thanks to all for the responses

The AddPrinter() API call is documented in the Microsoft Platform SDK, and apparently is a known method of installing a printer driver.

I say 'apparently' because the one and only reference I have been able to find at MSDN or anywhere else points you to the SDK.

There are no knowledgebase articles to support its use, and there are no indicators as to where in the SDK it might be.

If anyone has any ideas, I would be grateful. HTH

Chris [pc2]
 
We are using an NT network and it's possible to add an existing network printer to the client with these commands:


declare integer AddPrinterConnection in winspool.drv string
res=1-addprinterconnection('\\servername\printername')
if res<>0
=messagebox('Add printer failed',16,'Error')
endif
return res


of course you should replace the \\servername\printername with the correct names.

It's also possible to remove a printer with something like:
declare integer DeletePrinterConnection in winspool.drv string

res=1-deleteprinterconnection('\\servername\printername')

 
ChrisRChamberlain, do you have the MSDN cd's?

you can find samples in C and info on the function.

if not leave me your email address, I can help with some resources. Attitude is Everything
 
Jack

Unless I'm misunderstanding your code, it is creating a network connection to a printer that has already been added but has no network connection.

What I'm after is the means to programatically add a printer, either by AddPrinter() or some other means.

danceman

The name of the function AddPrinterDriver() suggests that it is adding a driver to an existing installed printer, not simply adding a printer as in AddPrinter()?

Have since found documentation on the functions in the MSDN CD, (surprising how you miss the obvious!), but there are no examples to evaluate. HTH

Chris [pc2]
 
from the documentation I have read it looks like this will install a printer in network or local.

there was a prameter that is a structure that held the data for the install. If you don't have this info I can email it to you. Attitude is Everything
 
danceman

Your right about what the function can do

The AddPrinterDriver function installs a local or remote printer driver and links the configuration, data, and driver files.

For more flexibility in installing or upgrading printer drivers, use the AddPrinterDriverEx function becaue it allows strict upgrade, strict downgrade, copying of newer files only, and copying of all files (regardless of the file time stamps).

BOOL AddPrinterDriver(
LPTSTR pName, // server name
DWORD Level, // driver information level
LPBYTE pDriverInfo // driver information buffer
);

Email address is chrisrchamberlainATyahooDOTcoDOTuk HTH

Chris [pc2]
 
If you have the MSDN cd's then you have the info on the friver info structure. there is also info in the cd's with samples in passing structures.

basic is to find out the size of each element of the structure and position the data in a string in its correct position. knowing that each char is one byte.

it has been over a year since I have done any of this, if you can't find any samples, I can email you some. Attitude is Everything
 
danceman

Have yet to find the samples and do not wish to re-invent the wheel on this if you've been there already.

Would appreciate any samples you might be able to find.


While you're at it, how about writing a FAQ titled

'How to programmatically add a printer'

or similar? [smarty] HTH

Chris [pc2]
 
ChrisRChamberlain

soory, I havn't done it. I havn't had the need, you know the old saying, need is the other of invention.

besides the MSDN cd's, I have an old winapi help file. I find it handy in it helps with discribing the data structures. another help is that the visual studio cd has all the include files for defining constants.

If there is something that I can pass on to you then ost email address. Attitude is Everything
 
danceman

I basically need the WinAPI declaration for :-

AddPrinterDriver(
LPTSTR pName, // server name
DWORD Level, // driver information level
LPBYTE pDriverInfo // driver information buffer
);

As I understand it

pName should be .NULL., (local printer)
Level will 2,3,4 or 6 depending on O/S
pDriverInfo will be...?

typedef struct _DRIVER_INFO_2 {
[tab]DWORD cVersion;
[tab]LPTSTR pName;
[tab]LPTSTR pEnvironment;
[tab]LPTSTR pDriverPath;
[tab]LPTSTR pDataFile;
[tab]LPTSTR pConfigFile;
} DRIVER_INFO_2, *PDRIVER_INFO_2;

Email address is chrisrchamberlainATyahooDOTcoDOTuk

TIA HTH

Chris [pc2]
 

declare integer AddPrinterDriver in winspool.drv string,integer,string

declare integer AddPrinterDriver in winspool.drv string cServer,integer COS, string cDrvInfo

Looking forward to see this posted, good luck. Attitude is Everything
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top