Annotation of q_a/samples/printer/enumprt.c, revision 1.1.1.1

1.1       root        1: /******************************************************************************\
                      2: *
                      3: *  MODULE:      ENUMPRT.C
                      4: *
                      5: *  PURPOSE:     To enumerate all system printers and to display the
                      6: *               corresponding PRINTER_INFO_1 and PRINTER_INFO_2 info.
                      7: *
                      8: *  FUNTIONS:    EnumPrtDlgProc()      - processes dialog messages
                      9: *               SetEnumPrtDlgFields() - formats data returned by
                     10: *                                       EnumPrinters and inserts in
                     11: *                                       listbox.
                     12: *
                     13: \******************************************************************************/
                     14: 
                     15: #include <windows.h>
                     16: #include <winspool.h>
                     17: #include <string.h>
                     18: #include "lookup.h"
                     19: #include "enumprt.h"
                     20: #include "printer.h"
                     21: 
                     22: 
                     23: 
                     24: /******************************************************************************\
                     25: *
                     26: *  FUNCTION:    EnumPrtDlgProc (standard dialog procedure INPUTS/RETURNS)
                     27: *
                     28: *  LOCAL VARS:  pPrtInfo1 - pointer to array of PRINTER_INFO_1 structs
                     29: *               pPrtInfo2 - pointer to array of PRINTER_INFO_2 structs
                     30: *               dwMaxPrt  - number of printers returned by EnumPrinters()
                     31: *               dwCurrPrt - index of printer whose info is currently
                     32: *                           listed in listbox
                     33: *
                     34: *  COMMENTS:    On the WM_CREATE we go out & enum all system printers,
                     35: *               Then we throw all the info about the first on the the
                     36: *               dialog's listbox, and allow user to look at next/prev
                     37: *               printer's info also.
                     38: *
                     39: \******************************************************************************/
                     40: 
                     41: BOOL APIENTRY EnumDlgProc (HWND hDlg, UINT message, UINT wParam,
                     42:                            LONG lParam)
                     43: {
                     44:   static LPPRINTER_INFO_1 pPrtInfo1;
                     45:   static LPPRINTER_INFO_2 pPrtInfo2;
                     46: 
                     47:   static DWORD dwMaxPrt;
                     48: 
                     49:   switch (message)
                     50:   {
                     51:     case WM_INITDIALOG:
                     52:     {
                     53:       DWORD  dwEnumFlags = PRINTER_ENUM_LOCAL | PRINTER_ENUM_FAVORITE;
                     54:       DWORD  dwBytesNeeded;
                     55:       DWORD  dwPrtRet1, dwPrtRet2;
                     56: 
                     57:       EnumPrinters (dwEnumFlags, NULL, 1, NULL, 0, &dwBytesNeeded,
                     58:                     &dwPrtRet1);
                     59: 
                     60:       pPrtInfo1 = (LPPRINTER_INFO_1) LocalAlloc (LPTR, dwBytesNeeded + 4);
                     61: 
                     62:       EnumPrinters (dwEnumFlags, NULL, 1, (LPBYTE) pPrtInfo1,
                     63:                     dwBytesNeeded, &dwBytesNeeded, &dwPrtRet1);
                     64: 
                     65:       EnumPrinters (dwEnumFlags, NULL, 2, NULL, 0, &dwBytesNeeded,
                     66:                     &dwPrtRet2);
                     67: 
                     68:       pPrtInfo2 = (LPPRINTER_INFO_2) LocalAlloc (LPTR, dwBytesNeeded + 4);
                     69: 
                     70:       EnumPrinters (dwEnumFlags, NULL, 2, (LPBYTE) pPrtInfo2,
                     71:                     dwBytesNeeded, &dwBytesNeeded, &dwPrtRet2);
                     72: 
                     73:       dwMaxPrt = dwPrtRet1 > dwPrtRet2 ? dwPrtRet2 : dwPrtRet1;
                     74:       SetEnumPrtDlgFields (hDlg, dwMaxPrt, pPrtInfo1, pPrtInfo2);
                     75:       break;
                     76:     }
                     77:     case WM_COMMAND:
                     78: 
                     79:       switch (LOWORD(wParam))
                     80:       {
                     81:         case IDOK:
                     82:         {
                     83:           LocalFree (LocalHandle ((LPSTR) pPrtInfo1));
                     84:           LocalFree (LocalHandle ((LPSTR) pPrtInfo2));
                     85:           EndDialog(hDlg, TRUE);
                     86:           break;
                     87:         }
                     88:       }
                     89:       return (TRUE);
                     90:   }
                     91:   return (FALSE);
                     92:   UNREFERENCED_PARAMETER(lParam);
                     93: }
                     94: 
                     95: 
                     96: 
                     97: /******************************************************************************\
                     98: *
                     99: *  FUNCTION:    SetEnumPrtDlgFields
                    100: *
                    101: *  LOCAL VARS:  buf - temp string buffer
                    102: *               i   - loop variables
                    103: *
                    104: *  COMMENTS:    This function formats the info returned by EnumPrinters()
                    105: *               into readable strings and inserts them in the listbox.
                    106: *
                    107: \******************************************************************************/
                    108: 
                    109: void SetEnumPrtDlgFields (HWND hDlg, DWORD dwMaxPrt,
                    110:                           LPPRINTER_INFO_1 pPrtInfo1,
                    111:                           LPPRINTER_INFO_2 pPrtInfo2)
                    112: {
                    113:   char buf[256];
                    114:   WORD  i;
                    115:   DWORD j;
                    116: 
                    117:   SendDlgItemMessage (hDlg, DID_LISTBOX, LB_RESETCONTENT, 0, 0);
                    118: 
                    119:   for (j = 0; j < dwMaxPrt; j++)
                    120:   {
                    121:     /**************************************************************************\
                    122:     *  Stick PRINTER_INFO_1  data in listbox
                    123:     \**************************************************************************/
                    124: 
                    125:     SendDlgItemMessage (hDlg, DID_LISTBOX, LB_INSERTSTRING, (UINT)-1,
                    126:                         (LONG) enumstr[0]);
                    127: 
                    128:     // nothing for Flags yet
                    129: 
                    130:     outstr (enumstr[1], (pPrtInfo1 + j)->pDescription);
                    131:     outstr (enumstr[2], (pPrtInfo1 + j)->pName);
                    132:     outstr (enumstr[3], (pPrtInfo1 + j)->pComment);
                    133: 
                    134: 
                    135:     /**************************************************************************\
                    136:     *  Stick PRINTER_INFO_2  data in listbox
                    137:     \**************************************************************************/
                    138: 
                    139:     SendDlgItemMessage (hDlg, DID_LISTBOX, LB_INSERTSTRING, (UINT)-1,
                    140:                         (LONG) enumstr[4]);
                    141: 
                    142:     outstr (enumstr[5],  (pPrtInfo2 + j)->pServerName);
                    143:     outstr (enumstr[6],  (pPrtInfo2 + j)->pPrinterName);
                    144:     outstr (enumstr[7],  (pPrtInfo2 + j)->pShareName);
                    145:     outstr (enumstr[8],  (pPrtInfo2 + j)->pPortName);
                    146:     outstr (enumstr[9],  (pPrtInfo2 + j)->pDriverName);
                    147:     outstr (enumstr[10], (pPrtInfo2 + j)->pComment);
                    148:     outstr (enumstr[11], (pPrtInfo2 + j)->pLocation);
                    149: 
                    150:     if ((pPrtInfo2 + j)->pDevMode)
                    151:     {
                    152:       outstr (enumstr[12], "");
                    153:       outstr (enumstr[13], (pPrtInfo2 + j)->pDevMode->dmDeviceName);
                    154:       outstr (enumstr[14], NULL);
                    155:       outstr (enumstr[15], NULL);
                    156:       outstr (enumstr[16], NULL);
                    157:       outstr (enumstr[17], NULL);
                    158:       outstr (enumstr[18], NULL);
                    159:       outstr (enumstr[19], NULL);
                    160:       outstr (enumstr[20], NULL);
                    161:       outstr (enumstr[21], NULL);
                    162:       outstr (enumstr[22], NULL);
                    163:       outstr (enumstr[23], NULL);
                    164:       outstr (enumstr[24], NULL);
                    165:       outstr (enumstr[25], NULL);
                    166:       outstr (enumstr[26], NULL);
                    167:       outstr (enumstr[27], NULL);
                    168:     }
                    169:     else
                    170:     {
                    171:       outstr (enumstr[12], NULL);
                    172:     }
                    173: 
                    174:     outstr (enumstr[28], (pPrtInfo2 + j)->pSepFile);
                    175:     outstr (enumstr[29], (pPrtInfo2 + j)->pPrintProcessor);
                    176:     outstr (enumstr[30], (pPrtInfo2 + j)->pDatatype);
                    177:     outstr (enumstr[31], (pPrtInfo2 + j)->pParameters);
                    178: 
                    179:     wsprintf (buf, enumstr[32]);
                    180:     for (i = 0; i < MAX_NUM_PRT_ATTRIBUTES; i++)
                    181: 
                    182:       if ((pPrtInfo2 + j)->Attributes & attributes[i].x.dwIndex)
                    183: 
                    184:         strcat (buf, attributes[i].lpszValue);
                    185: 
                    186:     outstr2 (buf);
                    187: 
                    188:     for (i = 0; i < MAX_NUM_PRT_PRIORITIES; i++)
                    189: 
                    190:       if ((pPrtInfo2 + j)->Priority & priorities[i].x.dwIndex)
                    191:       {
                    192:         outstr (enumstr[33], priorities[i].lpszValue);
                    193:         break;
                    194:       }
                    195:     if (i == MAX_NUM_PRT_PRIORITIES)
                    196: 
                    197:       outnum (enumstr[34], (pPrtInfo2 + j)->Priority);
                    198: 
                    199:     outnum (enumstr[35], (pPrtInfo2 + j)->DefaultPriority);
                    200:     outnum (enumstr[36], (pPrtInfo2 + j)->StartTime);
                    201:     outnum (enumstr[37], (pPrtInfo2 + j)->UntilTime);
                    202: 
                    203:     outstr2 (enumstr[38]);
                    204:     for (i = 0; i < MAX_NUM_PRT_STATUS; i++)
                    205: 
                    206:       if ((pPrtInfo2 + j)->Status & status[i].x.dwIndex)
                    207: 
                    208:         outstr2 (status[i].lpszValue);
                    209: 
                    210:     outnum (enumstr[39], (pPrtInfo2 + j)->cJobs);
                    211:     outnum (enumstr[40], (pPrtInfo2 + j)->AveragePPM);
                    212:   }
                    213: }

unix.superglobalmegacorp.com

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