AddPrinterDriver

Hello!

I have a problem with the code listed below. I want to add a Printer Driver
programatically. I am always receiving a message that the files cannot be
found. However the files are there. Even more then once. Has anybody ever
successfully used AddPrinterDriver or AddPrinterDriverEx???

Regards
Jürgen Hollfelder

Below my coding. I have also tried to give full path for dependent files. I
hape also tried with different versions and also used the whole driverpath.
I must admit I am new to C and rather be a Java and VB guy. However the same
in VB does not work either.

#include <windows.h>
void displayMessage();
void subAddPrinterDriver() {
BOOL ergeb;
DRIVER_INFO_3 DI3;
//Driverpath = "C:\WINDOWS\System32\spool\DRIVERS\W32X86\3"
DI3.cVersion = 1 ;
DI3.pEnvironment = “” ;
DI3.pName = “Mydrv” ;
DI3.pDataFile = “c:\drvfiles\HPDJ400.Gpd” ;
DI3.pDriverPath = “c:\drvfiles\UNIDRV.dll” ;
DI3.pConfigFile =“c:\drvfiles\UNIDRVUI.dll” ;
DI3.pHelpFile = “c:\drvfiles\UNIDRV.HLP” ;
DI3.pDependentFiles =“UNIRES.dll\0HPDRES.DLL\0STDNAMES.GPD\0\0” ;
DI3.pMonitorName = NULL ;
DI3.pDefaultDataType = “RAW” ;
ergeb = AddPrinterDriverEx(NULL, // server name
3, // driver information level
(LPBYTE)&DI3, // driver information buffer
APD_COPY_ALL_FILES
);
displayMessage();
}

void displayMessage() {
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
//MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
//MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
(LPTSTR) &lpMsgBuf,
0,
NULL );
MessageBox( NULL, (LPCTSTR)lpMsgBuf, “Error”, MB_OK | MB_ICONINFORMATION );
//printf(lpMsgBuf);
LocalFree( lpMsgBuf );
}

//LONG WINAPI WndProc (HWND , UINT , UINT , LONG) ;
int WINAPI WinMain(HANDLE hInstance , HANDLE hPrevInstance , LPSTR
lpszCmdParam , int nCmdShow)
{
subAddPrinterDriver();
}</windows.h>

Jürgen Hollfelder wrote:

Hello!

I have a problem with the code listed below. I want to add a Printer Driver
programatically. I am always receiving a message that the files cannot be
found. However the files are there. Even more then once. Has anybody ever
successfully used AddPrinterDriver or AddPrinterDriverEx???

Yes. Call the GetPrinterDriverDirectory API and place your driver
files there before calling AddPrinterDriver.


Steve Johnson

Dear Steve!

Thank you so much. That was it. You made my weekend.

Somehow there is a directory C:\windows\system32\spool\drivers\w32x86\3\
where I always thought I need to have to copy the files to. Now I copied my
stuff into the result of GetPrinterDirectory and all of a sudden it works.

I was searching the Internet like mad for a sample code and nothing really
worked I am posting the functioning code below. For others with the same
problem.

I want to add the remark that I have never before really coded in C++ and C
(I am actually a old COBOL programmer and learnt VB and Java during the past
year in my spare time). So this code may looks a bit strange. But it works.

Regards,
Jürgen Hollfelder

C++ Code:

BOOL ergeb;
DRIVER_INFO_3 DI3;

char * pDirInfo =“”;
ergeb =
GetPrinterDriverDirectory(NULL,NULL,1,(LPBYTE)pDirInfo,0,&bytes_needed );
pDirInfo = new char[1+bytes_needed/sizeof(char)];
cbBuf = sizeof(char)*(1+bytes_needed/sizeof(char));
ergeb =
GetPrinterDriverDirectory(NULL,NULL,1,(LPBYTE)pDirInfo,cbBuf,&bytes_needed );DI3.cVersion
= 3 ;

DI3.pEnvironment = NULL ;
DI3.pName = “HP DeskJet 400” ;
DI3.pDataFile = strcat(pDirInfo,“\HPDJ400.Gpd”) ;
DI3.pDriverPath = strcat(pDirInfo,\UNIDRV.DLL);
DI3.pConfigFile = strcat(pDirInfo,\UNIDRVUI.DLL);
DI3.pHelpFile = strcat(pDirInfo,\UNIDRV.HLP);
DI3.pDependentFiles = “UNIRES.dll\0HPDJRES.DLL\0STDNAMES.GPD\0\0” ;
DI3.pMonitorName = NULL ;
DI3.pDefaultDataType = NULL ;
ergeb = AddPrinterDriver(NULL, // server name
3, // driver information level
(LPBYTE)&DI3 // driver information buffer
);

“Steve Johnson” schrieb im Newsbeitrag
news:xxxxx@ntdev…
> Jürgen Hollfelder wrote:
>
>> Hello!
>
>> I have a problem with the code listed below. I want to add a Printer
>> Driver
>> programatically. I am always receiving a message that the files cannot be
>> found. However the files are there. Even more then once. Has anybody ever
>> successfully used AddPrinterDriver or AddPrinterDriverEx???
>
> Yes. Call the GetPrinterDriverDirectory API and place your driver
> files there before calling AddPrinterDriver.
>
>
> –
> Steve Johnson
>
>
>
>