Tech Support > Microsoft Windows > Drivers > Install Printer-Driver / Plugin programatically
Install Printer-Driver / Plugin programatically
Posted by Tobias Erichsen on April 19th, 2005


Hi everyone,

based on the OEMUNI sample in the XP DDK, I built a fax-printer-driver.
This works pretty nice.

Now I need to include this driver in the installation of our product. I'd
like
to do this using the AddPrinterDriver / AddPrinter APIs.

In which way do I have to initialize the DriverInfo structure for the
AddPrinterDriver to install this?

Best regards,
Tobias


Posted by Jürgen Hollfelder on April 19th, 2005


It is a bit tricky and you have to carefully read the SDK. Here is the
source from my routine I am using. Actually there is some more error
handling and some Debug macros in the real version but I removed it here to
keep it to the point.

DWORD Level = 1;
char *pDirInfo = NULL;
DWORD cbBuf = 0;
DWORD pcbNeeded;
// Detect the right directory
GetPrinterDriverDirectory(NULL,NULL,Level,(LPBYTE) pDirInfo,0,&pcbNeeded);
pDirInfo = new char[1+pcbNeeded/sizeof(char)+50];
cbBuf = sizeof(char)*(1+pcbNeeded/sizeof(char)+50);
GetPrinterDriverDirectory(NULL,NULL,Level,(LPBYTE) pDirInfo,cbBuf,&pcbNeeded);

// I do not remember whether I really needed that. But I kept it in my
routine
if (pDirInfo[strlen(pDirInfo)-1]!='\\') {
pDirInfo=(char *)realloc(pDirInfo,strlen(pDirInfo)+1);
strcat(pDirInfo,"\\");
}

// DrvCopy is a small function that does the filecopy, from is "" so it is
the current directory. During the test I copied it from all the places it
was compiled to
CString from="";
CString to=pDirInfo;
DrvCopy("UNIDRV.DLL",from,to); //pDriverPath
DrvCopy("UNIDRVUI.DLL",from,to); //pConfigFile
DrvCopy("UNIDRV.HLP",from,to); //pHelpFile
DrvCopy("UNIRES.DLL",from,to); //pDependentFiles
DrvCopy(DRIVER_DLL,from,to); //pDependentFiles
DrvCopy("BMPCREATOR.GPD",from,to); //pConfigFile
DrvCopy("STDNAMES.GPD",from,to); //pDependentFiles
DrvCopy("BMPCREATOR.INI",from,to); //pDependentFiles
Level = 3;
DRIVER_INFO_3 DI3;
DI3.cVersion = 3;
DI3.pName = DxxDriver;
//DI3.pEnvironment = "Windows NT x86" ;
DI3.pEnvironment = NULL ;
DI3.pDriverPath = "\\UNIDRV.DLL";
DI3.pDataFile = "\\BMPCREATOR.GPD" ;
DI3.pConfigFile = "\\UNIDRVUI.DLL";
DI3.pHelpFile = "\\UNIDRV.HLP";
// DRIVER_DLL is defined to the name of my driver actually e.g.
"MYDRIVER.DLL"
DI3.pDependentFiles = "\\UNIRES.DLL\0\\" DRIVER_DLL
"\0\\BMPCREATOR.INI\0\\STDNAMES.GPD\0";
DI3.pMonitorName = NULL ;
DI3.pDefaultDataType = NULL ;
AddPrinterDriverEx(NULL,Level,(LPBYTE)&DI3,APD_COP Y_ALL_FILES);

Here is the add printer. I have some #defines in the beginning of my program
and also DxxPrinter is a char*

HANDLE aph;
DWORD Level = 2;
PRINTER_INFO_2 PI2;
CString CommentString;
PI2.pServerName=NULL;
PI2.pPrinterName=DxxPrinter;
PI2.pShareName=NULL;
PI2.pPortName=PORT_NAME;
PI2.pDriverName=DxxDriver;
if (InstLanguage=="de") {
PI2.pLocation=DESCRIPTION_DE;
CommentString=COMMENT_DE;
} else {
PI2.pLocation=DESCRIPTION_EN;
CommentString=COMMENT_EN;
}
PI2.pComment=CommentString.GetBuffer(0);
PI2.pDevMode=NULL;
PI2.pSepFile=NULL;
PI2.pPrintProcessor="WinPrint";
PI2.pDatatype="RAW";
PI2.pParameters=NULL;
PI2.pSecurityDescriptor=NULL;
//PI2.Attributes=PRINTER_ATTRIBUTE_DIRECT || PRINTER_ATTRIBUTE_RAW_ONLY ||
PRINTER_ATTRIBUTE_LOCAL;
PI2.Attributes=PRINTER_ATTRIBUTE_RAW_ONLY || PRINTER_ATTRIBUTE_LOCAL;
PI2.Priority=1;
PI2.DefaultPriority=1;
PI2.StartTime=0;
PI2.UntilTime=0;
PI2.Status=0;
PI2.cJobs=0;
PI2.AveragePPM=0;
aph=AddPrinter(NULL,Level,(LPBYTE)&PI2);
ClosePrinter(aph);

Regards,
Jürgen Hollfelder
www.hollfelder-edv.de


"Tobias Erichsen" <t.erichsen@gmx.de> schrieb im Newsbeitrag
news:d428eq$ham$1@news1.wobline.de...


Posted by Tobias Erichsen on April 20th, 2005



"Jürgen Hollfelder" <info@hollfelder-edv.de> schrieb im Newsbeitrag
news:%23RxtWJQRFHA.580@TK2MSFTNGP15.phx.gbl...

Where do you copy the unidrv.dll and the other unidriver-related files from?

Tobias



Posted by Jürgen Hollfelder on April 20th, 2005


I have a monolithic driver in production. There I have my own files. In the
case of UNIDRV it is a problem because you cannot just redistribute them. In
other newsgroup entries I have read that there is cab file the people just
take it out of. MS says that is why they would like people to go via INF
files. I recommend you search around in this newsgroup to find where to get
the files from.

Regards,
Jürgen Hollfelder
www.hollfelder-edv.de

"Tobias Erichsen" <t.erichsen@gmx.de> schrieb im Newsbeitrag
news:d44rbv$8hf$1@news1.wobline.de...


Posted by Tobias Erichsen on April 21st, 2005



"Jürgen Hollfelder" <info@hollfelder-edv.de> schrieb im Newsbeitrag
news:e74ePhdRFHA.2252@TK2MSFTNGP15.phx.gbl...
Thx again for the info. I have solved the issue by using "expand" to extract
the missing files from "driver.cab" in the driver cache of the target-system
from within my installation-script... works like a charm.

Tobias