How to get a printer's type with GetPrinter ?

Ji Shirley 181 Reputation points
2021-10-12T05:20:10.437+00:00

I'm trying to use C++ to get a printer's type, such as Laser printers or Needle printer.
I think the GetPrinter interface may be implemented, but I'm not sure which value represents printer type.

    DWORD dwError = 0;
    HANDLE m_hPrinter = nullptr;
    if (!OpenPrinter(printDlg.GetDeviceName().GetBuffer(), &m_hPrinter, NULL))
    {
        dwError = GetLastError();
        printf("OpenPrinter failed with error code : %d", dwError);
        return;
    }

    //Get Printer type
    DWORD cbNeeded = 0;
    PRINTER_INFO_2* pPrintInfo_2 = nullptr;
    GetPrinter(m_hPrinter, 2, nullptr, 0, &cbNeeded);
    if (cbNeeded > 0)
    {
        pPrintInfo_2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR, cbNeeded);
        if (pPrintInfo_2)
        {
            if (!GetPrinter(m_hPrinter, 2, (LPBYTE)pPrintInfo_2, cbNeeded, &cbNeeded))
            {
                GlobalFree((HGLOBAL)pPrintInfo_2);
                pPrintInfo_2 = nullptr;
            }
        }
    }
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,709 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 119K Reputation points
    2021-10-12T05:49:09.777+00:00

    I think that you should also consider WMI and the MarkingTechnology property: https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/win32-printer.

    There is a full sample WMI program: https://learn.microsoft.com/en-us/windows/win32/wmisdk/example--getting-wmi-data-from-the-local-computer. Adjust the query to use the Win32_Printer class instead of Win32_OperatingSystem. Try extracting and displaying the Name and MarkingTechnology values.

    Before trying C++, execute this statement manually in Command Prompt:

    wmic printer get MarkingTechnology, Name

    If it does not display the details, then probably the drivers do not supply them, and WMI is useless.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.