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

  1. #include "windows.h"
  2.  
  3. #define INSTALLAPPNAME "BINXZ.EXE"
  4. #define INSTALLINFFILE "SETUP.INF"
  5.  
  6. // These must be bitwise opposite words (add to 65535)
  7.  
  8. #define CHUNKSIZE        16383  // 0011 1111 1111 1111
  9. #define NOTCHUNKSIZE     49152  // 1100 0000 0000 0000
  10.  
  11. // These must add to 16, and reflect the bits above
  12.  
  13. #define POWEROFCHUNK        14  //   ^^ ^^^^ ^^^^ ^^^^
  14. #define NOTPOWEROFCHUNK      2  // ^^
  15.  
  16. /*********************************************************************
  17.  
  18.    SETUP.C - Generic Windows Setup program.
  19.  
  20.              This is the SETUP.EXE program source.  It checks
  21.              to see if the setup is being run from a floppy drive.
  22.              If it is, then SETUP.EXE will copy the installation
  23.              program (BINCODE.EXE in this example) over to the
  24.              Windows TEMP directory and then run it from the
  25.              hard disk.  This allows the floppy disks to
  26.              be swapped without fear of losing code segments,
  27.              and allows the setup programs to have moveable,
  28.              discardable code segments.
  29.  
  30.    HISTORY:  Created                08-08-90              Dave
  31.              Added more robust copy 08-14-90              Dave
  32.              Fixed zillions of bugs 08-14-90 -> 09-10-90  Dave
  33.       
  34.  
  35. *********************************************************************/
  36.  
  37.  
  38.  
  39. /************************* FUNCTION PROTOTYPES **********************/
  40.  
  41. int  PASCAL WinMain ( HANDLE, HANDLE, LPSTR, int                     );
  42. BOOL DosCopy        ( LPSTR, LPSTR                                   );
  43. void DosDelete      ( char *                                         );
  44.  
  45. /*************************  GLOBAL VARIABLE   ***********************/
  46.  
  47. LPSTR  Buffer;     // Dos Copy buffer
  48. HANDLE hBuffer;
  49.  
  50. /********************************************************************/
  51.  
  52. int PASCAL WinMain( HANDLE hInstance,   HANDLE hPrevInstance, 
  53.                     LPSTR  lpszCmdLine, int cmdShow )
  54. {
  55.  
  56.   char    szModuleFileName[256];   // Name and path of this .EXE
  57.   char    szCurDir[256];           // The path of this .EXE
  58.   char    szWindowsDir[256];       // Where Windows Lives
  59.   char    szSrcFile[256];          // Name of this .EXE (without path)
  60.   char    szDstFile[256];          // Destination path of main install program
  61.   int     i;                       // Dinky counter
  62.   BOOL    bSuccess;                // Return Error value from DosCopy
  63.   HWND    hWnd;
  64.  
  65.  
  66.   // If there is another version of setup running, run it
  67.  
  68.   if (hWnd = FindWindow("BINCODE", NULL))
  69.     {
  70.     hWnd = GetLastActivePopup(hWnd);
  71.     BringWindowToTop(hWnd);
  72.     if (IsIconic(hWnd))
  73.       ShowWindow(hWnd, SW_RESTORE);
  74.     return FALSE;
  75.     }
  76.  
  77.   hBuffer = GlobalAlloc ( GMEM_MOVEABLE, CHUNKSIZE+1 );
  78.   Buffer = GlobalLock ( hBuffer );
  79.  
  80.   // Find out where this app is getting run from...
  81.  
  82.   GetModuleFileName(hInstance,szModuleFileName,255);
  83.  
  84.   // Check for running off a Floppy Disk
  85.  
  86.   if ((*szModuleFileName == 'A') || (*szModuleFileName == 'B'))
  87.     {
  88.     // If this is running from a floppy, copy it to your windows
  89.     // directory, and rerun it
  90.  
  91.     GetWindowsDirectory ( (LPSTR)szWindowsDir, 250 );
  92.  
  93.     // Extract the current directory
  94.  
  95.     wsprintf ( (LPSTR)szCurDir, (LPSTR)szModuleFileName );
  96.     i = lstrlen ( (LPSTR)szCurDir );
  97.     i--;
  98.     while ( i && szCurDir[i] != '\\' ) i--;
  99.     szCurDir[i+1] = 0;
  100.  
  101.     wsprintf ((LPSTR)szSrcFile, (LPSTR)"%s%s", (LPSTR)szCurDir, (LPSTR)INSTALLINFFILE );
  102.     wsprintf ((LPSTR)szDstFile, (LPSTR)"%s\\%s", (LPSTR)szWindowsDir, (LPSTR)INSTALLINFFILE );
  103.  
  104.     SetCursor ( LoadCursor ( NULL, IDC_WAIT ));
  105.  
  106.     bSuccess = DosCopy ((LPSTR)szSrcFile, (LPSTR)szDstFile);
  107.     if (!bSuccess) DosDelete ( szDstFile );
  108.  
  109.     wsprintf ((LPSTR)szSrcFile, (LPSTR)"%sBIN\\%s", (LPSTR)szCurDir, (LPSTR)INSTALLAPPNAME );
  110.     wsprintf ((LPSTR)szDstFile, (LPSTR)"%s\\%s", (LPSTR)szWindowsDir, (LPSTR)INSTALLAPPNAME );
  111.  
  112.     if (bSuccess) bSuccess = DosCopy ((LPSTR)szSrcFile, (LPSTR)szDstFile);
  113.     if (!bSuccess) 
  114.         {
  115.         DosDelete ( szDstFile );  // Get rid of BINCODE.EXE
  116.         wsprintf ((LPSTR)szDstFile, (LPSTR)"%s\\%s", (LPSTR)szWindowsDir, (LPSTR)INSTALLINFFILE );
  117.         DosDelete ( szDstFile );  // Get rid of SETUP.INF
  118.         }
  119.  
  120.     // By spawining and passing extra params, we know to delete ourself
  121.     // from the temp directory afterwards
  122.  
  123.     SetCursor ( LoadCursor ( NULL, IDC_ARROW ));
  124.  
  125.     wsprintf ((LPSTR)szDstFile, (LPSTR)"%s\\%s AEKDB %s %s\\%s %s\\%s", 
  126.              (LPSTR)szWindowsDir, (LPSTR)INSTALLAPPNAME,
  127.              (LPSTR)szCurDir, 
  128.              (LPSTR)szWindowsDir, (LPSTR)INSTALLINFFILE, 
  129.              (LPSTR)szWindowsDir, (LPSTR)INSTALLAPPNAME );
  130.  
  131.     if (bSuccess)
  132.       {
  133.       WinExec (szDstFile, cmdShow );
  134.       }
  135.     else
  136.       {
  137.       wsprintf ((LPSTR)szDstFile, (LPSTR)"There was an error copying files to your %s subdirectory. "
  138.                           "Please correct the problem and run Setup again.", (LPSTR)szWindowsDir );
  139.  
  140.       MessageBox ( GetFocus(), szDstFile, "Setup", MB_OK | MB_ICONHAND );
  141.       }
  142.     }
  143.   else  // if running off a hard disk or network, just spawn it...
  144.     {
  145.     wsprintf ((LPSTR)szDstFile, (LPSTR)"BIN\\%s AEKDB %s", 
  146.               (LPSTR)INSTALLAPPNAME,
  147.               (LPSTR)szCurDir );
  148.     WinExec  (szDstFile, cmdShow);
  149.     }
  150.  
  151.   GlobalUnlock ( hBuffer );
  152.   GlobalFree   ( hBuffer );
  153.  
  154.   return FALSE;
  155. }
  156. //*******************************************************************
  157. //
  158. //  Returns TRUE if no DOS error, FALSE if bad
  159. //
  160. //*******************************************************************
  161. BOOL DosCopy ( LPSTR szSrc, LPSTR szDest )
  162. {
  163.   LONG     FileSize;
  164.   WORD     NumChunks;
  165.   WORD     LastChunk;
  166.   int      i;
  167.   BOOL     err;
  168.   HANDLE   hSource, hDest;
  169.   OFSTRUCT of1, of2;
  170.   WORD     NumBytes;
  171.  
  172.   err = FALSE;
  173.  
  174.   if (-1 == (hSource = OpenFile ( szSrc,  &of1, OF_READ   ))) return FALSE;
  175.  
  176.   if (-1 == (hDest   = OpenFile ( szDest, &of2, OF_CREATE )))
  177.     {
  178.     _lclose ( hSource );
  179.     return FALSE;
  180.     }
  181.  
  182.   if ((LONG)-1 == (FileSize = _llseek (hSource, 0L, 2)))  // moves to EOF
  183.     {
  184.     _lclose ( hSource );
  185.     _lclose ( hDest );
  186.     return FALSE;  
  187.     }
  188.  
  189.   // NumChunks = (WORD)  ((LONG)FileSize / CHUNKSIZE );
  190.   // LastChunk = (WORD)  ((LONG)FileSize % CHUNKSIZE );
  191.   //
  192.   // The ASM below does it without calling C runtime.
  193.   //
  194.  
  195.   _asm
  196.      {
  197.      mov    ax,WORD PTR FileSize
  198.      push   ax
  199.      and    ax,CHUNKSIZE
  200.      mov    LastChunk,AX
  201.      mov    dx,WORD PTR FileSize+2
  202.      pop    ax
  203.      and    ax,NOTCHUNKSIZE
  204.      mov    cl,POWEROFCHUNK
  205.      shr    ax,cl
  206.      mov    cl,NOTPOWEROFCHUNK
  207.      shl    dx,cl
  208.      add    dx,ax
  209.      mov    NumChunks,dx
  210.      }
  211.  
  212.   if ((LONG)-1 == _llseek ( hSource, 0L, 0 ))
  213.     {
  214.     _lclose ( hSource );
  215.     _lclose ( hDest );
  216.     return FALSE;  
  217.     }
  218.  
  219.   for ( i = 0; (((WORD)i < NumChunks) && (!err)); i++ )
  220.     {
  221.     err |= ( (NumBytes = _lread  ( hSource, Buffer, CHUNKSIZE+1 )) <= 0);
  222.     if (!err) err |= ( (NumBytes =_lwrite ( hDest  , Buffer, CHUNKSIZE+1 )) <= 0);
  223.     }
  224.   
  225.   if ((LastChunk) && (!err))
  226.     {
  227.     err |= ( (NumBytes =_lread  ( hSource, Buffer, LastChunk )) <= 0 );
  228.     if (!err) err |= ( (NumBytes = _lwrite ( hDest  , Buffer, LastChunk )) <= 0);
  229.     }
  230.  
  231.   err |= (-1 == _lclose ( hSource ));
  232.   err |= (-1 == _lclose ( hDest   ));
  233.  
  234.   return !err;
  235. }
  236. //*******************************************************************
  237. void DosDelete ( char *szSrc )
  238. {
  239.   HANDLE   hSource;
  240.   OFSTRUCT of1;
  241.  
  242.   hSource = OpenFile ( szSrc,  &of1, OF_EXIST );
  243.   if (hSource > 0)  // If OK
  244.     {
  245.     hSource = OpenFile ( szSrc,  &of1, OF_DELETE );
  246.     _lclose ( hSource );
  247.     }
  248. }
  249. //*******************************************************************
  250.  
  251.