检查安装的网络打印机是否在线

PTT*_*PTT 6 c++ network-printers

我想检查打印机是否在线.为此,我获得了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;
}
Run Code Online (Sandbox Code Playgroud)

PTT*_*PTT 7

我修好了它.我使用了"pinfo2-> Attributes&PRINTER_ATTRIBUTE_WORK_OFFLINE".

这是代码.

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_2* pinfo = 0;
  int nRet = 0;
  nRet = GetPrinter(hPrinter, 2,(LPBYTE)pinfo, dwBufsize, &dwBufsize); //Get dwBufsize
  DWORD dwGetPrinter = 0;
  if (nRet == 0)
  {
    dwGetPrinter = GetLastError(); 
  }

  PRINTER_INFO_2* pinfo2 = (PRINTER_INFO_2*)malloc(dwBufsize); //Allocate with dwBufsize
  nRet = GetPrinter(hPrinter, 2,reinterpret_cast<LPBYTE>(pinfo2), dwBufsize, &dwBufsize);
  if (nRet == 0)
  {
    dwGetPrinter = GetLastError(); 
    return false;
  }

  if (pinfo2->Attributes & PRINTER_ATTRIBUTE_WORK_OFFLINE )
  {
    free(pinfo2); 
    ClosePrinter( hPrinter );
    return false;
  }

  free(pinfo2); 
  ClosePrinter( hPrinter );
  return true;
}
Run Code Online (Sandbox Code Playgroud)