News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Listing shared printers

Started by Vortex, May 18, 2024, 06:15:53 AM

Previous topic - Next topic

Vortex

Hello,

This command line tool enumerates the printers shared by a remote system. You can copy the import library winspool.lib offered with the zip archive to the folder \Link32\lib

Usage : ListPrinters \\server
ListPrinters.c :

#include <windows.h>

#include <winspool.h>

#include <stdio.h>

int main(int argc, char * argv[]) {

    DWORD cbBuf = 0;
    DWORD pcReturned;
    PRINTER_INFO_2 * prinfo;
    BOOL r;
    int rv = 0;
    DWORD i;

    char * err = "The EnumPrinters API could not retrieve printer information.\n";

    if (argc != 2) {
        printf("ListPrinters V1.0 by Vortex\n\nUsage : ListPrinters \\\\server\n");
        return 1;
    }

    EnumPrinters(PRINTER_ENUM_SHARED | PRINTER_ENUM_NAME, argv[1], 2, NULL, 0, & cbBuf, & pcReturned);

    if (!cbBuf) {
        printf("%s", err);
        return 2;
    }

    prinfo = (PRINTER_INFO_2 * )VirtualAlloc(0, cbBuf, MEM_COMMIT, PAGE_READWRITE);

    if (!prinfo) {
        printf("Memory allocation failed.\n");
        return 3;
    }

    r = EnumPrinters(PRINTER_ENUM_SHARED | PRINTER_ENUM_NAME, argv[1], 2, (LPBYTE) prinfo, cbBuf, & cbBuf, & pcReturned);

    if (!r) {
        printf("%s", err);
        rv = 2;

    } else {

        for (i = 0; i < pcReturned; ++i) {
            printf("%s\n", prinfo[i].pShareName);
        }
    }

    VirtualFree(prinfo, 0, MEM_RELEASE);
    return rv;

}

Building the project :

\pcc32\pcc32 /I\pcc32\include /c ListPrinters.c
\link32\link32 /SUBSYSTEM:CONSOLE /LIBPATH:\link32\lib /NEV ListPrinters.obj kernel32.lib user32.lib msvcrt.lib winspool.lib