(C++) Connected Printer by Thomas Roccia

Created the Sunday 19 March 2023. Updated 1 year ago.

Description:

This code uses the Windows API function EnumPrinters to enumerate the local printers installed on the system. If EnumPrinters is successful, the code prints the number of printers found and their names.

Code

            #include <windows.h>
#include <winspool.h>

int main() {
    DWORD numPrinters;
    PRINTER_INFO_2* printerInfo;
    if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, NULL, 0, &numPrinters, NULL)) {
        printerInfo = (PRINTER_INFO_2*) malloc(numPrinters * sizeof(PRINTER_INFO_2));
        if (EnumPrinters(PRINTER_ENUM_LOCAL, NULL, 2, (LPBYTE) printerInfo, numPrinters * sizeof(PRINTER_INFO_2), &numPrinters, NULL)) {
            printf("Number of printers found: %d\n", numPrinters);
            // Print the names of the printers
            for (DWORD i = 0; i < numPrinters; i++) {
                printf("%s\n", printerInfo[i].pPrinterName);
            }
        }
        free(printerInfo);
    }
    return 0;
}