home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / pcmagazi / 1991 / 10 / psmain.c < prev    next >
C/C++ Source or Header  |  1991-02-26  |  27KB  |  729 lines

  1. /************************************************************************
  2.  *                                                                      *
  3.  *  PROGRAM: PROGSET.C                                                  *
  4.  *                                                                      *
  5.  *  PURPOSE: Allow user to safely modify WIN.INI file.                  *
  6.  *                                                                      *     
  7.  *  FUNCTIONS:                                                          *
  8.  *                                                                      *
  9.  *    WinMain()        - initialization, processes message loop         *
  10.  *    MainWndProc()    - processes messages                             *
  11.  *    CommandHandler() - handles WM_COMMAND messages                    *
  12.  *    DoDialog()       - runs a dialog box                              *
  13.  *    DefaultDlg()     - default dialog box procudure                   *
  14.  *    ExtDlg()         -                                                *
  15.  *    ListExts()       -                                                *
  16.  *    ListApps()       -                                                *
  17.  *    GetApps()        -                                                *
  18.  *                                                                      *
  19.  ************************************************************************/
  20.  
  21. #include "ps.h"
  22. #include <string.h>
  23.  
  24. HANDLE  hInst;                    /* Handle to program instance      */
  25. HWND    hwnd;                     /* Handle to main window           */
  26. HANDLE  hHourGlass;               /* Handle to hourglass cursor      */
  27. HANDLE  hSaveCursor;              /* Handle to saved cursor          */
  28.  
  29. int     nMode = EXTENSION;
  30. char    szListBuf[1024];
  31. char    szAppList[50][50];
  32. PSTR    psListItem[50];
  33. int     nAppQty;
  34. char    str[255];
  35. HWND    hListBox;
  36.  
  37. #define NUMCONST 9
  38. #define LENCONST 14
  39.  
  40. char szConstList [NUMCONST][LENCONST] =
  41.      { "WINDOWS", "DESKTOP", "EXTENSIONS", "INTL", "PORTS",
  42.        "FONTS", "PRINTERPORTS", "DEVICES", "COLORS" };
  43.  
  44. /* function prototypes for MAIN */
  45. int  FAR         DoDialog  (HANDLE, LPSTR, HWND, FARPROC, DWORD);
  46. int  FAR  PASCAL DefaultDlg (HWND, unsigned, WORD, LONG);
  47. int  FAR  PASCAL MainDlgProc (HWND, unsigned, WORD, LONG);
  48. int  FAR  PASCAL ExtDlg (HWND, WORD, WORD, LONG);
  49. int  NEAR PASCAL CommandHandler(HWND,WORD,LONG);
  50. int  NEAR        ListExts();
  51. int  NEAR        ListApps();
  52. BOOL NEAR        GetApps(HWND);
  53.  
  54. /***********************************************************************
  55.  *                                                                     *
  56.  *   FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)                     *
  57.  *                                                                     *
  58.  *   PURPOSE: calls initialization functions, processes message loop   *
  59.  *                                                                     *
  60.  ***********************************************************************/
  61. int PASCAL WinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  62. HANDLE hInstance;
  63. HANDLE hPrevInstance;
  64. LPSTR  lpCmdLine;
  65. int    nCmdShow;
  66. {
  67.     hInst = hInstance;
  68.  
  69.     /* get hourglass cursor */
  70.     hHourGlass = LoadCursor(NULL, IDC_WAIT);
  71.  
  72.     if (hPrevInstance)
  73.        {
  74.        MessageBox (NULL, "ProgSet is already Running", "ProgSet",
  75.                    MB_ICONEXCLAMATION | MB_OK);
  76.        return (NULL);
  77.        }
  78.     else
  79.        {
  80.        DoDialog (hInst, IDD_MAIN, NULL, MainDlgProc, NULL);
  81.        }
  82.  
  83.     return (NULL);
  84. }
  85.  
  86.  
  87. /***********************************************************************
  88.  *                                                                     *
  89.  *  FUNCTION: MainDlgProc(HWND, unsigned, WORD, LONG)                  *
  90.  *                                                                     *
  91.  *  PURPOSE:  Processes messages                                       *
  92.  *                                                                     *
  93.  *  MESSAGES:                                                          *
  94.  *                                                                     *
  95.  *                                                                     *
  96.  *  COMMENTS:                                                          *
  97.  *                                                                     *
  98.  ***********************************************************************/
  99. int FAR PASCAL MainDlgProc (hWnd, message, wParam, lParam)
  100. HWND hWnd;
  101. unsigned message;
  102. WORD wParam;
  103. LONG lParam;
  104. {
  105.     HMENU hMenu;
  106.  
  107.     switch (message) 
  108.         {
  109.         case WM_INITDIALOG:
  110.              /* set global window handle */
  111.              hwnd = hWnd;
  112.              /* assign class icon */
  113.              SetClassWord (hwnd, GCW_HICON, LoadIcon(hInst, "PROGSET"));
  114.              /* modify system menu */
  115.              hMenu = GetSystemMenu (hwnd, FALSE);
  116.              AppendMenu (hMenu, MF_SEPARATOR, NULL, NULL);
  117.              AppendMenu (hMenu, MF_ENABLED, IDM_ABOUT, "About ProgSet...");
  118.              /* set file associations button */
  119.              SendDlgItemMessage (hwnd, IDC_ASSOC, BM_SETCHECK, TRUE, NULL);
  120.              /* simulate click of file associations button */
  121.              SendMessage (hwnd, WM_COMMAND, IDC_ASSOC, NULL);
  122.              /* set focus to list box */
  123.              hListBox = GetDlgItem (hwnd, IDC_LIST_BOX);
  124.              SetFocus (hListBox);
  125.              return (FALSE); /* Focus set */
  126.              break;
  127.  
  128.         case WM_COMMAND:
  129.              return (CommandHandler(hwnd, wParam, lParam));
  130.              break;
  131.  
  132.         case WM_CLOSE:
  133.              EndDialog(hwnd, NULL);
  134.              return(TRUE);
  135.              break;
  136.  
  137.         case WM_SYSCOMMAND:
  138.              if (wParam == IDM_ABOUT)
  139.                 {
  140.                 DoDialog (hInst, IDD_ABOUT, hWnd, NULL, NULL);
  141.                 break;
  142.                 }
  143.              /* fall through */
  144.  
  145.         default:
  146.              return (FALSE);
  147.         }
  148. }
  149.  
  150.  
  151. /***********************************************************************
  152.  *                                                                     *
  153.  *  FUNCTION   : CommandHandler()                                      *
  154.  *                                                                     *
  155.  *  PURPOSE    : Processes all WM_COMMAND messages.                    *
  156.  *                                                                     *
  157.  ***********************************************************************/
  158. int NEAR PASCAL CommandHandler(hWnd, wParam, lParam)
  159. HWND hWnd;
  160. WORD wParam;
  161. LONG lParam;
  162. {
  163.     BOOL bItems;
  164.  
  165.     switch (wParam)
  166.         {
  167.         /* control ID's */
  168.  
  169.         case IDC_ASSOC:
  170.              nMode = EXTENSION;
  171.              lstrcpy (str, "File Associations");
  172.              SetDlgItemText (hwnd, IDC_LIST_TITLE, str);
  173.              bItems = ListExts();
  174.              EnableWindow (GetDlgItem (hwnd, IDC_ADD), TRUE);
  175.              EnableWindow (GetDlgItem (hwnd, IDC_MODIFY), bItems);
  176.              EnableWindow (GetDlgItem (hwnd, IDC_REMOVE), bItems);
  177.              SetFocus (hListBox);
  178.              break;
  179.  
  180.  
  181.         case IDC_CUSTOM:
  182.              nMode = CUSTOM;
  183.              lstrcpy (str, "Installed programs");
  184.              SetDlgItemText (hwnd, IDC_LIST_TITLE, str);
  185.              bItems = ListApps();
  186.              EnableWindow (GetDlgItem (hwnd, IDC_ADD), FALSE);
  187.              EnableWindow (GetDlgItem (hwnd, IDC_MODIFY), FALSE);
  188.              EnableWindow (GetDlgItem (hwnd, IDC_REMOVE), bItems);
  189.              SetFocus (hListBox);
  190.              break;
  191.  
  192.         case IDC_ADD:
  193.              {
  194.              if (nMode == EXTENSION)
  195.                 {
  196.                 DoDialog(hInst, IDD_ADDEXT, hwnd, ExtDlg, ADD);
  197.                 ListExts();
  198.                 }
  199.              SetFocus (hListBox);
  200.              SendDlgItemMessage (hwnd, IDC_ADD, BM_SETSTYLE,
  201.                                 (WORD)BS_PUSHBUTTON, MAKELONG(NULL, TRUE));
  202.              }
  203.              break;
  204.  
  205.         case IDC_MODIFY:
  206.              {
  207.              if (nMode == EXTENSION)
  208.                 DoDialog(hInst, IDD_MODEXT, hwnd, ExtDlg, EDIT);
  209.              SetFocus (hListBox);
  210.              SendDlgItemMessage (hwnd, IDC_MODIFY, BM_SETSTYLE,
  211.                                 (WORD)BS_PUSHBUTTON, MAKELONG(NULL, TRUE));
  212.              }
  213.              break;
  214.  
  215.         case IDC_REMOVE:
  216.              {
  217.              if (nMode == EXTENSION)
  218.                 {
  219.                 char szExt[4];
  220.                 int nCurSel;
  221.                 int nResponse;
  222.  
  223.                 nCurSel = SendDlgItemMessage (hwnd, IDC_LIST_BOX,
  224.                                               LB_GETCURSEL, NULL,NULL);
  225.                 SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_GETTEXT,
  226.                                    nCurSel, (long)(LPSTR)szExt);
  227.  
  228.                 wsprintf(str, "Remove *.%s association from WIN.INI",
  229.                          (LPSTR)szExt);
  230.                 nResponse = MessageBox(hWnd, str, "ProgSet",
  231.                                        MB_YESNO | MB_ICONQUESTION);
  232.                 if (nResponse == IDYES)
  233.                    {
  234.                    WriteProfileString ("extensions",szExt,NULL);
  235.                    ListExts();
  236.                    }
  237.                 }
  238.              else /* nMode == CUSTOM */
  239.                 {
  240.                 char szApp[50];
  241.                 int nCurSel;
  242.                 int nResponse;
  243.  
  244.                 nCurSel = SendDlgItemMessage (hwnd, IDC_LIST_BOX,
  245.                                               LB_GETCURSEL, NULL,NULL);
  246.                 SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_GETTEXT,
  247.                                    nCurSel, (long)(LPSTR)szApp);
  248.  
  249.                 wsprintf(str, "Remove [%s] section from WIN.INI",
  250.                          (LPSTR)szApp);
  251.                 nResponse = MessageBox(hWnd, str, "ProgSet",
  252.                                        MB_YESNO | MB_ICONQUESTION);
  253.                 if (nResponse == IDYES)
  254.                    {
  255.                    WriteProfileString (szApp,NULL,NULL);
  256.                    ListApps();
  257.                    }
  258.                 }
  259.              SetFocus (hListBox);
  260.              SendDlgItemMessage (hwnd, IDC_REMOVE, BM_SETSTYLE,
  261.                                 (WORD)BS_PUSHBUTTON, MAKELONG(NULL, TRUE));
  262.              }
  263.              break;
  264.  
  265.         case IDC_EXIT:
  266.              SendMessage (hwnd, WM_CLOSE, NULL, NULL);
  267.              break;
  268.  
  269.         default:
  270.              return (FALSE);
  271.         }
  272.  
  273.     return (TRUE);
  274. }
  275.  
  276.  
  277. /***********************************************************************
  278.  *                                                                     *
  279.  *  FUNCTION   : DoDialog(hInst, lpResName, hWnd, lpProc, dwInitParam) *
  280.  *                                                                     *
  281.  *  PURPOSE    : Runs a dialog box.  A dialog procedure and initial    *
  282.  *               parameter are both optional.  If the dialog procedure *
  283.  *               is NULL then DefaultDlg is used.  If the initial      *
  284.  *               parameter is NULL then DialogBox() is called instead  *
  285.  *               of DialogBoxParam().                                  *
  286.  *                                                                     *
  287.  ***********************************************************************/
  288. int FAR DoDialog (hInst, lpResName, hWnd, lpProc, dwInitParam)
  289. HANDLE   hInst;
  290. LPSTR    lpResName;
  291. HWND     hWnd;
  292. FARPROC  lpProc;
  293. DWORD    dwInitParam;
  294. {
  295.     FARPROC lpfn;
  296.     int     ret;
  297.  
  298.     if (lpProc == NULL)  /* if no proc was specified, use default */
  299.        lpProc = DefaultDlg;
  300.  
  301.     lpfn = MakeProcInstance(lpProc, hInst);
  302.  
  303.     if (!dwInitParam)  /*  if dwInitParam = 0, do not pass param */
  304.        ret = DialogBox(hInst, lpResName, hWnd, lpfn);
  305.     else
  306.        ret = DialogBoxParam(hInst, lpResName, hWnd, lpfn, dwInitParam);
  307.  
  308.     FreeProcInstance(lpfn);
  309.  
  310.     return(ret);
  311. }
  312.  
  313.  
  314. /***********************************************************************
  315.  *                                                                     *
  316.  *   FUNCTION: DefaultDlg(HWND, unsigned, WORD, LONG)                  *
  317.  *                                                                     *
  318.  *   PURPOSE:  Processes only "OK" and "Cancel" messages for any       *
  319.  *             dialog box.                                             *
  320.  *                                                                     *
  321.  *   MESSAGES:                                                         *
  322.  *                                                                     *
  323.  *       WM_INITDIALOG - initialize dialog box                         *
  324.  *       WM_COMMAND    - Input received                                *
  325.  *                                                                     *
  326.  ***********************************************************************/
  327. int FAR PASCAL DefaultDlg(hDlg, message, wParam, lParam)
  328. HWND hDlg;
  329. unsigned message;
  330. WORD wParam;
  331. LONG lParam;
  332. {
  333.     switch (message)
  334.          {
  335.          case WM_INITDIALOG:
  336.               return (TRUE);
  337.  
  338.          case WM_CLOSE:
  339.               SendMessage (hDlg, WM_COMMAND, IDCANCEL, NULL);
  340.               return (TRUE);
  341.  
  342.          case WM_COMMAND:
  343.               if (wParam == IDOK)
  344.                  {
  345.                  EndDialog(hDlg, IDOK);
  346.                  return(TRUE);
  347.                  }
  348.               else if (wParam == IDCANCEL)
  349.                  {
  350.                  EndDialog(hDlg, IDCANCEL);
  351.                  return (TRUE);
  352.                  }
  353.               break;
  354.          }
  355.     return (FALSE);
  356. }
  357.  
  358.  
  359. /***********************************************************************
  360.  *                                                                     *
  361.  *   FUNCTION: ExtDlg (HWND, WORD, WORD, LONG)                         *
  362.  *                                                                     *
  363.  *   PURPOSE:  Processes only "OK" and "Cancel" messages for any       *
  364.  *             dialog box.                                             *
  365.  *                                                                     *
  366.  ***********************************************************************/
  367. int FAR PASCAL ExtDlg (hDlg, message, wParam, lParam)
  368. HWND hDlg;
  369. WORD message;
  370. WORD wParam;
  371. LONG lParam;
  372. {
  373.     switch (message)
  374.     {
  375.     case WM_INITDIALOG:
  376.          {
  377.          /* initializes dialog for ADD or EDIT */
  378.          switch (lParam)
  379.               {
  380.               case ADD:
  381.                    SendDlgItemMessage (hDlg, IDC_EXTENSION,
  382.                                        EM_LIMITTEXT, 3, NULL);
  383.                    SendDlgItemMessage (hDlg, IDC_COMMAND,
  384.                                        EM_LIMITTEXT, 127, NULL);
  385.                    break;
  386.               case EDIT:
  387.                    {
  388.                    int nCurSel;
  389.                    char szExt[4];
  390.                    char szCmd[128];
  391.    
  392.                    nCurSel = SendDlgItemMessage (hwnd, IDC_LIST_BOX,
  393.                                                  LB_GETCURSEL, NULL,NULL);
  394.                    SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_GETTEXT,
  395.                                        nCurSel, (long)(LPSTR)szExt);
  396.                    SetDlgItemText (hDlg, IDC_EXTENSION, szExt);
  397.                    GetProfileString ("extensions", szExt, "", szCmd,128);
  398.                    SendDlgItemMessage (hDlg, IDC_COMMAND,
  399.                                        EM_LIMITTEXT, 127, NULL);
  400.                    SetDlgItemText (hDlg, IDC_COMMAND, szCmd);
  401.                    SendDlgItemMessage (hDlg, IDC_COMMAND, EM_SETSEL,
  402.                                        NULL, MAKELONG(0,127));
  403.                    }
  404.                    break;
  405.               }
  406.          return (TRUE);
  407.          }
  408.          break;
  409.     
  410.     case WM_CLOSE:
  411.          SendMessage (hDlg, WM_COMMAND, IDCANCEL, NULL);
  412.          return (TRUE);
  413.  
  414.     case WM_COMMAND:
  415.          switch (wParam)
  416.          {
  417.          case IDC_EXTENSION:
  418.               EnableWindow(GetDlgItem(hDlg,IDOK), 
  419.                   (GetWindowTextLength(GetDlgItem(hDlg,IDC_EXTENSION)) &&
  420.                    GetWindowTextLength(GetDlgItem(hDlg,IDC_COMMAND))));
  421.               break;
  422.          case IDC_COMMAND:
  423.               EnableWindow(GetDlgItem(hDlg,IDOK), 
  424.                   (GetWindowTextLength(GetDlgItem(hDlg,IDC_EXTENSION)) &&
  425.                    GetWindowTextLength(GetDlgItem(hDlg,IDC_COMMAND))));
  426.               break;
  427.          case IDOK:
  428.               {
  429.               char szExt[4];
  430.               char szCmd[128];
  431.  
  432.               GetDlgItemText (hDlg, IDC_EXTENSION, szExt, 4);
  433.               GetDlgItemText (hDlg, IDC_COMMAND, szCmd, 128);
  434.               WriteProfileString ("extensions", szExt, szCmd);
  435.               EndDialog(hDlg, IDOK);
  436.               return(TRUE);
  437.               break;
  438.               }
  439.          case IDCANCEL:
  440.               {
  441.               EndDialog(hDlg, IDCANCEL);
  442.               return (TRUE);
  443.               break;
  444.               }
  445.          case IDC_BROWSE:
  446.               {
  447.               DoDialog (hInst, IDD_BROWSE, hDlg, BrowseDlg, NULL);
  448.  
  449.               if (*szProgName)
  450.                  {
  451.                  char szExt[4];
  452.  
  453.                  GetDlgItemText (hDlg, IDC_EXTENSION, szExt, 4);
  454.                  lstrcat (szProgName, " ^.");
  455.                  lstrcat (szProgName, szExt);
  456.                  SetDlgItemText (hDlg, IDC_COMMAND , szProgName);
  457.                  }
  458.  
  459.               SendDlgItemMessage (hDlg, IDC_COMMAND, EM_SETSEL, 
  460.                                   NULL, MAKELONG(0,128));
  461.               SendDlgItemMessage (hDlg, IDOK,
  462.                                   BM_SETSTYLE, (WORD) BS_DEFPUSHBUTTON,
  463.                                   MAKELONG(NULL, TRUE));
  464.               SendDlgItemMessage (hDlg, IDC_BROWSE,
  465.                                   BM_SETSTYLE, (WORD) BS_PUSHBUTTON,
  466.                                   MAKELONG(NULL, TRUE));
  467.               SetFocus (GetDlgItem (hDlg, IDC_COMMAND));
  468.  
  469.               return (TRUE);
  470.               break;
  471.               }
  472.          }
  473.     }
  474.     return (FALSE);
  475. }
  476.  
  477.  
  478. /***********************************************************************
  479.  *                                                                     *
  480.  *   FUNCTION: ListExts()                                              *
  481.  *                                                                     *
  482.  *   PURPOSE:  Lists file extensions with associations.                *
  483.  *                                                                     *
  484.  ***********************************************************************/
  485. int NEAR ListExts()
  486. {
  487.     PSTR  psTemp=szListBuf;
  488.     int   nBufSize=0;
  489.     int   nIndex=0;
  490.     int   nItemCount=0;
  491.  
  492.     /* empty the list box */
  493.     SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_RESETCONTENT, NULL, NULL); 
  494.  
  495.     /* get list of [extension] entries as a series of null terminated  */
  496.     /* strings packed into szListBuf.  nBufSize is set to the total    */
  497.     /* number of characters packed into szListBuf.                     */
  498.     nBufSize = GetProfileString ("extensions", NULL, NULL, szListBuf, 1024);
  499.  
  500.     /* if call failed, or no [extension] section found, return 0 */
  501.     if (!nBufSize)
  502.        return (0);
  503.  
  504.     /* set the psListItem array to point to the file associations */
  505.     do
  506.     {
  507.     psListItem[nIndex] = psTemp;
  508.     psTemp += lstrlen(psListItem[nIndex]) + 1;
  509.     nIndex++;
  510.     }
  511.     while (psTemp < (szListBuf + nBufSize - 1));
  512.  
  513.     nItemCount = nIndex;
  514.     nIndex = 0;
  515.  
  516.     /* turn off listbox drawing while adding items - stops flicker */
  517.     SendDlgItemMessage (hwnd, IDC_LIST_BOX, WM_SETREDRAW, FALSE, NULL);
  518.  
  519.     /* add the file associations */
  520.     while (nIndex < nItemCount)
  521.        {
  522.        AnsiUpper (psListItem[nIndex]);
  523.        SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_ADDSTRING, NULL, 
  524.                            (LONG)(LPSTR)psListItem[nIndex]);
  525.        nIndex++;
  526.  
  527.        /* if it is the last item, turn drawing back on for display */
  528.        if (nIndex == (nItemCount - 1))
  529.           {
  530.           SendDlgItemMessage (hwnd, IDC_LIST_BOX,
  531.                               WM_SETREDRAW, TRUE, NULL);
  532.           }
  533.        }
  534.  
  535.     /* select the first item in the listbox initially */
  536.     SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_SETCURSEL, 0, 0L);
  537.  
  538.     /* return the number of file associations processed */
  539.     return (nItemCount);
  540. }
  541.  
  542.  
  543. /***********************************************************************
  544.  *                                                                     *
  545.  *   FUNCTION: ListApps()                                              *
  546.  *                                                                     *
  547.  *   PURPOSE:  Lists applications with profile data in WIN.INI.        *
  548.  *                                                                     *
  549.  ***********************************************************************/
  550. int NEAR ListApps()
  551. {
  552.     int nIndex;
  553.  
  554.     /* empty the list box */
  555.     SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_RESETCONTENT, NULL, NULL); 
  556.  
  557.     /* get the list of application headings */
  558.     GetApps(hwnd);
  559.  
  560.     /* turn off listbox drawing while adding items - stops flicker */
  561.     SendDlgItemMessage (hwnd, IDC_LIST_BOX, WM_SETREDRAW, FALSE, NULL);
  562.  
  563.     /* add the application headings found in GetApps() */
  564.     for (nIndex = 0; nIndex < nAppQty; nIndex++)
  565.        {
  566.        SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_ADDSTRING, NULL, 
  567.                            (LONG)(LPSTR)szAppList[nIndex]);
  568.  
  569.        /* if it is the last item, turn drawing back on for display */
  570.        if (nIndex == (nAppQty - 1))
  571.           {
  572.           SendDlgItemMessage (hwnd, IDC_LIST_BOX,
  573.                               WM_SETREDRAW, TRUE, NULL);
  574.           }
  575.        }
  576.  
  577.     /* select the first item in the listbox initially */
  578.     SendDlgItemMessage (hwnd, IDC_LIST_BOX, LB_SETCURSEL, 0, 0L);
  579.  
  580.     /* return the number of application headings processed */
  581.     return (nAppQty);
  582. }
  583.  
  584.  
  585. /***********************************************************************
  586.  *                                                                     *
  587.  *  FUNCTION   : GetApps (HWND)                                        *
  588.  *                                                                     *
  589.  *  PURPOSE    : Gets the list of applications from the WIN.INI file.  *
  590.  *                                                                     *
  591.  *  RETURNS    : TRUE if the file was successfully opened, else FALSE. *
  592.  *                                                                     *
  593.  **********************************************************************/
  594. BOOL NEAR GetApps (hWnd)
  595. HWND hWnd;
  596. {
  597.     char   szWinIni[255];    /* WIN.INI path & filename */
  598.     int    hFile;            /* DOS file handle         */
  599.     LONG   lLength;          /* length of file          */
  600.     WORD   wStatus;          /* result of file I/O      */
  601.     HANDLE hBuffer;          /* HANDLE to file buffer   */
  602.     PSTR   pBuffer;          /* pointer to file buffer  */
  603.     PSTR   pBuffPtr;         /* location in pBuffer     */
  604.     PSTR   pStartKey =       /* start of app name '['   */
  605.               (PSTR)TRUE;
  606.     PSTR   pEndKey;          /* end of app name ']'     */
  607.     int    nAppNum = 0;      /* holder for index        */
  608.     BOOL   bConst;           /* TRUE if app is constant */
  609.     int    nTemp;            /* general purpose temp    */
  610.  
  611.     /* get the path for WIN.INI */
  612.     GetWindowsDirectory ((LPSTR)szWinIni, 255);
  613.  
  614.     /* add a '\' if not already there */
  615.     if (szWinIni[lstrlen((LPSTR)szWinIni) - 1] != '\\')
  616.        lstrcat((LPSTR)szWinIni,(LPSTR)"\\");
  617.  
  618.     /* add file name 'WIN.INI' to path */
  619.     lstrcat((LPSTR)szWinIni,(LPSTR)"WIN.INI");
  620.  
  621.     /* open the file */
  622.     hFile = _lopen (szWinIni, OF_READ);
  623.  
  624.     /* error if file could not be opened */
  625.     if (hFile < 0)
  626.        {
  627.         wsprintf(str, "Cannot read file: %s.", (LPSTR)szWinIni);
  628.         MessageBox(hWnd, str, "ProgSet", MB_OK | MB_ICONEXCLAMATION);
  629.         return (FALSE);  /* error opening file */
  630.        }
  631.  
  632.     /* get file length */
  633.     lLength = _llseek (hFile,0L,2); 
  634.     _llseek (hFile, 0L, 0);
  635.  
  636.     /* error if file is too large */
  637.     if (lLength > MAXFILESIZE)
  638.        {
  639.         MessageBox(hWnd, "The WIN.INI file is too large", 
  640.                    "ProgSet", MB_OK | MB_ICONHAND);
  641.         return (FALSE);
  642.        }
  643.  
  644.     /* allocate buffer to the length of the file + 1 */
  645.     hBuffer = LocalAlloc(LHND, (WORD)lLength + 1);
  646.  
  647.     /* error if buffer could not be allocated */
  648.     if (!hBuffer) 
  649.        {
  650.         MessageBox(hWnd, "Not enough memory to load WIN.INI", 
  651.                    "ProgSet", MB_OK | MB_ICONHAND);
  652.         return (FALSE);
  653.        }
  654.  
  655.     /* set the hourglass cursor */
  656.     hSaveCursor = SetCursor(hHourGlass);
  657.  
  658.     /* lock the buffer and receive a pointer to it */
  659.     pBuffer = LocalLock(hBuffer);
  660.  
  661.     /* read the file into the buffer */
  662.     wStatus = _lread(hFile, (LPSTR)pBuffer, (WORD)lLength);
  663.     _lclose(hFile);
  664.  
  665.     /* error if # bytes read not equal to file size */
  666.     if (wStatus != lLength)
  667.        {
  668.         wsprintf(str, "Error reading %s.", (LPSTR)szWinIni);
  669.         MessageBox(hWnd, str, "ProgSet", MB_OK | MB_ICONEXCLAMATION);
  670.  
  671.         /* restore the cursor and return */
  672.         SetCursor(hSaveCursor);
  673.         return (FALSE);
  674.        }
  675.  
  676.     /* zero terminate the buffer */
  677.     pBuffer[lLength] = 0;
  678.  
  679.     /* fill application name array */
  680.     pBuffPtr = pBuffer;
  681.     nAppNum = 0;
  682.     while (pStartKey)
  683.        {
  684.        /* This loop locates application headings */
  685.        pStartKey = strchr(pBuffPtr, '[');       
  686.        if (pStartKey)
  687.           {
  688.           /* start of application heading found, look for end */
  689.           pStartKey++;
  690.           for (pEndKey=pStartKey;*pEndKey!=']' && *pEndKey!='\n';pEndKey++);
  691.  
  692.           /* zero terminate the app heading for later string operations */
  693.           *pEndKey = 0;
  694.  
  695.           /* see if this application heading should be protected */
  696.           for (bConst=FALSE,nTemp = 0; nTemp < NUMCONST; nTemp++)
  697.               {
  698.               /* upshift application heading and compare to constant */
  699.               AnsiUpper (pStartKey);
  700.               if (lstrcmp (pStartKey, szConstList[nTemp]) == 0)
  701.                  bConst = TRUE;
  702.               }
  703.  
  704.           /* is the app heading protected AND does it start in col 1? */
  705.           if ((!bConst) && (pStartKey-1==pBuffer || *(pStartKey-2)=='\n'))    
  706.               {
  707.               /* add the application heading to the list.     */
  708.               lstrcpy(szAppList[nAppNum], pStartKey);         
  709.               nAppNum++;
  710.               }
  711.  
  712.           /* advance past the end of the previous application heading */
  713.           pBuffPtr = pEndKey + 1;
  714.           }
  715.        }
  716.  
  717.     /* set the number of applications added to the list */
  718.     nAppQty = nAppNum;
  719.  
  720.     /* unlock and release buffer */
  721.     LocalUnlock (hBuffer);
  722.     LocalFree (hBuffer);
  723.  
  724.     /* restore the cursor */
  725.     SetCursor(hSaveCursor);
  726.  
  727.     return (TRUE);
  728. }
  729.