根据状态获取打印机详细信息

Get printer details based on their status

VC++ 中使用 Windows Management Instrumentation (WMI) 我们可以找到 SystemInfo,如系统名称和其他属性。

GetComputerName 的示例:

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

我的系统中连接了 3 台打印机,1 台热敏打印机和 2 台共享打印机,

如何获取离线打印机的信息?
我如何根据状态 classify/list 打印机?

谢谢

另见 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;
}