home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_syst / setup.arj / BINCODE.C next >
Encoding:
C/C++ Source or Header  |  1991-06-25  |  10.7 KB  |  325 lines

  1. // BINCODE.C
  2.  
  3. /*****************************************************************************
  4.  
  5.    BINCODE.C - Generic Windows Setup program.
  6.  
  7.              This is the BINCODE.EXE program source.
  8.              It will read the .INF file, and follow its format
  9.              for copying files from floppy disk to the hard disk,
  10.              and adding icons to the program manager.
  11.  
  12.    HISTORY:  Created 08-08-90  Dave
  13.              Fixed zillions of bugs 08/90 - 10/90
  14.              Added Selva's Decompression 10/16/90
  15.  
  16.  
  17.  
  18.  
  19.    Functions:
  20.    ---------------------------------------------------------------------
  21.  
  22.    WinMain                  Main Program Entry Point
  23.  
  24.    CheckForEnoughDiskSpace  This routine is called only if *all* of
  25.                             the files in the .INF file are tagged
  26.                             as "R" (required) in the flags.  This
  27.                             routine checks to see if the destination
  28.                             disk has enough room to copy all of the
  29.                             files over.
  30.  
  31.    DisplayDialogBox         Given the template name and the function
  32.                             name, displays a dialog box.
  33.  
  34.    WndProc                  The main window message filter. Even though
  35.                             this window is invisible, the code resides
  36.                             just to make the code clean and non-hackish.
  37.  
  38.    BackupProgmanIniFile     Backs up the PROGMAN.INI file in case an
  39.                             obscure bug in progman trashes the ini file.
  40.  
  41. *****************************************************************************/
  42.  
  43. #include "windows.h"
  44.  
  45. #define INMAIN
  46. #include "bincode.h"
  47. #undef INMAIN
  48.  
  49. #include "gasgauge.h"                // Code for the % gas gausge
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include <dos.h>
  55.  
  56. /********************* STATIC, GLOBAL VARIABLES  ********************/
  57.  
  58. extern int __argc;               // # of Command line parameters
  59. extern char **__argv;            // Command line parameter table
  60.  
  61. /********************************************************************/
  62.  
  63. int PASCAL WinMain( HANDLE hInstance,   HANDLE hPrevInstance, 
  64.                     LPSTR  lpszCmdLine, int nCmdShow )
  65. {
  66.   BOOL        retval;         // Return value. If FALSE, user aborted
  67.   WNDCLASS    wndclass ;
  68.   MSG         msg;
  69.   BOOL        realmode;
  70.   HANDLE      hCode;
  71.   int         i;
  72.   BOOL        UserDecides;
  73.   char        szMsg[255];
  74.  
  75.   // Look for magic word
  76.  
  77.   if (
  78.       (__argv[1][0] != 'A') ||
  79.       (__argv[1][1] != 'E') ||
  80.       (__argv[1][2] != 'K') ||
  81.       (__argv[1][3] != 'D') ||
  82.       (__argv[1][4] != 'B')
  83.      )
  84.     {
  85.     MessageBox ( GetFocus(), "Please run SETUP.EXE.", "Bincode", 
  86.                  MB_OK | MB_ICONHAND );
  87.     return FALSE;
  88.     }
  89.  
  90.   lstrcpy ( (LPSTR)szCurrentInstallDir, (LPSTR)__argv[2] );
  91.  
  92.   if (hPrevInstance)
  93.     {
  94.     HWND hWnd;
  95.  
  96.     hWnd = FindWindow(szAppName, NULL);
  97.     hWnd = GetLastActivePopup(hWnd);
  98.     BringWindowToTop(hWnd);
  99.     if (IsIconic(hWnd))
  100.         ShowWindow(hWnd, SW_RESTORE);
  101.     return FALSE;
  102.     }
  103.  
  104.   SetErrorMode ( 1 );   // Blow off that stupid file I/O error box
  105.  
  106.   realmode = (!(GetWinFlags () & WF_PMODE ));  // this is for the Dan bug
  107.   if (realmode)
  108.     {
  109.     BackupProgmanIniFile ();     // This is in case Program Pukes
  110.                                  // due to an obscure bug
  111.     hCode = GetCodeHandle ( (FARPROC)WndProc );
  112.     GlobalFix ( hCode );
  113.     }
  114.  
  115.   ghInst = hInstance;      // Set the global variable
  116.  
  117.   if (!hPrevInstance) 
  118.     {
  119.     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;        // Redraw when resized
  120.     wndclass.lpfnWndProc   = WndProc ;                        // Messages go to WndProc
  121.     wndclass.cbClsExtra    = 0 ;                              // No extra info
  122.     wndclass.cbWndExtra    = 0 ;                              // No extra info
  123.     wndclass.hInstance     = hInstance ;                      // Set class for this instance
  124.     wndclass.hIcon         = LoadIcon (hInstance, szAppName) ;// Assign "Bincode" Icon from RC file
  125.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;   // Use arrow cursor
  126.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;   // White background
  127.     wndclass.lpszMenuName  = NULL ;                           // Use "Phone" menu from RC file
  128.     wndclass.lpszClassName = szAppName ;
  129.  
  130.     if (!RegisterClass (&wndclass))                           // Register the class
  131.       return FALSE;
  132.  
  133.     if (!RegisterGasGauge ( hInstance ))                      // Register the Gas Guage class
  134.       return FALSE;
  135.  
  136.     if (!ddeInit ( hInstance ))                               // Register and create DDE Window
  137.       return FALSE;
  138.     }
  139.  
  140.   ghWnd = CreateWindow (szAppName, "Setup",                   // Window Title, caption
  141.                       WS_OVERLAPPEDWINDOW,                    // Use normal window attributes
  142.                       0, 0, 0, 0,                             // Invisible Window Coordinates
  143.                       NULL,                                   // No parent
  144.                       NULL,                                   // Use class menu
  145.                       hInstance,                              // This instance
  146.                       NULL) ;                                 // No extra info
  147.  
  148.   if ((!ghWnd) || (!InitLZExpand(realmode)))
  149.     {
  150.     MessageBox ( NULL, "There is not enough memory to run Setup. "
  151.                        "Please quit one or more applications and "
  152.                        "run Setup again.",
  153.                        "Setup", MB_OK | MB_ICONHAND );
  154.     retval = FALSE;
  155.     }                                                         // Only continue if window was created
  156.   else
  157.     retval = TRUE;
  158.  
  159.   // Display the "Reading .INF file" box
  160.   if (retval) retval = DisplayDialogBox ( (LPSTR)"STARTUPINFO",     StartUpDlgProc    );
  161.  
  162.   // Display the dialog box to allow user to select destination directory
  163.   if (retval) retval = DisplayDialogBox ( (LPSTR)"INSTALLDIR",  InstallDirDlgProc );
  164.  
  165.   // Display the dialog box to allow user to select files (if necessary)
  166.   if (retval) 
  167.     {
  168.     UserDecides = FALSE;
  169.     for ( i = 0; i < NumFiles; i++ )
  170.       if ( !FileInfo[i].bSelected )     // If there is an unselected file, then
  171.                                         // we must let the user decide (bring up
  172.                                         // Select files dialog box
  173.         {
  174.         UserDecides = TRUE;
  175.         }
  176.  
  177.     if (UserDecides)  
  178. #ifdef COMPLEXSETUP
  179.       retval = DisplayDialogBox ( (LPSTR)"SELECTFILES", SelectDlgProc     );
  180. #endif
  181. #ifdef SIMPLESETUP
  182.       retval = CheckForEnoughDiskSpace ();
  183. #endif
  184.     else
  185.       retval = CheckForEnoughDiskSpace ();
  186.     }
  187.  
  188.   // Display the "Copy Files...." dialog box, and copy the files
  189.  
  190.   SetCursor ( LoadCursor ( NULL, IDC_WAIT ));
  191.  
  192.   if (retval) retval = DisplayDialogBox ( (LPSTR)"FILECOPY",    FileCopyDlgProc   );
  193.  
  194.  
  195.   GetPrivateProfileString ( "boot", "shell", "NONE", (LPSTR)szMsg, 128, "system.ini" );
  196.   if (!lstrcmp ( (LPSTR)szMsg, (LPSTR)"progman.exe"))
  197.     {
  198.     // DIsplat the "Adding icons..." dialog box, and add the icons
  199.     if (retval) retval = DisplayDialogBox ( (LPSTR)"ICONADD",     IconCopyDlgProc   );
  200.     }
  201.  
  202.   SetCursor ( LoadCursor ( NULL, IDC_ARROW ));
  203.  
  204.   // Indicate sucess!
  205.   if (retval) 
  206.     MessageBox ( ghWnd, "Installation Complete!", "Setup", MB_OK );
  207.   else
  208.     {
  209.     wsprintf ( (LPSTR)szMsg, (LPSTR)"Setup has not completely installed %s.  Please correct "
  210.                                     "any problems and run Setup again.", (LPSTR)szPackageName );
  211.     MessageBox ( GetFocus(), (LPSTR)szMsg, "Setup", MB_OK );
  212.     }
  213.  
  214.   // Close the invisible parent window
  215.   if (IsWindow(ghWnd))
  216.     PostMessage ( ghWnd, WM_CLOSE, 0, 0L );
  217.  
  218.  
  219.   // Message loop to keep things Petzoldian clean
  220.   while (GetMessage(&msg,NULL,NULL,NULL)) 
  221.     {
  222.     TranslateMessage(&msg);
  223.     DispatchMessage(&msg);
  224.     }
  225.  
  226.   // If the was copied to the temp dir from a floppy, then the
  227.   // two parameters passed are the .EXE and .INF file to delete
  228.  
  229.   if (__argc > 3)   // this means we have to commit suicide!
  230.     {
  231.     DosDelete (__argv[3]);
  232.     DosDelete (__argv[4]);
  233.     }
  234.  
  235.   // More on Dan bug
  236.   if (realmode)
  237.     {
  238.     GlobalUnfix ( hCode );
  239.     }
  240.  
  241.   DeInitLZExpand ( );
  242.  
  243.   return retval;
  244. }
  245. /********************************************************************/
  246. BOOL CheckForEnoughDiskSpace ( void)
  247. {
  248.   LONG TotalRequired;
  249.   LONG TotalNeeded;
  250.   int  i;
  251.   char szMsg[255];
  252.  
  253.   TotalRequired = 0L;
  254.  
  255.   // Scan through .INF data strcutures to find out space requirements
  256.   for ( i = 0; i < NumFiles; i++ )
  257.     TotalRequired += FileInfo[i].BytesRequired;
  258.  
  259.   TotalNeeded = DosDiskFreeSpace ( DirInfo[0].szDirectory[0]-'@' );
  260.  
  261.   if (TotalRequired > TotalNeeded)
  262.     {
  263.     sprintf ( szMsg, "Setup needs %ldK of disk space to install %s.  Please "
  264.                      "free enough disk space, or choose a different drive "
  265.                      "and/or directory.", TotalRequired/1024L, szPackageName );
  266.  
  267.     MessageBox ( ghWnd, (LPSTR)szMsg, (LPSTR)"Not Enough Disk Space", MB_OK | MB_ICONHAND );
  268.     return FALSE;
  269.     }
  270.  
  271.   return TRUE;
  272. }
  273. /********************************************************************/
  274. int DisplayDialogBox ( LPSTR TemplateName, FARPROC FunctionName )
  275. {
  276.   FARPROC  lpDialogProc;
  277.   int      RetVal;
  278.  
  279.   lpDialogProc = MakeProcInstance ( FunctionName, ghInst );
  280.   RetVal       = DialogBox ( ghInst, TemplateName, ghWnd, lpDialogProc );
  281.   FreeProcInstance ( lpDialogProc );
  282.   return RetVal;
  283. }
  284. /*******************************************************************/
  285. WINPROC WndProc ( WINDOWS_PARAMS )
  286.   switch (msg) 
  287.     {
  288.     case WM_DESTROY:
  289.  
  290.       PostQuitMessage(0);
  291.       break;
  292.  
  293.     default:
  294.  
  295.       return (DefWindowProc(hWnd, msg, wParam, lParam));
  296.     }
  297.  
  298.   return 0L;
  299. }
  300. /*******************************************************************/
  301. //
  302. // Due to an obscure bug in Progman 3.00, if in real mode, there is
  303. // a possibility that the PROGMAN.INI file can get trashed when adding
  304. // a new group.  We make a backup of it so of the crash happens, the
  305. // flame calling Product Support will get a kind voice saying
  306. // "Please copy your progman.bak to progman.ini".
  307. //
  308. //-------------------------------------------------------------------
  309. void BackupProgmanIniFile ( void )
  310. {
  311.   char szCurDir[255];
  312.   char szProgmanPath[255];
  313.   char szProgmanBackupPath[255];
  314.  
  315.   GetWindowsDirectory ( (LPSTR)szCurDir, 254 );
  316.   wsprintf ( (LPSTR)szProgmanPath, (LPSTR)"%s\\PROGMAN.INI", (LPSTR)szCurDir );
  317.   wsprintf ( (LPSTR)szProgmanBackupPath, (LPSTR)"%s\\PROGMAN.BAK", (LPSTR)szCurDir );
  318.   DosCopy ( (LPSTR)szProgmanPath, (LPSTR)szProgmanBackupPath );
  319. }
  320.   
  321.  
  322.  
  323.  
  324.