获取系统安装的所有打印机名称和驱动名称

看代码:

 

// Get the names of all printers void CTGetPrintsDlg::OnBtnGetPrinterNames() { // TODO: Add your control notification handler code here DWORD tFlag = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS; DWORD ttBuf = 1024; DWORD tBuf = ttBuf; DWORD tReturn; LPPRINTER_INFO_4 tInfo4 = NULL; tInfo4 = (LPPRINTER_INFO_4)LocalAlloc(LPTR, tBuf + 4); BOOL tSuccess = FALSE; tSuccess = EnumPrinters(tFlag,NULL,4,(LPBYTE)tInfo4,tBuf,&tBuf,&tReturn); if(tBuf > ttBuf){ LocalFree(LocalHandle(tInfo4)); tInfo4 = (LPPRINTER_INFO_4)LocalAlloc(LPTR, tBuf + 4); tSuccess = EnumPrinters(tFlag,NULL,4,(LPBYTE)tInfo4,tBuf,&tBuf,&tReturn); } CString tPrinters = ""; if(tSuccess){ CString ts; for(int i = 0 ; i < tReturn; i++){ ts.Format("%d - %s/r/n",i,(tInfo4+i)->pPrinterName); tPrinters += ts; } MessageBox(tPrinters); }else{ DWORD te = GetLastError(); CString ts; ts.Format("%d - %d",te,tBuf); MessageBox(ts); } LocalFree(LocalHandle(tInfo4)); } // get the driver names of all printer void CTGetPrintsDlg::OnBtnDriverNames() { // TODO: Add your control notification handler code here DWORD ttBuf = 1024; DWORD tBuf = ttBuf; DWORD tReturn; LPDRIVER_INFO_1 tDriverInfo1 = NULL; tDriverInfo1 = (LPDRIVER_INFO_1)LocalAlloc(LPTR, tBuf + 4); BOOL tSuccess = FALSE; tSuccess = EnumPrinterDrivers(NULL,NULL,1,(LPBYTE)tDriverInfo1,tBuf,&tBuf,&tReturn); if(tBuf > ttBuf){ LocalFree(LocalHandle(tDriverInfo1)); tDriverInfo1 = (LPDRIVER_INFO_1)LocalAlloc(LPTR,tBuf + 4); tSuccess = EnumPrinterDrivers(NULL,NULL,1,(LPBYTE)tDriverInfo1,tBuf,&tBuf,&tReturn); } CString tPrinterDrivers = ""; if(tSuccess){ CString ts; for(int i =0 ; i < tReturn ; i++){ ts.Format("%s/r/n",((LPDRIVER_INFO_1)(tDriverInfo1+i))->pName); tPrinterDrivers += ts; } MessageBox(tPrinterDrivers); }else{ DWORD te = GetLastError(); CString ts; ts.Format("%d - %d",te,tBuf); MessageBox(ts); } LocalFree(LocalHandle(tDriverInfo1)); }

 

// Get printer names and driver names of all printers void CTGetPrintsDlg::OnBtnGetprints() { // TODO: Add your control notification handler code here DWORD tFlag = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS; DWORD ttBuf = 1024; DWORD tBuf = ttBuf; DWORD tReturn; LPPRINTER_INFO_2 tInfo4 = NULL; tInfo4 = (LPPRINTER_INFO_2)LocalAlloc(LPTR, tBuf + 4); BOOL tSuccess = FALSE; tSuccess = EnumPrinters(tFlag,NULL,2,(LPBYTE)tInfo4,tBuf,&tBuf,&tReturn); if(tBuf > ttBuf){ LocalFree(LocalHandle(tInfo4)); tInfo4 = (LPPRINTER_INFO_2)LocalAlloc(LPTR, tBuf + 4); tSuccess = EnumPrinters(tFlag,NULL,2,(LPBYTE)tInfo4,tBuf,&tBuf,&tReturn); } CString tPrinters = ""; if(tSuccess){ CString ts; for(int i = 0 ; i < tReturn; i++){ ts.Format("%d - %s - %s/r/n",i,((LPPRINTER_INFO_2)(tInfo4+i))->pPrinterName,((LPPRINTER_INFO_2)(tInfo4+i))->pDriverName); tPrinters += ts; } MessageBox(tPrinters); }else{ DWORD te = GetLastError(); CString ts; ts.Format("%d - %d",te,tBuf); MessageBox(ts); } LocalFree(LocalHandle(tInfo4)); }

你可能感兴趣的:(null)