Hi,all,
When I try to add a printer programmatically,I retrieve a
error message that error code is 2.
I don't know why?Because I can success to install it by
windows! Follows is my codes and anybody can tell me
what wrong?

PRINTER_INFO_2 pi2;
DRIVER_INFO_3 di3;
HANDLE hPrinter;

ZeroMemory(&di3, sizeof(DRIVER_INFO_3));
di3.cVersion = 3; //for 2000/xp
di3.pName = "Hewlett-Packard HP-GL/2 Plotter";
di3.pEnvironment = NULL; //Written for x86 "Windows NT x86"
di3.pDriverPath = "plotter.dll";
di3.pDataFile = "hpgl2pen.pcd";
di3.pConfigFile = "plotui.dll";
di3.pHelpFile = "plotui.hlp";
di3.pMonitorName= NULL;
di3.pDefaultDataType = "";
di3.pDependentFiles =
"plotter.dll\0hpgl2pen.pcd\0plotui.dll\0plotui.hlp\0\0";

//A error will be occured here!
if(!AddPrinterDriverEx(NULL, 3, (LPBYTE)&di3,APD_COPY_ALL_FILES))
{
ShowError("Add Printer Driver");
//AfxMessageBox("Failure to add printer");
return;
}

ZeroMemory(&pi2, sizeof(PRINTER_INFO_2));
pi2.pPrinterName = "HPGL Printer for Testing";
pi2.pPortName = "LPT3:";
pi2.pDriverName = "Hewlett-Packard HP-GL/2 Plotter";
pi2.pPrintProcessor = "WinPrint";
hPrinter = AddPrinter(NULL, 2, (LPBYTE)&pi2);
if(hPrinter==NULL)
{
ShowError("Add a Printer");
//AfxMessageBox("Failure to add printer");
return;
}
ClosePrinter(hPrinter);

AfxMessageBox("Finish to Add Printer");

Thanks for your any advices!

Horky

RE: When try to add a printer programmatically! by Conan

Conan
Tue Jul 05 22:07:02 CDT 2005

Hi, Horky:
I don't know whether you look at the MSDN.
In it,"Before calling the AddPrinterDriverEx function, all files required by
the driver must be copied to the system's printer-driver directory. To
retrieve the name of this directory, call the GetPrinterDriverDirectory
function. "

And later I copy files into the system directory,then run your code
,everything is OK.
I hope this is usefully for you.

Conan.



"Horky" wrote:

> Hi,all,
> When I try to add a printer programmatically,I retrieve a
> error message that error code is 2.
> I don't know why?Because I can success to install it by
> windows! Follows is my codes and anybody can tell me
> what wrong?
>
> PRINTER_INFO_2 pi2;
> DRIVER_INFO_3 di3;
> HANDLE hPrinter;
>
> ZeroMemory(&di3, sizeof(DRIVER_INFO_3));
> di3.cVersion = 3; //for 2000/xp
> di3.pName = "Hewlett-Packard HP-GL/2 Plotter";
> di3.pEnvironment = NULL; //Written for x86 "Windows NT x86"
> di3.pDriverPath = "plotter.dll";
> di3.pDataFile = "hpgl2pen.pcd";
> di3.pConfigFile = "plotui.dll";
> di3.pHelpFile = "plotui.hlp";
> di3.pMonitorName= NULL;
> di3.pDefaultDataType = "";
> di3.pDependentFiles =
> "plotter.dll\0hpgl2pen.pcd\0plotui.dll\0plotui.hlp\0\0";
>
> //A error will be occured here!
> if(!AddPrinterDriverEx(NULL, 3, (LPBYTE)&di3,APD_COPY_ALL_FILES))
> {
> ShowError("Add Printer Driver");
> //AfxMessageBox("Failure to add printer");
> return;
> }
>
> ZeroMemory(&pi2, sizeof(PRINTER_INFO_2));
> pi2.pPrinterName = "HPGL Printer for Testing";
> pi2.pPortName = "LPT3:";
> pi2.pDriverName = "Hewlett-Packard HP-GL/2 Plotter";
> pi2.pPrintProcessor = "WinPrint";
> hPrinter = AddPrinter(NULL, 2, (LPBYTE)&pi2);
> if(hPrinter==NULL)
> {
> ShowError("Add a Printer");
> //AfxMessageBox("Failure to add printer");
> return;
> }
> ClosePrinter(hPrinter);
>
> AfxMessageBox("Finish to Add Printer");
>
> Thanks for your any advices!
>
> Horky
>
>
>