获得所有打印机

来源:互联网 发布:传奇永恒网络传输异常 编辑:程序博客网 时间:2024/05/15 09:30
  1. //得到所有打印机   
  2. DWORD dwSize,dwPrinters;   
  3. ::EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL,NULL,5,NULL,0,&dwSize,&dwPrinters);   
  4. BYTE *pBuffer=new BYTE[dwSize];   
  5. ::EnumPrinters(PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL,NULL,5,pBuffer,dwSize,&dwSize,&dwPrinters);   
  6.   
  7. CString sPrinter;  
  8. if(dwPrinters!=0)   
  9. {   
  10.     PRINTER_INFO_5 *pPrnInfo=(PRINTER_INFO_5 *)pBuffer;   
  11.     for(int i=0;i <dwPrinters;i++)   
  12.     {   
  13.         sPrinter.Format(_T("%s"),pPrnInfo-> pPrinterName);  
  14.         pPrnInfo++;//指针后移    
  15.     }   
  16. }   
  17. delete []pBuffer;  

 

  1. //得到默认打印机并弹出属性编辑   
  2. #include <WinSpool.h>     
  3. #pragma comment(lib, "Winspool.lib")    
  4.   
  5. #include <shellapi.h>     
  6. #pragma comment(lib, "shell32.lib")    
  7.   
  8. BOOL OpenDefaultPrinter()    
  9. {    
  10.     BOOL bRet = FALSE;    
  11.   
  12.     DWORD dwSize = 0;    
  13.     if(!GetDefaultPrinter(NULL, &dwSize)     
  14.         && GetLastError()==ERROR_INSUFFICIENT_BUFFER)    
  15.     {    
  16.         TCHAR *szPrinter = new TCHAR[dwSize+1];    
  17.         if(GetDefaultPrinter(szPrinter, &dwSize))    
  18.         {    
  19.             if(SHInvokePrinterCommand(    
  20.                 NULL, //GetSafeHwnd(),      
  21.                 PRINTACTION_PROPERTIES,    
  22.                 szPrinter,    
  23.                 NULL,    
  24.                 FALSE))    
  25.             {    
  26.                 bRet = TRUE;    
  27.             }    
  28.         }    
  29.         delete []szPrinter;    
  30.     }    
  31.   
  32.     return bRet;    
  33. }    

 

原创粉丝点击