get_printer_inf_path(std::string printerName) { CP_LOG_INFO("control panel printerName: %s", printerName.c_str()); std::string infPath = ""; DWORD numPrinters; DWORD bufferSize = 0; DWORD bytesNeeded; // 第一次调用获取所需缓冲区大小 EnumPrinters(PRINTER_ENUM_LOCAL, nullptr, 2, nullptr, 0, &bufferSize, &numPrinters); if (bufferSize == 0) { CP_LOG_WARNING("EnumPrinters, No printers found."); return infPath; } // 分配缓冲区 BYTE* buffer = new BYTE[bufferSize]; // 第二次调用获取打印机信息 if (EnumPrinters(PRINTER_ENUM_LOCAL, nullptr, 2, buffer, bufferSize, &bytesNeeded, &numPrinters)) { PRINTER_INFO_2* printerInfo = reinterpret_cast<PRINTER_INFO_2*>(buffer); for (DWORD i = 0; i < numPrinters; i++) { std::wstring wPrinterName = (printerInfo[i].pPrinterName); std::string printerName_ = wstring_to_string(wPrinterName); CP_LOG_DEBUG("control panel Name: %s", printerName_.c_str()); if (printerName_ == printerName) { // 获取打印机句柄 HANDLE hPrinter; if (OpenPrinter(printerInfo[i].pPrinterName, &hPrinter, nullptr)) { DWORD cbNeeded = 0; if (!GetPrinterDriver(hPrinter, NULL, 8, NULL, 0, &cbNeeded) && cbNeeded > 0) { LPBYTE pDriverInfo = new BYTE[cbNeeded]; if (GetPrinterDriver(hPrinter, NULL, 8, pDriverInfo, cbNeeded, &cbNeeded)) { DRIVER_INFO_8* pDriverInfo8 = reinterpret_cast<DRIVER_INFO_8*>(pDriverInfo); if (pDriverInfo8->pName) { std::wstring wDriverName = (pDriverInfo8->pName); std::string driverName = wstring_to_string(wDriverName); if (driverName != "" && driverName.c_str()) CP_LOG_INFO("DriverName: %s", driverName.c_str()); } if (pDriverInfo8->pszInfPath) { std::wstring wInfPath = (pDriverInfo8->pszInfPath); infPath = wstring_to_string(wInfPath); if (infPath != "" && infPath.c_str()) CP_LOG_INFO("infPath: %s", infPath.c_str()); } } else { CP_LOG_WARNING("Failed to get printer driver information. Error code: %d",GetLastError()); } delete[] pDriverInfo; } } else { CP_LOG_WARNING("Failed to OpenPrinter. Error code: %d",GetLastError()); }
ClosePrinter(hPrinter); } } } else { CP_LOG_WARNING("Failed to EnumPrinters. Error code: %d",GetLastError()); } // 释放缓冲区 delete[] buffer;
return infPath; }
|