File:  [WindowsNT SDKs] / q_a / samples / printer / enumprt.c
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs
Thu Aug 9 18:29:19 2018 UTC (7 years, 9 months ago) by root
Branches: msft, MAIN
CVS tags: ntsdk-jun-1992, HEAD
Microsoft Windows NT Build 297 06-28-1992

/******************************************************************************\
*
*  MODULE:      ENUMPRT.C
*
*  PURPOSE:     To enumerate all system printers and to display the
*               corresponding PRINTER_INFO_1 and PRINTER_INFO_2 info.
*
*  FUNTIONS:    EnumPrtDlgProc()      - processes dialog messages
*               SetEnumPrtDlgFields() - formats data returned by
*                                       EnumPrinters and inserts in
*                                       listbox.
*
\******************************************************************************/

#include <windows.h>
#include <winspool.h>
#include <string.h>
#include "lookup.h"
#include "enumprt.h"
#include "printer.h"



/******************************************************************************\
*
*  FUNCTION:    EnumPrtDlgProc (standard dialog procedure INPUTS/RETURNS)
*
*  LOCAL VARS:  pPrtInfo1 - pointer to array of PRINTER_INFO_1 structs
*               pPrtInfo2 - pointer to array of PRINTER_INFO_2 structs
*               dwMaxPrt  - number of printers returned by EnumPrinters()
*               dwCurrPrt - index of printer whose info is currently
*                           listed in listbox
*
*  COMMENTS:    On the WM_CREATE we go out & enum all system printers,
*               Then we throw all the info about the first on the the
*               dialog's listbox, and allow user to look at next/prev
*               printer's info also.
*
\******************************************************************************/

BOOL APIENTRY EnumDlgProc (HWND hDlg, UINT message, UINT wParam,
                           LONG lParam)
{
  static LPPRINTER_INFO_1 pPrtInfo1;
  static LPPRINTER_INFO_2 pPrtInfo2;

  static DWORD dwMaxPrt;

  switch (message)
  {
    case WM_INITDIALOG:
    {
      DWORD  dwEnumFlags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_FAVORITE;
      DWORD  dwBytesNeeded;
      DWORD  dwPrtRet1, dwPrtRet2;

      EnumPrinters (dwEnumFlags, NULL, 1, NULL, 0, &dwBytesNeeded,
                    &dwPrtRet1);

      pPrtInfo1 = (LPPRINTER_INFO_1) LocalAlloc (LPTR, dwBytesNeeded + 4);

      EnumPrinters (dwEnumFlags, NULL, 1, (LPBYTE) pPrtInfo1,
                    dwBytesNeeded, &dwBytesNeeded, &dwPrtRet1);

      EnumPrinters (dwEnumFlags, NULL, 2, NULL, 0, &dwBytesNeeded,
                    &dwPrtRet2);

      pPrtInfo2 = (LPPRINTER_INFO_2) LocalAlloc (LPTR, dwBytesNeeded + 4);

      EnumPrinters (dwEnumFlags, NULL, 2, (LPBYTE) pPrtInfo2,
                    dwBytesNeeded, &dwBytesNeeded, &dwPrtRet2);

      dwMaxPrt = dwPrtRet1 > dwPrtRet2 ? dwPrtRet2 : dwPrtRet1;
      SetEnumPrtDlgFields (hDlg, dwMaxPrt, pPrtInfo1, pPrtInfo2);
      break;
    }
    case WM_COMMAND:

      switch (LOWORD(wParam))
      {
        case IDOK:
        {
          LocalFree (LocalHandle ((LPSTR) pPrtInfo1));
          LocalFree (LocalHandle ((LPSTR) pPrtInfo2));
          EndDialog(hDlg, TRUE);
          break;
        }
      }
      return (TRUE);
  }
  return (FALSE);
  UNREFERENCED_PARAMETER(lParam);
}



/******************************************************************************\
*
*  FUNCTION:    SetEnumPrtDlgFields
*
*  LOCAL VARS:  buf - temp string buffer
*               i   - loop variables
*
*  COMMENTS:    This function formats the info returned by EnumPrinters()
*               into readable strings and inserts them in the listbox.
*
\******************************************************************************/

void SetEnumPrtDlgFields (HWND hDlg, DWORD dwMaxPrt,
                          LPPRINTER_INFO_1 pPrtInfo1,
                          LPPRINTER_INFO_2 pPrtInfo2)
{
  char buf[256];
  WORD  i;
  DWORD j;

  SendDlgItemMessage (hDlg, DID_LISTBOX, LB_RESETCONTENT, 0, 0);

  for (j = 0; j < dwMaxPrt; j++)
  {
    /**************************************************************************\
    *  Stick PRINTER_INFO_1  data in listbox
    \**************************************************************************/

    SendDlgItemMessage (hDlg, DID_LISTBOX, LB_INSERTSTRING, (UINT)-1,
                        (LONG) enumstr[0]);

    // nothing for Flags yet

    outstr (enumstr[1], (pPrtInfo1 + j)->pDescription);
    outstr (enumstr[2], (pPrtInfo1 + j)->pName);
    outstr (enumstr[3], (pPrtInfo1 + j)->pComment);


    /**************************************************************************\
    *  Stick PRINTER_INFO_2  data in listbox
    \**************************************************************************/

    SendDlgItemMessage (hDlg, DID_LISTBOX, LB_INSERTSTRING, (UINT)-1,
                        (LONG) enumstr[4]);

    outstr (enumstr[5],  (pPrtInfo2 + j)->pServerName);
    outstr (enumstr[6],  (pPrtInfo2 + j)->pPrinterName);
    outstr (enumstr[7],  (pPrtInfo2 + j)->pShareName);
    outstr (enumstr[8],  (pPrtInfo2 + j)->pPortName);
    outstr (enumstr[9],  (pPrtInfo2 + j)->pDriverName);
    outstr (enumstr[10], (pPrtInfo2 + j)->pComment);
    outstr (enumstr[11], (pPrtInfo2 + j)->pLocation);

    if ((pPrtInfo2 + j)->pDevMode)
    {
      outstr (enumstr[12], "");
      outstr (enumstr[13], (pPrtInfo2 + j)->pDevMode->dmDeviceName);
      outstr (enumstr[14], NULL);
      outstr (enumstr[15], NULL);
      outstr (enumstr[16], NULL);
      outstr (enumstr[17], NULL);
      outstr (enumstr[18], NULL);
      outstr (enumstr[19], NULL);
      outstr (enumstr[20], NULL);
      outstr (enumstr[21], NULL);
      outstr (enumstr[22], NULL);
      outstr (enumstr[23], NULL);
      outstr (enumstr[24], NULL);
      outstr (enumstr[25], NULL);
      outstr (enumstr[26], NULL);
      outstr (enumstr[27], NULL);
    }
    else
    {
      outstr (enumstr[12], NULL);
    }

    outstr (enumstr[28], (pPrtInfo2 + j)->pSepFile);
    outstr (enumstr[29], (pPrtInfo2 + j)->pPrintProcessor);
    outstr (enumstr[30], (pPrtInfo2 + j)->pDatatype);
    outstr (enumstr[31], (pPrtInfo2 + j)->pParameters);

    wsprintf (buf, enumstr[32]);
    for (i = 0; i < MAX_NUM_PRT_ATTRIBUTES; i++)

      if ((pPrtInfo2 + j)->Attributes & attributes[i].x.dwIndex)

        strcat (buf, attributes[i].lpszValue);

    outstr2 (buf);

    for (i = 0; i < MAX_NUM_PRT_PRIORITIES; i++)

      if ((pPrtInfo2 + j)->Priority & priorities[i].x.dwIndex)
      {
        outstr (enumstr[33], priorities[i].lpszValue);
        break;
      }
    if (i == MAX_NUM_PRT_PRIORITIES)

      outnum (enumstr[34], (pPrtInfo2 + j)->Priority);

    outnum (enumstr[35], (pPrtInfo2 + j)->DefaultPriority);
    outnum (enumstr[36], (pPrtInfo2 + j)->StartTime);
    outnum (enumstr[37], (pPrtInfo2 + j)->UntilTime);

    outstr2 (enumstr[38]);
    for (i = 0; i < MAX_NUM_PRT_STATUS; i++)

      if ((pPrtInfo2 + j)->Status & status[i].x.dwIndex)

        outstr2 (status[i].lpszValue);

    outnum (enumstr[39], (pPrtInfo2 + j)->cJobs);
    outnum (enumstr[40], (pPrtInfo2 + j)->AveragePPM);
  }
}

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.