// 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.