SplEnumPrinter - Example Code

This example code will print out all queues and printers for the local computer. It will print out both printers that are attached to a queue, and those that are direct printers.

#define INCL_SPL
#define INCL_SPLDOSPRINT
#define INCL_SPLERRORS
#include <os2.h>
#include <stdio.h>      /* for printf function */

INT main ()
{
    PVOID pBuf;
    ULONG fsType ;
    ULONG cbBuf ;
    ULONG cRes ;
    ULONG cTotal ;
    ULONG cbNeeded ;
    SPLERR splerr = 0 ;
    PPRINTERINFO pRes ;

    /* Set fsType to use all the flags. We will print out local device/queues.*/
    fsType = SPL_PR_QUEUE | SPL_PR_DIRECT_DEVICE |
             SPL_PR_QUEUED_DEVICE | SPL_PR_LOCAL_ONLY;

    /* Make function call with cbBuf equal to zero to get a return in cbNeeded*/
    /* of the number of bytes needed for  buffer to hold all the information  */

    splerr = SplEnumPrinter ( NULL,0 ,fsType ,NULL ,NULL ,&cRes ,
                                 &cTotal,&cbNeeded ,NULL ) ;

    /* The error return code will be one of the two following codes if        */
    /* all the parameters were correct.  Otherwise it could be                */
    /* ERROR_INVALID_PARAMETER.                                         */

    if ( splerr == ERROR_MORE_DATA || splerr == NERR_BufTooSmall )
    {
       /* Allocate memory for the buffer using the count of bytes that were   */
       /* returned in cbNeeded.  For simplicity, no error checking is done.   */
       DosAllocMem( &pBuf, cbNeeded,
                    PAG_READ|PAG_WRITE|PAG_COMMIT);

       /* Set count of bytes in buffer to value used to allocate buffer.      */
       cbBuf = cbNeeded;

       /* Call function again with the correct buffer size.                   */
       splerr = SplEnumPrinter  ( NULL,0 ,fsType ,pBuf ,cbBuf ,&cRes ,
                                  &cTotal,&cbNeeded,NULL);

       /* If there are any returned structures in the buffer, then we will    */
       /* print out some of the information.                                  */
       if (cRes)
       {
          pRes = (PPRINTERINFO)pBuf ;
          while ( cRes-- )
          {
             /* Look at the flType element in the pRes structure to determine */
             /* what type of print destination the structure represents.      */
             switch (pRes[cRes].flType)
             {
                case  SPL_PR_QUEUE:
                   printf("Print destination %s is a queue.\n",
                   pRes[cRes].pszPrintDestinationName) ;
                   break;
                case SPL_PR_QUEUED_DEVICE:
                   printf("Print destination %s is a queued printer.\n",
                   pRes[cRes].pszPrintDestinationName) ;
                   break;
                case SPL_PR_DIRECT_DEVICE:
                   printf("Print destination %s is a direct printer.\n",
                   pRes[cRes].pszPrintDestinationName) ;
             }
             printf("Description -
                    %s\n\n",pRes[cRes].pszDescription) ;
          }
       }
       DosFreeMem(pBuf);
    }
    else
    {
       /* If we had any other return code other than ERROR_MORE_DATA or */
       /* NERR_BufTooSmall, we will print out the following information.   */
       printf("SplEnumPrinter error= %ld \n",splerr);
    }
    DosExit( EXIT_PROCESS , 0 ) ;
    return (splerr);
}


[Back: SplEnumPrinter - Related Functions]
[Next: SplEnumPrinter - Topics]