check if installed network printer is online

余生长醉 提交于 2019-12-03 14:58:51

I fixed it. i used "pinfo2->Attributes & PRINTER_ATTRIBUTE_WORK_OFFLINE".

Here is the code.

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!