// 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_DEFAULT | 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() and it return successful.
But AddPrinter() hang and doesn't return anything.This code is running successfully in Windows 7 but got stuck in Windows 8.