AddPrinter() function failure error "Printer Driver Unknown"

I am using AddPrinter() function to add a local printer.But it return error 1797(Printer Driver Unknown).

This is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Define Printer information structure
PRINTER_INFO_2 pi;

BOOL Result = FALSE;
HANDLE pHd;
DWORD m_errorCode;
// Memory initialisation - pi structure to zero
memset(&pi,0,sizeof(PRINTER_INFO_2));
pi.pPrinterName ="MyPrinter";
// Select Driver Name
pi.pDriverName = "MyPrinter";
// Select Share Name
pi.pShareName ="MyPrinter";
// Select Server Name
pi.pServerName = NULL;
// Select Port Name
pi.pPortName ="COM3:";
pi.pSecurityDescriptor = NULL;
// Select Print Processor
pi.pPrintProcessor = L"winprint";
// Select Attributes
//pi.Attributes = PRINTER_ATTRIBUTE_DO_COMPLETE_FIRST;
// Set Priority
pi.Priority = 1;
// Call the function AddPrinter
pHd = AddPrinter(NULL,2,(LPBYTE)&pi);


I also used AddPrinterDriverEx() before AddPrinter().

One odd thing i saw that If i add the printer manually through the Add Printer Wizard, then remove remove printer, after that if i use AddPrinter function, then it completes OK and the printer is added.
Probably the issue is in the AddPrinterDriverEx portion of code. Not sure.
Thanx for your reply. But AddPrinterDriverEx() return successful.
All of what seems to be on MSDN or Google, seems to say AddPrinterDriverEx has something wrong inside.
I may be wrong, anyways.
Topic archived. No new replies allowed.