AddPrinter (IEPrint) VC++ Example

void CEPrintDemoView::OnAddEPrintPrinter()
{
   IEpnPrinterSpecifications * pPrinterSpecifications; 
   IEpnMultiSaveOptions * pMultiSaveOptions; 
   IEpnMailMessageInfo * pMailMessageInfo; 
   IEpnBatchPrinterInfo * pBatchPrinterInfo; 
   IEpnPrinterList * pPrintersList; 
   IEpnMailConfigurationSettings * pMailConfigurationSettings; 
   IEPrint * pLeadPrnt; 
   HRESULT hr; 
   int nRet = 0; 

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

   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_EpnMailMessageInfo, NULL, CLSCTX_ALL, IID_IEpnMailMessageInfo, (void**) &pMailMessageInfo); 
   if(FAILED(hr)) 
      return; 

   hr = ::CoCreateInstance(CLSID_EpnMultiSaveOptions, NULL, CLSCTX_ALL, IID_IEpnMultiSaveOptions, (void**) &pMultiSaveOptions); 
   if(FAILED(hr)) 
      return; 

   hr = ::CoCreateInstance(CLSID_EpnPrinterSpecifications, NULL, CLSCTX_ALL, IID_IEpnPrinterSpecifications, (void**) &pPrinterSpecifications); 
   if(FAILED(hr)) 
      return; 

   hr = ::CoCreateInstance(CLSID_EpnMailConfigurationSettings, NULL, CLSCTX_ALL, IID_IEpnMailConfigurationSettings, (void**) &pMailConfigurationSettings); 
   if(FAILED(hr)) 
      return; 

   if (pLeadPrnt->IsSupportLocked(SUPPORT_GENERAL)) 
      pLeadPrnt->UnlockSupport (SUPPORT_GENERAL, OEM_KEY); 
   
   nRet = pLeadPrnt->AddPrinter(TEST_PRINTER_NAME, PRINTER_TASK); 

   if(nRet == 0) 
   {
      //Custom Papers IDs starts from "DMPAPER_USER + 200"
      pPrinterSpecifications->PaperID = DMPAPER_USER + 200; 
      pPrinterSpecifications->PaperSizeName = "Custom Paper Name";
      pPrinterSpecifications->PaperHeight = 11; 
   
      pPrinterSpecifications->PaperWidth = 8; 
      pPrinterSpecifications->DimensionsInInches = true; 
      pPrinterSpecifications->PortraitOrient = true; 
      pPrinterSpecifications->MarginsPrinter = "Margins Printer Name";
      pPrinterSpecifications->PrintQuality = 300; 
      pPrinterSpecifications->YResolution = 300; 
   
      //Set Printer Specifications
      pLeadPrnt->SetPrinterSpecifications (TEST_PRINTER_NAME, pPrinterSpecifications); 

      //Prepare Save Options
   
      pMultiSaveOptions->UseSave = true; 
      pMultiSaveOptions->SaveElementsCount = 1; 
   
      pMultiSaveOptions->GetPrinterSaveOptions (0)->SaveOptions->FileName = "C:\\Untitled.cmp";
      pMultiSaveOptions->GetPrinterSaveOptions(0)->SaveOptions->Format = FILE_CMP; 
      pMultiSaveOptions->GetPrinterSaveOptions(0)->SaveOptions->DocumentType = FT_SAVE_TYPE_RASTER; 
      pMultiSaveOptions->GetPrinterSaveOptions(0)->SaveOptions->RasterOptions->BitsPerPixel = 24; 
      pMultiSaveOptions->GetPrinterSaveOptions(0)->SaveOptions->RasterOptions->QFactor = 100; 
      pMultiSaveOptions->GetPrinterSaveOptions(0)->SaveOptions->RasterOptions->StampBits = 24; 
      pMultiSaveOptions->GetPrinterSaveOptions(0)->SaveOptions->RasterOptions->StampWidth = 128; 
      pMultiSaveOptions->GetPrinterSaveOptions(0)->SaveOptions->RasterOptions->StampHeight = 128; 
      pMultiSaveOptions->GetPrinterSaveOptions(0)->SaveOptions->RasterOptions->MultiPageFile = false; 
   
      //Set Printer Save Options
      pLeadPrnt->SetPrinterSaveOptions (TEST_PRINTER_NAME, pMultiSaveOptions); 

      //Set Printer Renaming Options
      IEpnRenamingOptions * pRenamingOptions = NULL; 
      pLeadPrnt->GetDefaultRenamingOptions(&pRenamingOptions); 
      pLeadPrnt->SetPrinterConflictSolving (TEST_PRINTER_NAME, SAVE_APPEND_FAVOR_RENAME, pRenamingOptions); 

      //Prepare Mail Message
      pMailMessageInfo->UseMail = true; 
      pMailMessageInfo->To = "someone@somedomain.com";
      pMailMessageInfo->Cc = "another@anotherdomain.com";
      pMailMessageInfo->Bcc = "someonespecial@anotherdomain.com";
      pMailMessageInfo->Subject = "Attached Images";
      pMailMessageInfo->Body = "Hello, attached are the images."; 

      //Index of the save options index want to be used to save the e-mailed files. 
      //It is the index of the PrinterSaveOptions property in the EpnMultiSaveOptions
      //passed to the SetPrinterSaveOptions. 
      pMailMessageInfo->EmailSaveOptionsIndex = 0; 
      
      //Set Printer Mail Message
      pLeadPrnt->SetPrinterMailMessageInfo (TEST_PRINTER_NAME, pMailMessageInfo); 

      //Prepare Printers List
      //This will add the Default Printer to the Printers List
      pBatchPrinterInfo->PrinterName = " ";//Printer.DeviceName; 
      pLeadPrnt->GetDefaultEnhancedOptions(&pBatchPrinterInfo->EnhancedOptions); 
      pBatchPrinterInfo->Flags = VALID_ENHANCED_OPTIONS; 
      pPrintersList->AddPrinter (pBatchPrinterInfo); 

      //Set Printer Batch Printers List
      pLeadPrnt->SetPrinterBatchPrintersList (TEST_PRINTER_NAME, pPrintersList); 
   
 
      //Set OEM E-Mail Configurations
      pMailConfigurationSettings->IsMAPI = true; 
      pMailConfigurationSettings->UseDefaultMapiProfile = true; 
      pMailConfigurationSettings->Flags = 0; 

      pLeadPrnt->SetPrinterEmailConfiguration(TEST_PRINTER_NAME, pMailConfigurationSettings); 
   }

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

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

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

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

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

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