AddPrinter (IEpnPrinterList) VC++ Example

void CEPrintDemoView::OnAddPrinter()
{
   IEpnEnhancedOptions * pEnhancedOptions; 
   IEpnBatchPrinterInfo * pBatchPrinterInfo; 
   IEpnPrinterList * pPrintersList; 
   IEPrint * pLeadPrnt; 
   HRESULT hr; 
   int i = 0; 

   hr = ::CoCreateInstance(CLSID_EPrint, NULL, CLSCTX_ALL, IID_IEPrint, (void**) &pLeadPrnt); 
   if(FAILED(hr)) 
      return; 

   hr = ::CoCreateInstance(CLSID_EpnPrinterList, NULL, CLSCTX_ALL, IID_IEpnPrinterList, (void**) &pPrintersList); 
   if(FAILED(hr)) 
      return; 

   hr = ::CoCreateInstance(CLSID_EpnBatchPrinterInfo, NULL, CLSCTX_ALL, IID_IEpnBatchPrinterInfo, (void**) &pBatchPrinterInfo); 
   if(FAILED(hr)) 
      return; 

   hr = ::CoCreateInstance(CLSID_EpnEnhancedOptions, NULL, CLSCTX_ALL, IID_IEpnEnhancedOptions, (void**) &pEnhancedOptions); 
   if(FAILED(hr)) 
      return; 

   if (pLeadPrnt->IsSupportLocked (SUPPORT_OEM)) 
      pLeadPrnt->UnlockSupport (SUPPORT_OEM, OEM_KEY); 

   pLeadPrnt->GetDefaultEnhancedOptions (&pEnhancedOptions); 

   pBatchPrinterInfo->PrinterName = "";
   pBatchPrinterInfo->Flags = VALID_ENHANCED_OPTIONS; 

   pBatchPrinterInfo->EnhancedOptions->VerticalAlign = pEnhancedOptions->VerticalAlign; 
   pBatchPrinterInfo->EnhancedOptions->HorizontalAlign = pEnhancedOptions->HorizontalAlign; 
   pBatchPrinterInfo->EnhancedOptions->Layout = pEnhancedOptions->Layout; 
   pBatchPrinterInfo->EnhancedOptions->Margin = pEnhancedOptions->Margin; 
   pBatchPrinterInfo->EnhancedOptions->Ordering = pEnhancedOptions->Ordering; 
   pBatchPrinterInfo->EnhancedOptions->Borders = pEnhancedOptions->Borders; 
   pBatchPrinterInfo->EnhancedOptions->PrintingMethod = pEnhancedOptions->PrintingMethod; 
   pBatchPrinterInfo->EnhancedOptions->GutterUnit = pEnhancedOptions->GutterUnit; 
   pBatchPrinterInfo->EnhancedOptions->Gutter = pEnhancedOptions->Gutter; 
   pBatchPrinterInfo->EnhancedOptions->EnhancedOptions = pEnhancedOptions->EnhancedOptions; 
   pBatchPrinterInfo->EnhancedOptions->GutterInside = pEnhancedOptions->GutterInside; 
   pBatchPrinterInfo->EnhancedOptions->MarginOffset = pEnhancedOptions->MarginOffset; 
   pBatchPrinterInfo->EnhancedOptions->Repeat = pEnhancedOptions->Repeat; 
   pBatchPrinterInfo->EnhancedOptions->BestFit = pEnhancedOptions->BestFit; 
   pBatchPrinterInfo->EnhancedOptions->RightToLeft = pEnhancedOptions->RightToLeft; 
   pBatchPrinterInfo->EnhancedOptions->GutterWidth = pEnhancedOptions->GutterWidth; 

   pPrintersList->AddPrinter(pBatchPrinterInfo); 

   CString strMsg; 
   strMsg.Format("Printers Count = %d", pPrintersList->PrintersCount); 
   MessageBox(strMsg); 

   pLeadPrnt->SetPrinterBatchPrintersList(TEST_PRINTER_NAME, pPrintersList); 

   if(pEnhancedOptions) 
   {
      pEnhancedOptions->Release();
      pEnhancedOptions = NULL; 
   }

   if(pBatchPrinterInfo) 
   {
      pBatchPrinterInfo->Release();
      pBatchPrinterInfo = NULL; 
   }

   if(pPrintersList) 
   {
      pPrintersList->Release();
      pPrintersList = NULL; 
   }

   if(pLeadPrnt) 
   {
      pLeadPrnt->Release();
      pLeadPrnt = NULL; 
   }
}