HOWTO: Retrieve Print Job Information

ID: Q228769


The information in this article applies to:


SUMMARY

This article contains source code for an application that would do the following:


MORE INFORMATION

Copy the following code as a C source file and build a Win32 console application. The resulting application lists the installed printers on the system and continously dumps the information of any job submitted to the printers until the user exits the application by pressing any key.

Sample Code


#include <windows.h>
#include <winerror.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <conio.h>
#include <string.h>

#define MAX_PRINTERS 256
#define dwFlags PRINTER_ENUM_CONNECTIONS | PRINTER_ENUM_LOCAL

main
(
 int    argc,
 char * argv[]    
 )
{
    HANDLE             hPrinter[MAX_PRINTERS];
    DWORD              pcbNeeded=0,pcReturned=0,dwBytesNeeded=0,dwReturned=0;
    JOB_INFO_2 *       pJobInfo = NULL;
    PRINTER_INFO_4 *   pinfo4 = NULL;
    int                previous[MAX_PRINTERS], i =0;
    SYSTEMTIME         st;
    
    for (i=0; i<MAX_PRINTERS; i++) { previous[i]=0; }
    
    /***********************************************************************
    Call EnumPrinters with a PRINTER_INFO_4 to enumerate all the printers 
    installed on the machine locally or networked.
    PRINTER_INFO_4 queries the registry to find all Prns.
    PRINTER_INFO_4 : Name=NULL; function will query on the Local machine;
    Also, because Level=4, you can only use the above two flags (dwFlags).
    ***********************************************************************/ 
    
    while ( !EnumPrinters (dwFlags, NULL, 4, (LPBYTE) pinfo4, dwBytesNeeded, 
                &dwBytesNeeded, &dwReturned) )
    {
        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            if (pinfo4) LocalFree (pinfo4);
            pinfo4 = (PRINTER_INFO_4 *) LocalAlloc (LPTR, dwBytesNeeded);
        }
        else
        {
            printf("EnumPrinters failed: %d\n",GetLastError());
            return 1;
        }
    }
    
    printf("\n*******************************************************\n\n");
    
    printf("# OF PRINTERS (Local/Networked) INSTALLED ON YOUR MACHINE = %lu\n", 
            dwReturned);
    
    for (i = 0; i < (int)dwReturned; i++)
    {
        printf("\nPrinter #%d: %s \n\n",i+1,pinfo4[i].pPrinterName);
    }
    
    /**********************************************************************/ 
        
    for (i = 0; i < (int)dwReturned; i++)
    {
        if ( !OpenPrinter( pinfo4[i].pPrinterName, (LPHANDLE)&hPrinter[i],
            (LPPRINTER_DEFAULTS)NULL) )
        {
            printf("OpenPrinter failed: %d\n",GetLastError());    
            return 1;
        }
    }
    
    /**********************************************************************/ 
    
    
    while (!kbhit()) 
                    // Keep polling forever and dump the info whenever a 
                    // job is submitted to any printer.
    {
        
        for(i=0; i<(int)dwReturned; i++) // Number of printers installed.
        {
            
           
            /**************************************************************/ 
            
            
            while ( !EnumJobs((HANDLE)hPrinter[i], 0, 1, 2, (LPBYTE)pJobInfo, 
                pcbNeeded, (LPDWORD)&pcbNeeded, (LPDWORD)&pcReturned) )
            {                   
                
                if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
                {
                    if (pJobInfo) LocalFree(pJobInfo);
                    pJobInfo = (JOB_INFO_2 *) LocalAlloc(LPTR, pcbNeeded);    
                }
                else
                {
                    printf("EnumJobs on printer %d failed: %d\n",i+1,
                        GetLastError());
                    goto Cleanup;
                }
            }
            
            
            if (pcReturned > 0) // There is a JOB to print from printer[i]
            {                 
                
                if ((int)pJobInfo->JobId != previous[i])
                { // next job
                    
                    printf("\n\nPRINTER # %d  IS PRINTING ************\n",i+1);
                    
                    printf("DUMP THE INFO FROM THE CURRENT JOB - devmode\n");
                                        
                    /******************************************************/                
                    
                    if(pJobInfo->pDevMode->dmFields & DM_YRESOLUTION)
                      printf("Y-Resolution:  %d dpi\n",
                        pJobInfo->pDevMode->dmYResolution);                                    
                    
                    if(pJobInfo->pDevMode->dmPrintQuality & 
                        (DMRES_HIGH | DMRES_MEDIUM | DMRES_LOW | DMRES_DRAFT))
                      printf("Print Quality: %d dpi\n", 
                        pJobInfo->pDevMode->dmPrintQuality);
                                        
                    /******************************************************/ 
                    /*   Check if Orientation is Portrait or Landscape    */ 
                    /******************************************************/ 
                    
                    if(pJobInfo->pDevMode->dmOrientation == DMORIENT_PORTRAIT)
                      printf("Orientation:Portrait\n");
                    
                    if(pJobInfo->pDevMode->dmOrientation == DMORIENT_LANDSCAPE)
                      printf("Orientation:Landscape\n");
                                    
                    /******************************************************/ 
                    
                    if(pJobInfo->pDevMode->dmFields & DM_COPIES)
                      printf("Num of Copies: %d\n",
                        pJobInfo->pDevMode->dmCopies);
                    
                    if(pJobInfo->pDevMode->dmFields & DM_PAPERLENGTH)
                      printf("Paper Length: %d\n",
                        pJobInfo->pDevMode->dmPaperLength);
                    
                    if(pJobInfo->pDevMode->dmFields & DM_PAPERWIDTH)
                      printf("Paper Width: %d\n",
                        pJobInfo->pDevMode->dmPaperWidth);
                                        
                    /******************************************************/ 
                    
                    if(pJobInfo->pDevMode->dmDuplex & DM_DUPLEX)
                      printf("Duplex Mode ON \n");
                    
                    
                    if(pJobInfo->pDevMode->dmColor == DMCOLOR_COLOR)
                      printf("Color Image\n");       // COLOR
                    
                    else printf("Monochrome Image\n"); // MONOCHROME
                    
                    if(pJobInfo->pDevMode->dmFields & DM_BITSPERPEL)
                      printf("Bits Per Pel: %d\n",
                        pJobInfo->pDevMode->dmBitsPerPel);
                    
                                    
                    printf("\nJOB INFO-2 dump      **************\n");
                    
                    printf("JobId           : %lu\n",pJobInfo->JobId);
                    
                    previous[i] = pJobInfo->JobId;
                    
                    printf("Printer Name    : %s\n",pJobInfo->pPrinterName);
                    printf("Machine Name    : %s\n",pJobInfo->pMachineName);
                    printf("User Name       : %s\n",pJobInfo->pUserName);
                    printf("Document        : %s\n",pJobInfo->pDocument);
                    printf("Notify Name     : %s\n",pJobInfo->pNotifyName);
                    printf("Datatype        : %s\n",pJobInfo->pDatatype);
                    printf("Print Processor : %s\n",pJobInfo->pPrintProcessor);
                    printf("Parameters      : %s\n",pJobInfo->pParameters);
                    printf("Driver Name     : %s\n",pJobInfo->pDriverName);
                    
                    printf("TotalPages      : %lu\n",pJobInfo->TotalPages);
                    printf("Size            : %lu\n",pJobInfo->Size);
                    
                    GetLocalTime (&st);   /* Get Local time */ 
                    
                    printf("Date            : %d/%d/%d \ 
                          \nTime            : %d:%02d:%02d:%02d\n", \ 
                        
                        pJobInfo->Submitted.wMonth            =(int)st.wMonth,
                        pJobInfo->Submitted.wDay            =(int)st.wDay,
                        pJobInfo->Submitted.wYear            =(int)st.wYear,
                        pJobInfo->Submitted.wHour            =(int)st.wHour,
                        pJobInfo->Submitted.wMinute            =(int)st.wMinute,
                        pJobInfo->Submitted.wSecond            =(int)st.wSecond
                        );
                    printf("\n Press any key if you want to quit\n\n");
                }                 
            }        
        }
    } 
    
    getch ();

Cleanup : 
    LocalFree (pinfo4); LocalFree (pJobInfo); //cleanup memory!
    
    for (i=0; i< (int)dwReturned; i++){
        if ( !ClosePrinter( (HANDLE)hPrinter[i] ) )
        {
            printf("ClosePrinter failed: %d\n",GetLastError());
            return 1;
        }
    }
    return (0);
} 

Additional query words:


Keywords          : kbDDK kbNTOS400 kbWinOS2000 kbPrinting 
Version           : winnt:
Platform          : winnt 
Issue type        : kbhowto 

Last Reviewed: April 21, 1999