打印机的管理-创新互联
枚举计算机的所有打印机
成都创新互联公司是专业的纳雍网站建设公司,纳雍接单;提供网站设计、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行纳雍网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
#includePRINTER_INFO_5* prninfo = NULL; void CMFCApplication1Dlg::OnBnClickedButton8() { DWORD dwNeeded=0, dwReturned=0; EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, NULL, 0, &dwNeeded, &dwReturned); prninfo = (PRINTER_INFO_5*)GlobalAlloc(GPTR, dwNeeded); if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, (LPBYTE)prninfo, dwNeeded, &dwNeeded, &dwReturned)) { for (DWORD i = 0; i < dwReturned; i++, prninfo++) { TRACE(_T("x = %s\n"), prninfo->pPrinterName); TRACE(_T("x = %s\n"), prninfo->pPortName); } } }
2.设置默认打印机
TCHAR szPrinterName[1024] = { 0 }; GetDlgItemText(IDC_EDIT1, szPrinterName, 1024); BOOL ret = FALSE; ret = SetDefaultPrinter(szPrinterName); if (ret == FALSE) AfxMessageBox(_T("设置默认打印机失败")); else AfxMessageBox(_T("设置默认打印机成功"));
3.启动打印机服务
SHELLEXECUTEINFO ShExecInfo = { 0 }; ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS; ShExecInfo.hwnd = NULL; ShExecInfo.lpVerb = NULL; ShExecInfo.lpFile = _T("cmd"); ShExecInfo.lpParameters = _T("/C net start spooler"); ShExecInfo.lpDirectory = NULL; ShExecInfo.nShow = SW_HIDE; ShExecInfo.hInstApp = NULL; ShellExecuteEx(&ShExecInfo); WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
4.获取指定打印机的所有纸张
TCHAR szPaperName[30000] = {0}; TCHAR str[650] = {0}; void CMFCApplication1Dlg::Printerdo1() { BYTE* pBuffer = NULL; DWORD dwSize = 0; DWORD dwNum = 0; //获得缓冲区大小 ::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, pBuffer, dwSize, &dwSize, &dwNum); pBuffer = new BYTE[dwSize]; //枚举打印机 ::EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 5, pBuffer, dwSize, &dwSize, &dwNum); if (dwNum != 0) { //获得打印机信息 PRINTER_INFO_5* pPrinterInfo = (PRINTER_INFO_5*)pBuffer; LPHANDLE phandle; HANDLE handle; handle = malloc(sizeof(HANDLE)); phandle = &handle; PRINTER_DEFAULTS *printer_defaults; printer_defaults = (PRINTER_DEFAULTS *)malloc(sizeof(PRINTER_DEFAULTS)); LPWSTR str_printername, str_portname, papername2; //添加打印机名及端口 str_printername = pPrinterInfo->pPrinterName; str_portname = pPrinterInfo->pPortName; int nNeeded = 0; nNeeded = DeviceCapabilities(str_printername, str_portname, DC_PAPERNAMES, NULL, NULL); DeviceCapabilities(str_printername, str_portname, DC_PAPERNAMES, szPaperName,NULL); for (int i = 0, j=0; i < nNeeded && szPaperName[j] != 0;) { wmemcpy_s(str, 64, (LPCWSTR)(&szPaperName)+j,64); m_ListPrinter.AddString(str); TRACE(_T("%s\n"), str); j += 64; } } }
5.获取所有纸张的尺寸
CPoint pt[107]; DeviceCapabilities(str_printername, str_printername, DC_PAPERSIZE, (LPWSTR)&pt, NULL);
6.获取默认打印机的默认纸型和大小
void CMFCApplication1Dlg::OnBnClickedButton13() { // TODO: 在此添加控件通知处理程序代码 PRINTDLG FAR * pPrintDlg = new PRINTDLG; //获取当前打印机的设置 if (AfxGetApp()->GetPrinterDeviceDefaults(pPrintDlg)) { //获取打印机的两个设置信息结构 DEVNAMES FAR*lpDevNames=(DEVNAMES FAR *)::GlobalLock(pPrintDlg->hDevNames); DEVMODE FAR*lpDevMode =(DEVMODE FAR *)::GlobalLock(pPrintDlg->hDevMode); //获取相关信息:驱动服务名,打印机名,端口号 CString szDriver((LPTSTR)lpDevNames +lpDevNames->wDriverOffset); CString szDevice((LPTSTR)lpDevNames +lpDevNames->wDeviceOffset); CString szOutput((LPTSTR)lpDevNames +lpDevNames->wOutputOffset); HANDLE hPrinter; ::OpenPrinter((LPWSTR)(LPCWSTR)szDevice, &hPrinter, NULL); ::DocumentProperties(NULL,hPrinter,(LPWSTR)(LPCWSTR)szDevice,lpDevMode, lpDevMode, DM_IN_BUFFER | DM_OUT_BUFFER); // 结构解锁 ::GlobalUnlock(pPrintDlg->hDevNames); ::GlobalUnlock(pPrintDlg->hDevMode); } delete pPrintDlg; }
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享名称:打印机的管理-创新互联
网址分享:http://ybzwz.com/article/hpeee.html