【问题标题】:check if installed network printer is online检查安装的网络打印机是否在线
【发布时间】:2012-10-19 09:15:50
【问题描述】:

我想检查打印机是否在线。为此,我得到 带有 OpenPrinter 的打印机句柄。然后我想使用 PRINTER_STATUS_OFFLINE 在 PRINTER_INFO_6 中使用 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;
}

【问题讨论】:

  • 两个GetPrinter真的成功了吗?
  • 第二次 GetPrinter 已成功(返回非零)。但状态始终为零。

标签: c++ network-printers


【解决方案1】:

我修好了。我使用了“pinfo2->属性和 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;
}

【讨论】:

  • @CristianAmarie:你能解释一下你的评论吗?
  • 网络打印机可以报告为在线,而物理设备实际上是离线的。我并不是说总是正确的,但这种情况正在发生。也发生了相反的情况:网络打印机在我的计算机上报告为脱机(这是错误的),我无法打印任何东西。我让一位同事打印一些东西,瞧——打印机将其状态更新回在线。假脱机程序和网络列表管理器/UPnP/Win32 使用的任何机制之间很可能存在分歧。
猜你喜欢
  • 2015-09-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多