2015-05-23 102 views
1

VC++中使用Windows Management Instrumentation(WMI),我們可以找到SystemInfo,如系統名稱和其他屬性。根據其狀態獲取打印機詳細信息

GetComputerName例子:

BOOL WINAPI GetComputerName(
    _Out_ LPTSTR lpBuffer, 
    _Inout_ LPDWORD lpnSize 
); 

有附着在我的系統1臺的熱和2共享打印機3臺打印機,

我如何獲得有關打印機是脫機的信息?
如何基於狀態分類/列出打印機?

感謝

回答

1

參見EnumPrinters

DWORD flags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_CONNECTIONS; 
DWORD bufsize, printerCount; 
DWORD level = 2; //2 is for PRINTER_INFO_2 

::EnumPrinters(flags, NULL, level, NULL, 0, &bufsize, &printerCount); 
if (bufsize) 
{ 
    BYTE* buffer = new BYTE[bufsize]; 
    ::EnumPrinters(flags, NULL, level, buffer, bufsize, &bufsize, &printerCount); 

    if (bufsize && printerCount) 
    { 
     const PRINTER_INFO_2* info = (PRINTER_INFO_2*)buffer; 
     for (DWORD i = 0; i < printerCount; i++) 
     { 
      if (info->pServerName) cout << "pServerName: " << info->pServerName << endl; 
      if (info->pPrinterName) cout << "pPrinterName: " << info->pPrinterName << endl; 
      if (info->pShareName) cout << "pShareName: " << info->pShareName << endl; 
      if (info->pPortName) cout << "pPortName: " << info->pPortName << endl; 
      if (info->Attributes & PRINTER_ATTRIBUTE_LOCAL) cout << "[local]\n"; 
      if (info->Attributes & PRINTER_ATTRIBUTE_NETWORK) cout << "[network]\n"; 

      wcout << "status: " << info->Status << endl; 
      if (info->Status & PRINTER_STATUS_ERROR) cout << "status: error\n"; 

      wcout << endl; 
      info++; 
     } 
    } 
    delete[] buffer; 
}