Advertisement
Guest User

Printer DC

a guest
Mar 17th, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include<windows.h>
  2. #include<vector>
  3.  
  4. std::vector<char*> query_printers(){
  5.     DWORD dwReturned;
  6.     DWORD dwNeeded;
  7.  
  8.     PRINTER_INFO_4 *pInfo;
  9.  
  10.     char szPrinterName[255];
  11.     unsigned long lPrinterNameLength;
  12.  
  13.     GetDefaultPrinter(szPrinterName, &lPrinterNameLength);
  14.  
  15.     auto fnReturn = EnumPrinters(
  16.         PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
  17.         szPrinterName,
  18.         1L,                // printer info level
  19.         (LPBYTE)NULL,
  20.         0L,
  21.         &dwNeeded,
  22.         &dwReturned);
  23.  
  24.     if (dwNeeded > 0)
  25.     {
  26.         pInfo = (PRINTER_INFO_4 *)HeapAlloc(
  27.             GetProcessHeap(), 0L, dwNeeded);
  28.     }
  29.  
  30.     if (NULL != pInfo)
  31.     {
  32.         dwReturned = 0;
  33.         fnReturn = EnumPrinters(
  34.             PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS,
  35.             NULL,
  36.             1L,                // printer info level
  37.             (LPBYTE)pInfo,
  38.             dwNeeded,
  39.             &dwNeeded,
  40.             &dwReturned);
  41.     }
  42.  
  43.     std::vector<char*> names;
  44.  
  45.     if (fnReturn)
  46.     {
  47.         std::string cached_name;
  48.  
  49.         for (auto i = 0; i < dwReturned; i++)
  50.         {
  51.             cached_name = pInfo[i].pPrinterName;
  52.  
  53.             if (cached_name.find("NULL") != std::string::npos ||
  54.                 cached_name.empty())
  55.                 continue;
  56.  
  57.             names.push_back(pInfo[i].pPrinterName);
  58.         }
  59.     }
  60.  
  61.     return names;
  62. }
  63.  
  64. std::vector<HDC> get_printers_dc(){
  65.  
  66.     auto printers = query_printers();
  67.     std::vector<HDC> device_contexts;
  68.  
  69.     for (auto& printer : printers)
  70.     {
  71.         device_contexts.push_back(CreateDC("WINSPOOL", printer, NULL, NULL));
  72.     }
  73.  
  74.     return device_contexts;
  75. }
  76.  
  77. int main(){
  78.     auto captured = get_printers_dc();
  79.  
  80.     return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement