參見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;
}