我想检查打印机是否在线。为此,我使用 OpenPrinter 获取打印机句柄。然后我想在 PRINTER_INFO_6 中使用 PRINTER_STATUS_OFFLINE 和 GetPrinter()。结果总是0?
如何获取打印机的离线状态?
我使用的代码。
bool IsPrinterOnline(wstring strPrinterFriendlyName)
{
HANDLE hPrinter ;
if ( OpenPrinter(const_cast<LPWSTR>(strPrinterFriendlyName.c_str()), &hPrinter, NULL) == 0 )
{
/*OpenPrinter call failed*/
return false;
}
DWORD dwBufsize = 0;
PRINTER_INFO_6* pinfo = 0;
GetPrinter(hPrinter, 6,(LPBYTE)pinfo, dwBufsize, &dwBufsize); //Get dwBufsize
PRINTER_INFO_6* pinfo6 = (PRINTER_INFO_6*)malloc(dwBufsize); //Allocate with dwBufsize
GetPrinter(hPrinter, 6,(LPBYTE)pinfo6, dwBufsize, &dwBufsize);
DWORD dwStatus = pinfo6->dwStatus; //always returns 0
if (dwStatus == PRINTER_STATUS_OFFLINE)
{
free(pinfo6);
ClosePrinter( hPrinter );
return false;
}
free(pinfo6);
ClosePrinter( hPrinter );
return true;
}