home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / gdi / printer / enumprt.c < prev    next >
C/C++ Source or Header  |  1997-10-05  |  21KB  |  721 lines

  1.  
  2. /******************************************************************************\
  3. *       This is a part of the Microsoft Source Code Samples. 
  4. *       Copyright (C) 1993-1997 Microsoft Corporation.
  5. *       All rights reserved. 
  6. *       This source code is only intended as a supplement to 
  7. *       Microsoft Development Tools and/or WinHelp documentation.
  8. *       See these sources for detailed information regarding the 
  9. *       Microsoft samples programs.
  10. \******************************************************************************/
  11.  
  12. /******************************************************************************\
  13. *
  14. *  PROGRAM:     ENUMPRT.C
  15. *
  16. *  PURPOSE:     Handles display of information returned by calls to
  17. *               EnumPrinters, EnumPrinterDrivers. Info formatted and
  18. *               displayed in a dialog box.
  19. *
  20. *
  21. *  FUNTIONS:    EnumPrintersDlgProc      - handles messages for dialog
  22. *               DisplayEnumPrintersInfo  - retrieves printer info
  23. *               SetEnumPrintersDlgFields - formats & displays printer info
  24. *               ComplexEnumPrintersLine  - formats bitfield printer info
  25. *               EnumPrinterDriversDlgProc- handles messages for dialog
  26. *               DisplayPrinterDriversInfo- retrieves, formats, & displays
  27. *                                            printer info
  28. *
  29. \******************************************************************************/
  30.  
  31. #include <windows.h>
  32. #include <string.h>
  33. #include <drivinit.h>
  34. #include <stdio.h>
  35. #include <winspool.h>
  36. #include "common.h"
  37. #include "enumprt.h"
  38.  
  39.  
  40.  
  41. /******************************************************************************\
  42. *
  43. *  FUNCTION:    EnumPrintersDlgProc (standard dialog procedure INPUTS/RETURNS)
  44. *
  45. *  COMMENTS:    Processes messages for EnumPrinters dialog box
  46. *
  47. \******************************************************************************/
  48.  
  49. LRESULT CALLBACK EnumPrintersDlgProc (HWND   hwnd, UINT msg, WPARAM wParam,
  50.                                       LPARAM lParam)
  51. {
  52.   switch (msg)
  53.   {
  54.     case WM_INITDIALOG:
  55.     {
  56.       BOOL bReturn;
  57.  
  58.       //
  59.       // prompt user for EnumPrinters flags...
  60.       //
  61.  
  62.       if (DialogBox (GetModuleHandle (NULL), (LPCTSTR) "EnumPrtOpt",
  63.                      NULL, (DLGPROC) EnumPrintersOptionsDlgProc))
  64.       {
  65.         //
  66.         // shove all the enum printer info in the list box
  67.         //
  68.  
  69.         SetCursor (LoadCursor (NULL, IDC_WAIT));
  70.         bReturn = DisplayEnumPrintersInfo (hwnd);
  71.         SetCursor (LoadCursor (NULL, IDC_ARROW));
  72.  
  73.         if (!bReturn)
  74.  
  75.           EndDialog (hwnd, TRUE);
  76.  
  77.         else
  78.  
  79.           SetWindowText (hwnd, (LPCTSTR)"EnumPrinters");
  80.       }
  81.       else
  82.  
  83.           EndDialog (hwnd, TRUE);
  84.  
  85.       break;
  86.     }
  87.  
  88.     case WM_COMMAND:
  89.  
  90.       switch (LOWORD (wParam))
  91.       {
  92.         case DID_OK:
  93.  
  94.           EndDialog (hwnd, TRUE);
  95.           break;
  96.       }
  97.       break;
  98.   }
  99.   return 0;
  100. }
  101.  
  102.  
  103.  
  104. /******************************************************************************\
  105. *
  106. *  FUNCTION:    EnumPrintersOptionsDlgProc (standard dlg proc INPUTS/RETURNS)
  107. *
  108. *  COMMENTS:    Processes messages for EnumPrtOpt dialog box
  109. *
  110. \******************************************************************************/
  111.  
  112. LRESULT CALLBACK EnumPrintersOptionsDlgProc (HWND   hwnd,   UINT msg,
  113.                                              WPARAM wParam, LPARAM lParam)
  114. {
  115.   switch (msg)
  116.   {
  117.     case WM_INITDIALOG:
  118.  
  119.       gdwEnumFlags = 0;
  120.       gszEnumName[0] = 0;
  121.       break;
  122.  
  123.     case WM_COMMAND:
  124.  
  125.       switch (LOWORD (wParam))
  126.       {
  127.         case DID_OK:
  128.  
  129.           if (gdwEnumFlags)
  130.           {
  131.             if (gdwEnumFlags & PRINTER_ENUM_NAME)
  132.             {
  133.               GetDlgItemText (hwnd, DID_EDITTEXT, (LPTSTR)gszEnumName,
  134.                               BUFSIZE);
  135.  
  136. #ifdef FORCE_VALID_NAME
  137.               if (!strlen (gszEnumName))
  138.               {
  139.                 MessageBox (hwnd,
  140.                             (LPCTSTR) GetStringRes(IDS_ASKDOMSRVNM),
  141.                             (LPCTSTR) "", MB_OK);
  142.                 SetFocus (GetDlgItem (hwnd, DID_EDITTEXT));
  143.                 break;
  144.               }
  145. #endif
  146.  
  147.             }
  148.  
  149.             EndDialog (hwnd, TRUE);
  150.           }
  151.  
  152.           else
  153.  
  154.             EndDialog (hwnd, FALSE);
  155.  
  156.           break;
  157.  
  158.         case DID_CANCEL:
  159.  
  160.           EndDialog (hwnd, FALSE);
  161.           break;
  162.  
  163.         default:
  164.  
  165.           if (HIWORD(wParam) == BN_CLICKED)
  166.           {
  167.             DWORD dwControlId = (DWORD) LOWORD (wParam);
  168.  
  169.             if (gdwEnumFlags & dwControlId)
  170.             {
  171.               //
  172.               // remove that flag, if PRINTER_ENUM_NAME disable edittext
  173.               //
  174.  
  175.               gdwEnumFlags &= ~dwControlId;
  176.  
  177.               if (dwControlId & PRINTER_ENUM_NAME)
  178.               {
  179.                 SetDlgItemText (hwnd, DID_EDITTEXT, (LPCTSTR)"");
  180.                 EnableWindow   (GetDlgItem (hwnd, DID_EDITTEXT), FALSE);
  181.               }
  182.             }
  183.  
  184.             else
  185.             {
  186.               //
  187.               // add that flag, if PRINTER_ENUM_NAME enable edittext
  188.               //
  189.  
  190.               gdwEnumFlags |= dwControlId;
  191.  
  192.               if (dwControlId & PRINTER_ENUM_NAME)
  193.  
  194.                 EnableWindow (GetDlgItem (hwnd, DID_EDITTEXT), TRUE);
  195.             }
  196.           }
  197.           break;
  198.       }
  199.   }
  200.   return 0;
  201. }
  202.  
  203.  
  204.  
  205. /******************************************************************************\
  206. *
  207. *  FUNCTION:    DisplayEnumPrintersInfo
  208. *
  209. *  INPUTS:      hwnd - handle of the EnumPrinters dialog box
  210. *
  211. *  RETURNS:     TRUE if successful,
  212. *               FALSE otherwise
  213. *
  214. \******************************************************************************/
  215.  
  216. BOOL DisplayEnumPrintersInfo (HWND hwnd)
  217. {
  218.   DWORD  dwBytesNeeded;
  219.   DWORD  dwPrtRet1, dwPrtRet2;
  220.   DWORD  dwMaxPrt;
  221.   LPTSTR lpName = gdwEnumFlags & PRINTER_ENUM_NAME ? gszEnumName : NULL;
  222.  
  223.   LPPRINTER_INFO_1 pPrtInfo1;
  224.   LPPRINTER_INFO_2 pPrtInfo2;
  225.  
  226.   BOOL   bReturn = TRUE;
  227.  
  228.   //
  229.   // get byte count needed for buffer, alloc buffer, the enum the printers
  230.   //
  231.  
  232.   EnumPrinters (gdwEnumFlags, lpName, 1, NULL, 0, &dwBytesNeeded,
  233.                      &dwPrtRet1);
  234.  
  235.   //
  236.   // (simple error checking, if these work assume rest will too)
  237.   //
  238.  
  239.   if (!(pPrtInfo1 = (LPPRINTER_INFO_1) LocalAlloc (LPTR, dwBytesNeeded)))
  240.   {
  241.     ErrMsgBox (GetStringRes(IDS_ENUMPRTLALLOCFAIL), GetStringRes2(ERR_MOD_NAME));
  242.     bReturn = FALSE;
  243.     goto display_prts_info_done1;
  244.   }
  245.  
  246.   if (!EnumPrinters (gdwEnumFlags, lpName, 1, (LPBYTE) pPrtInfo1,
  247.                 dwBytesNeeded, &dwBytesNeeded, &dwPrtRet1))
  248.   {
  249.     TCHAR  tcBuffer[256];
  250.     wsprintf (tcBuffer, "%s, 1, GetLastError: %d", GetStringRes2(ERR_MOD_NAME), GetLastError());
  251.     ErrMsgBox (GetStringRes(IDS_ENUMPRT1FAIL), tcBuffer);
  252.     bReturn = FALSE;
  253.     goto display_prts_info_done2;
  254.   }
  255.  
  256.   //
  257.   // If we don't get any printers from the Level == 1 call, there is
  258.   //  no point in continuing... report it, free memory, and return.
  259.   //
  260.  
  261.   if (dwPrtRet1 == 0)
  262.   {
  263.     MessageBox (ghwndMain,
  264.                (LPCTSTR) "EnumPrinters (Level == 1) returned 0 printers",
  265.                GetStringRes2(ERR_MOD_NAME),
  266.                MB_OK);
  267.     bReturn = FALSE;
  268.     goto display_prts_info_done2;
  269.   }
  270.  
  271.  
  272.   //
  273.   // Call EnumPrinters again with Level == 2.
  274.   //
  275.   // get byte count needed for buffer, alloc buffer, the enum the printers
  276.   //
  277.  
  278.   EnumPrinters (gdwEnumFlags, lpName, 2, NULL, 0, &dwBytesNeeded,
  279.                 &dwPrtRet2);
  280.  
  281.   pPrtInfo2 = (LPPRINTER_INFO_2) LocalAlloc (LPTR, dwBytesNeeded);
  282.  
  283.   EnumPrinters (gdwEnumFlags, lpName, 2, (LPBYTE) pPrtInfo2,
  284.                 dwBytesNeeded, &dwBytesNeeded, &dwPrtRet2);
  285.  
  286.   //
  287.   // Calling EnumPrinters with Level == 2 frequently returns 0 printers.
  288.   //  If so display only the PRINTER_INFO_1 structures we got before.
  289.   //
  290.  
  291.   if (dwPrtRet2 == 0)
  292.   {
  293.     dwMaxPrt = dwPrtRet1;
  294.     pPrtInfo2 = NULL;
  295.   } else {
  296.   dwMaxPrt = dwPrtRet1 > dwPrtRet2 ? dwPrtRet2 : dwPrtRet1;
  297.  
  298.   }
  299.  
  300.  
  301.  
  302.   SetEnumPrintersDlgFields (hwnd, dwMaxPrt, pPrtInfo1, pPrtInfo2);
  303.  
  304.  
  305.   LocalFree (LocalHandle (pPrtInfo2));
  306.  
  307. display_prts_info_done2:
  308.  
  309.   LocalFree (LocalHandle (pPrtInfo1));
  310.  
  311. display_prts_info_done1:
  312.  
  313.   return bReturn;
  314. }
  315.  
  316.  
  317. /******************************************************************************\
  318. *
  319. *  FUNCTION:    SetEnumPrintersDlgFields
  320. *
  321. *  INPUTS:      hwnd      - handle of the EnumPrinters dialog box
  322. *               dwMaxPrt  - number of elements in the following two arrays
  323. *               pdisplay_prts_info1 - ptr to an array of PRINTER_INFO_1 structs
  324. *               pdisplay_prts_info2 - ptr to an array of PRINTER_INFO_2 structs
  325. *
  326. *  COMMENTS:    This function formats the info returned by EnumPrinters()
  327. *               into readable strings and inserts them in the listbox.
  328. *
  329. \******************************************************************************/
  330.  
  331. void SetEnumPrintersDlgFields (HWND hwnd, DWORD dwMaxPrt,
  332.                                LPPRINTER_INFO_1 pPrtInfo1,
  333.                                LPPRINTER_INFO_2 pPrtInfo2)
  334. {
  335.   char  buf[256];
  336.   WORD  i;
  337.   DWORD j;
  338.  
  339.   SendDlgItemMessage (hwnd, DID_LISTBOX, LB_RESETCONTENT, 0, 0);
  340.  
  341.   for (j = 0; j < dwMaxPrt; j++) {
  342.     //
  343.     // Stick PRINTER_INFO_1  data in listbox
  344.     //
  345.  
  346.     SendDlgItemMessage (hwnd, DID_LISTBOX, LB_INSERTSTRING, (UINT)-1,
  347.                         (LONG) gaEnumPrt[0]);
  348.  
  349.     outstr (gaEnumPrt[1], (pPrtInfo1 + j)->pDescription);
  350.     outstr (gaEnumPrt[2], (pPrtInfo1 + j)->pName);
  351.     outstr (gaEnumPrt[3], (pPrtInfo1 + j)->pComment);
  352.  
  353.     //
  354.     // Stick PRINTER_INFO_2  data in listbox
  355.     //
  356.  
  357.     if (pPrtInfo2 != NULL) {
  358.  
  359.     SendDlgItemMessage (hwnd, DID_LISTBOX, LB_INSERTSTRING, (UINT)-1,
  360.                         (LONG) gaEnumPrt[4]);
  361.  
  362.     outstr (gaEnumPrt[5],  (pPrtInfo2 + j)->pServerName);
  363.     outstr (gaEnumPrt[6],  (pPrtInfo2 + j)->pPrinterName);
  364.     outstr (gaEnumPrt[7],  (pPrtInfo2 + j)->pShareName);
  365.     outstr (gaEnumPrt[8],  (pPrtInfo2 + j)->pPortName);
  366.     outstr (gaEnumPrt[9],  (pPrtInfo2 + j)->pDriverName);
  367.     outstr (gaEnumPrt[10], (pPrtInfo2 + j)->pComment);
  368.     outstr (gaEnumPrt[11], (pPrtInfo2 + j)->pLocation);
  369.  
  370.     if ((pPrtInfo2 + j)->pDevMode)
  371.     {
  372.       DWORD dwFields;
  373.  
  374.       outstr (gaEnumPrt[12], "");
  375.       outstr (gaEnumPrt[13], (pPrtInfo2 + j)->pDevMode->dmDeviceName);
  376.       outnum (gaEnumPrt[14], (pPrtInfo2 + j)->pDevMode->dmSpecVersion);
  377.       outnum (gaEnumPrt[15], (pPrtInfo2 + j)->pDevMode->dmDriverVersion);
  378.       outnum (gaEnumPrt[16], (pPrtInfo2 + j)->pDevMode->dmSize);
  379.       outnum (gaEnumPrt[17], (pPrtInfo2 + j)->pDevMode->dmDriverExtra);
  380.  
  381.       dwFields = (pPrtInfo2 + j)->pDevMode->dmFields;
  382.       ComplexEnumPrintersLine (hwnd, gaEnumPrt[18], gaFields, MAX_FIELDS,
  383.                                dwFields);
  384.  
  385.       if (dwFields & DM_ORIENTATION)
  386.  
  387.         ComplexEnumPrintersLine (hwnd, gaEnumPrt[19], gaOrientation,
  388.                                  MAX_ORIENTATION,
  389.                                  (DWORD)
  390.                                  (pPrtInfo2+j)->pDevMode->dmOrientation);
  391.  
  392.       if (dwFields & DM_PAPERSIZE)
  393.  
  394.         ComplexEnumPrintersLine (hwnd, gaEnumPrt[20], gaPaperSize,
  395.                                  MAX_PAPERSIZE,
  396.                                  (DWORD)(pPrtInfo2 + j)->pDevMode->dmPaperSize);
  397.  
  398.       if (dwFields & DM_PAPERLENGTH)
  399.  
  400.         outnum (gaEnumPrt[21], (pPrtInfo2 + j)->pDevMode->dmPaperLength);
  401.  
  402.  
  403.       if (dwFields & DM_PAPERWIDTH)
  404.  
  405.         outnum (gaEnumPrt[22], (pPrtInfo2 + j)->pDevMode->dmPaperWidth);
  406.  
  407.       if (dwFields & DM_SCALE)
  408.  
  409.         outnum (gaEnumPrt[23], (pPrtInfo2 + j)->pDevMode->dmScale);
  410.  
  411.       if (dwFields & DM_COPIES)
  412.  
  413.         outnum (gaEnumPrt[24], (pPrtInfo2 + j)->pDevMode->dmCopies);
  414.  
  415.       if (dwFields & DM_DEFAULTSOURCE)
  416.  
  417.         ComplexEnumPrintersLine (hwnd, gaEnumPrt[25], gaDefaultSource,
  418.                                  MAX_DEFAULTSOURCE,
  419.                                (DWORD)(pPrtInfo2+j)->pDevMode->dmDefaultSource);
  420.  
  421.       if (dwFields & DM_PRINTQUALITY)
  422.  
  423.         ComplexEnumPrintersLine (hwnd, gaEnumPrt[26], gaPrintQuality,
  424.                                  MAX_PRINTQUALITY,
  425.                                 (DWORD)(pPrtInfo2+j)->pDevMode->dmPrintQuality);
  426.  
  427.       if (dwFields & DM_COLOR)
  428.  
  429.         ComplexEnumPrintersLine (hwnd, gaEnumPrt[27], gaColor,
  430.                                  MAX_COLOR,
  431.                                  (DWORD)(pPrtInfo2 + j)->pDevMode->dmColor);
  432.  
  433.       if (dwFields & DM_DUPLEX)
  434.  
  435.         ComplexEnumPrintersLine (hwnd, gaEnumPrt[28], gaDuplex,
  436.                                  MAX_DUPLEX,
  437.                                  (DWORD)(pPrtInfo2 + j)->pDevMode->dmDuplex);
  438.  
  439.       if (dwFields & DM_YRESOLUTION)
  440.  
  441.         outnum (gaEnumPrt[29], NULL);
  442.  
  443.       if (dwFields & DM_TTOPTION)
  444.  
  445.         outnum (gaEnumPrt[29], NULL);
  446.  
  447.       if (dwFields & DM_COLLATE)
  448.  
  449.         outnum (gaEnumPrt[30], NULL);
  450.  
  451.       if (dwFields & DM_FORMNAME)
  452.  
  453.         outnum (gaEnumPrt[31], NULL);
  454.     }
  455.     else
  456.     {
  457.       outstr (gaEnumPrt[12], NULL);
  458.     }
  459.  
  460.     outstr (gaEnumPrt[33], (pPrtInfo2 + j)->pSepFile);
  461.     outstr (gaEnumPrt[34], (pPrtInfo2 + j)->pPrintProcessor);
  462.     outstr (gaEnumPrt[35], (pPrtInfo2 + j)->pDatatype);
  463.     outstr (gaEnumPrt[36], (pPrtInfo2 + j)->pParameters);
  464.  
  465.     ComplexEnumPrintersLine (hwnd, gaEnumPrt[37], gaAttributes,
  466.                              MAX_ATTRIBUTES,
  467.                              (pPrtInfo2 + j)->Attributes);
  468.  
  469.     for (i = 0; i < MAX_PRIORITIES; i++)
  470.  
  471.       if ((pPrtInfo2 + j)->Priority & gaPriorities[i].dwValue)
  472.       {
  473.         outstr (gaEnumPrt[38], gaPriorities[i].szValue);
  474.         break;
  475.       }
  476.  
  477.     if (i == MAX_PRIORITIES)
  478.  
  479.       outnum (gaEnumPrt[39], (pPrtInfo2 + j)->Priority);
  480.  
  481.     outnum (gaEnumPrt[40], (pPrtInfo2 + j)->DefaultPriority);
  482.     outnum (gaEnumPrt[41], (pPrtInfo2 + j)->StartTime);
  483.     outnum (gaEnumPrt[42], (pPrtInfo2 + j)->UntilTime);
  484.  
  485.     ComplexEnumPrintersLine (hwnd, gaEnumPrt[43], gaStatus, MAX_STATUS,
  486.                              (pPrtInfo2 + j)->Status);
  487.  
  488.     outnum (gaEnumPrt[44], (pPrtInfo2 + j)->cJobs);
  489.     outnum (gaEnumPrt[45], (pPrtInfo2 + j)->AveragePPM);
  490.   }
  491. }
  492. }
  493.  
  494.  
  495.  
  496. /******************************************************************************\
  497. *
  498. *  FUNCTION:    ComplexEnumPrintersLine
  499. *
  500. *  INPUTS:      hwnd        - handle of the EnumPrinters dialog box
  501. *               pbuf        - pointer to buffer containing a cap-type
  502. *                              string
  503. *               pLkUp       - pointer to a CAPSLOOKUP table
  504. *               iMaxEntries - # of enries in table pointed at by pLkUp
  505. *               iValue      - an integer containing 1+ bit-value flags.
  506. *
  507. *  COMMENTS:    This function is used to expand an int containing
  508. *               multiple bit-values into a set of strings which are
  509. *               inserted into the DevCapsDlg listbox. The iValue
  510. *               parameter is checked against each iIndex entry in the
  511. *               CAPSLOOKUP table pointed at by pLkUp, and when matches
  512. *               are found the corresponding (lpszValue) string is
  513. *               inserted.
  514. *
  515. *               The buffer pointed to by pbuf will be destroyed.
  516. *
  517. \******************************************************************************/
  518.  
  519. void ComplexEnumPrintersLine (HWND hwnd, char  *pstr, ENUMPRTLOOKUP *pLkUp,
  520.                               int iMaxEntries, DWORD  dwValue)
  521. {
  522.   char buf [BUFSIZE];
  523.   int  i;
  524.   BOOL bNewLine = FALSE;
  525.  
  526.   strcpy (buf, pstr);
  527.  
  528.   for (i = 0; i < iMaxEntries; i++)
  529.  
  530.     if (dwValue & (pLkUp + i)->dwValue)
  531.     {
  532.       if (bNewLine)
  533.       {
  534.         //
  535.         // Keep the first symbolic constant on the same line as the
  536.         //   cap type, eg:  "TECHNOLOGY:     DT_RASDISPLAY".
  537.         //
  538.  
  539.         strcpy (buf, BLANKS);
  540.         strcat (buf, (pLkUp + i)->szValue);
  541.       }
  542.       else
  543.       {
  544.         //
  545.         // Put symbolic constant on new line, eg:
  546.         //                  "                DT_RASPRINTER".
  547.         //
  548.  
  549.         strcat (buf, (pLkUp + i)->szValue);
  550.         bNewLine = TRUE;
  551.       }
  552.       SendDlgItemMessage (hwnd, DID_LISTBOX, LB_INSERTSTRING,
  553.                           (UINT)-1, (LONG) buf);
  554.    }
  555. }
  556.  
  557.  
  558.  
  559. /******************************************************************************\
  560. *
  561. *  FUNCTION:    EnumPrinterDriversDlgProc (standard dlg proc INPUTS/RETURNS)
  562. *
  563. *  COMMENTS:    Processes messages for EnumPrinterDrivers dialog box
  564. *
  565. \******************************************************************************/
  566.  
  567. LRESULT CALLBACK EnumPrinterDriversDlgProc (HWND   hwnd,   UINT   msg,
  568.                                             WPARAM wParam, LPARAM lParam)
  569. {
  570.   switch (msg)
  571.   {
  572.     case WM_INITDIALOG:
  573.     {
  574.       BOOL bReturn;
  575.  
  576.       SetCursor (LoadCursor (NULL, IDC_WAIT));
  577.       bReturn = DisplayPrinterDriversInfo (hwnd);
  578.       SetCursor (LoadCursor (NULL, IDC_ARROW));
  579.  
  580.       if (!bReturn)
  581.  
  582.         EndDialog (hwnd, TRUE);
  583.  
  584.       else
  585.  
  586.         SetWindowText (hwnd, (LPCTSTR) "EnumPrinterDrivers");
  587.  
  588.       break;
  589.     }
  590.  
  591.     case WM_COMMAND:
  592.  
  593.       switch (LOWORD (wParam))
  594.       {
  595.         case DID_OK:
  596.  
  597.           EndDialog (hwnd, TRUE);
  598.  
  599.           return 1;
  600.       }
  601.       break;
  602.   }
  603.   return 0;
  604. }
  605.  
  606.  
  607.  
  608. /******************************************************************************\
  609. *
  610. *  FUNCTION:    DisplayPrinterDriversInfo
  611. *
  612. *  INPUTS:      hwnd - handle of the EnumPrinterDrivers dialog box
  613. *
  614. *  RETURNS:     TRUE if successful,
  615. *               FALSE otherwise
  616. *
  617. *  COMMENTS:    Retrieves EnumPrinterDrivers info, formats, & inserts
  618. *               in listbox.
  619. *
  620. \******************************************************************************/
  621.  
  622. BOOL DisplayPrinterDriversInfo (HWND hwnd)
  623. {
  624.   DWORD         dwBytesNeeded, dwDrvRet, i;
  625.   DRIVER_INFO_1 *pDriverInfo1;
  626.   DRIVER_INFO_2 *pDriverInfo2;
  627.   char          buf[BUFSIZE];
  628.   BOOL          bReturn = TRUE;
  629.  
  630.   //
  631.   // get byte count needed for buffer, alloc buffer, the enum the drivers
  632.   //
  633.  
  634.   EnumPrinterDrivers ((LPTSTR) NULL, (LPTSTR) NULL, 1, NULL,
  635.                       0, &dwBytesNeeded, &dwDrvRet);
  636.  
  637.   //
  638.   // simple error checking, if these work assume rest will too
  639.   //
  640.  
  641.   if (!(pDriverInfo1 = (DRIVER_INFO_1 *) LocalAlloc (LPTR, dwBytesNeeded)))
  642.   {
  643.     ErrMsgBox (GetStringRes(IDS_LALLOCFAIL), GetStringRes2(ERR_MOD_NAME));
  644.     bReturn = FALSE;
  645.     goto display_prt_drvs_info_done1;
  646.   }
  647.  
  648.   if (!EnumPrinterDrivers ((LPTSTR) NULL, (LPTSTR) NULL, 1,
  649.                            (LPBYTE) pDriverInfo1, dwBytesNeeded, &dwBytesNeeded,
  650.                            &dwDrvRet))
  651.   {
  652.     ErrMsgBox (GetStringRes(IDS_ENUMPRTDRVRET0), GetStringRes2(ERR_MOD_NAME));
  653.     bReturn = FALSE;
  654.     goto display_prt_drvs_info_done2;
  655.   }
  656.  
  657.   EnumPrinterDrivers ((LPTSTR) NULL,(LPTSTR) NULL, 2, NULL,
  658.                       0, &dwBytesNeeded, &dwDrvRet);
  659.  
  660.   pDriverInfo2 = (DRIVER_INFO_2 *) LocalAlloc (LPTR, dwBytesNeeded);
  661.  
  662.   EnumPrinterDrivers ((LPTSTR) NULL, (LPTSTR) NULL, 2,
  663.                       (LPBYTE) pDriverInfo2, dwBytesNeeded, &dwBytesNeeded,
  664.                       &dwDrvRet);
  665.  
  666.   if (!dwDrvRet)
  667.   {
  668.     ErrMsgBox (GetStringRes(IDS_ENUMPRTDRVRET0), "");
  669.     bReturn = FALSE;
  670.     goto display_prt_drvs_info_done3;
  671.   }
  672.  
  673.   //
  674.   // insert formatted info into listbox
  675.   //
  676.  
  677.   for (i = 0; i < dwDrvRet; i++)
  678.   {
  679.     sprintf (buf, gaDriverInfo[0]);
  680.     outstr3();
  681.  
  682.     sprintf (buf, gaDriverInfo[1], (pDriverInfo1 + i)->pName);
  683.     outstr3();
  684.  
  685.     sprintf (buf, gaDriverInfo[2]);
  686.     outstr3();
  687.  
  688.     sprintf (buf, gaDriverInfo[3], (pDriverInfo2 + i)->cVersion);
  689.     outstr3();
  690.  
  691.     sprintf (buf, gaDriverInfo[4], (pDriverInfo2 + i)->pName);
  692.     outstr3();
  693.  
  694.     sprintf (buf, gaDriverInfo[5], (pDriverInfo2 + i)->pEnvironment);
  695.     outstr3();
  696.  
  697.     sprintf (buf, gaDriverInfo[6], (pDriverInfo2 + i)->pDriverPath);
  698.     outstr3();
  699.  
  700.     sprintf (buf, gaDriverInfo[7], (pDriverInfo2 + i)->pDataFile);
  701.     outstr3();
  702.  
  703.     sprintf (buf, gaDriverInfo[8], (pDriverInfo2 + i)->pConfigFile);
  704.     outstr3();
  705.  
  706.   }
  707.  
  708. display_prt_drvs_info_done3:
  709.  
  710.   LocalFree (LocalHandle (pDriverInfo2));
  711.  
  712. display_prt_drvs_info_done2:
  713.  
  714.   LocalFree (LocalHandle (pDriverInfo1));
  715.  
  716. display_prt_drvs_info_done1:
  717.  
  718.   return bReturn;
  719. }
  720.  
  721.