Tharif
Tharif

Reputation: 13971

Get printer details based on their status

Using Windows Management Instrumentation (WMI) in VC++ we can find the SystemInfo like System Name and other properties.

Example for GetComputerName :

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

There are 3 printers attached in my system 1 thermal and 2 Shared printers ,

How can i get the information about printer which is offline ?
How can i classify/list printers based on their status ?

Thanks

Upvotes: 1

Views: 1338

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

See also 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;
}

Upvotes: 1

Related Questions