home *** CD-ROM | disk | FTP | other *** search
- #include "windows.h"
-
- #define INSTALLAPPNAME "BINXZ.EXE"
- #define INSTALLINFFILE "SETUP.INF"
-
- // These must be bitwise opposite words (add to 65535)
-
- #define CHUNKSIZE 16383 // 0011 1111 1111 1111
- #define NOTCHUNKSIZE 49152 // 1100 0000 0000 0000
-
- // These must add to 16, and reflect the bits above
-
- #define POWEROFCHUNK 14 // ^^ ^^^^ ^^^^ ^^^^
- #define NOTPOWEROFCHUNK 2 // ^^
-
- /*********************************************************************
-
- SETUP.C - Generic Windows Setup program.
-
- This is the SETUP.EXE program source. It checks
- to see if the setup is being run from a floppy drive.
- If it is, then SETUP.EXE will copy the installation
- program (BINCODE.EXE in this example) over to the
- Windows TEMP directory and then run it from the
- hard disk. This allows the floppy disks to
- be swapped without fear of losing code segments,
- and allows the setup programs to have moveable,
- discardable code segments.
-
- HISTORY: Created 08-08-90 Dave
- Added more robust copy 08-14-90 Dave
- Fixed zillions of bugs 08-14-90 -> 09-10-90 Dave
-
-
- *********************************************************************/
-
-
-
- /************************* FUNCTION PROTOTYPES **********************/
-
- int PASCAL WinMain ( HANDLE, HANDLE, LPSTR, int );
- BOOL DosCopy ( LPSTR, LPSTR );
- void DosDelete ( char * );
-
- /************************* GLOBAL VARIABLE ***********************/
-
- LPSTR Buffer; // Dos Copy buffer
- HANDLE hBuffer;
-
- /********************************************************************/
-
- int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance,
- LPSTR lpszCmdLine, int cmdShow )
- {
-
- char szModuleFileName[256]; // Name and path of this .EXE
- char szCurDir[256]; // The path of this .EXE
- char szWindowsDir[256]; // Where Windows Lives
- char szSrcFile[256]; // Name of this .EXE (without path)
- char szDstFile[256]; // Destination path of main install program
- int i; // Dinky counter
- BOOL bSuccess; // Return Error value from DosCopy
- HWND hWnd;
-
-
- // If there is another version of setup running, run it
-
- if (hWnd = FindWindow("BINCODE", NULL))
- {
- hWnd = GetLastActivePopup(hWnd);
- BringWindowToTop(hWnd);
- if (IsIconic(hWnd))
- ShowWindow(hWnd, SW_RESTORE);
- return FALSE;
- }
-
- hBuffer = GlobalAlloc ( GMEM_MOVEABLE, CHUNKSIZE+1 );
- Buffer = GlobalLock ( hBuffer );
-
- // Find out where this app is getting run from...
-
- GetModuleFileName(hInstance,szModuleFileName,255);
-
- // Check for running off a Floppy Disk
-
- if ((*szModuleFileName == 'A') || (*szModuleFileName == 'B'))
- {
- // If this is running from a floppy, copy it to your windows
- // directory, and rerun it
-
- GetWindowsDirectory ( (LPSTR)szWindowsDir, 250 );
-
- // Extract the current directory
-
- wsprintf ( (LPSTR)szCurDir, (LPSTR)szModuleFileName );
- i = lstrlen ( (LPSTR)szCurDir );
- i--;
- while ( i && szCurDir[i] != '\\' ) i--;
- szCurDir[i+1] = 0;
-
- wsprintf ((LPSTR)szSrcFile, (LPSTR)"%s%s", (LPSTR)szCurDir, (LPSTR)INSTALLINFFILE );
- wsprintf ((LPSTR)szDstFile, (LPSTR)"%s\\%s", (LPSTR)szWindowsDir, (LPSTR)INSTALLINFFILE );
-
- SetCursor ( LoadCursor ( NULL, IDC_WAIT ));
-
- bSuccess = DosCopy ((LPSTR)szSrcFile, (LPSTR)szDstFile);
- if (!bSuccess) DosDelete ( szDstFile );
-
- wsprintf ((LPSTR)szSrcFile, (LPSTR)"%sBIN\\%s", (LPSTR)szCurDir, (LPSTR)INSTALLAPPNAME );
- wsprintf ((LPSTR)szDstFile, (LPSTR)"%s\\%s", (LPSTR)szWindowsDir, (LPSTR)INSTALLAPPNAME );
-
- if (bSuccess) bSuccess = DosCopy ((LPSTR)szSrcFile, (LPSTR)szDstFile);
- if (!bSuccess)
- {
- DosDelete ( szDstFile ); // Get rid of BINCODE.EXE
- wsprintf ((LPSTR)szDstFile, (LPSTR)"%s\\%s", (LPSTR)szWindowsDir, (LPSTR)INSTALLINFFILE );
- DosDelete ( szDstFile ); // Get rid of SETUP.INF
- }
-
- // By spawining and passing extra params, we know to delete ourself
- // from the temp directory afterwards
-
- SetCursor ( LoadCursor ( NULL, IDC_ARROW ));
-
- wsprintf ((LPSTR)szDstFile, (LPSTR)"%s\\%s AEKDB %s %s\\%s %s\\%s",
- (LPSTR)szWindowsDir, (LPSTR)INSTALLAPPNAME,
- (LPSTR)szCurDir,
- (LPSTR)szWindowsDir, (LPSTR)INSTALLINFFILE,
- (LPSTR)szWindowsDir, (LPSTR)INSTALLAPPNAME );
-
- if (bSuccess)
- {
- WinExec (szDstFile, cmdShow );
- }
- else
- {
- wsprintf ((LPSTR)szDstFile, (LPSTR)"There was an error copying files to your %s subdirectory. "
- "Please correct the problem and run Setup again.", (LPSTR)szWindowsDir );
-
- MessageBox ( GetFocus(), szDstFile, "Setup", MB_OK | MB_ICONHAND );
- }
- }
- else // if running off a hard disk or network, just spawn it...
- {
- wsprintf ((LPSTR)szDstFile, (LPSTR)"BIN\\%s AEKDB %s",
- (LPSTR)INSTALLAPPNAME,
- (LPSTR)szCurDir );
- WinExec (szDstFile, cmdShow);
- }
-
- GlobalUnlock ( hBuffer );
- GlobalFree ( hBuffer );
-
- return FALSE;
- }
- //*******************************************************************
- //
- // Returns TRUE if no DOS error, FALSE if bad
- //
- //*******************************************************************
- BOOL DosCopy ( LPSTR szSrc, LPSTR szDest )
- {
- LONG FileSize;
- WORD NumChunks;
- WORD LastChunk;
- int i;
- BOOL err;
- HANDLE hSource, hDest;
- OFSTRUCT of1, of2;
- WORD NumBytes;
-
- err = FALSE;
-
- if (-1 == (hSource = OpenFile ( szSrc, &of1, OF_READ ))) return FALSE;
-
- if (-1 == (hDest = OpenFile ( szDest, &of2, OF_CREATE )))
- {
- _lclose ( hSource );
- return FALSE;
- }
-
- if ((LONG)-1 == (FileSize = _llseek (hSource, 0L, 2))) // moves to EOF
- {
- _lclose ( hSource );
- _lclose ( hDest );
- return FALSE;
- }
-
- // NumChunks = (WORD) ((LONG)FileSize / CHUNKSIZE );
- // LastChunk = (WORD) ((LONG)FileSize % CHUNKSIZE );
- //
- // The ASM below does it without calling C runtime.
- //
-
- _asm
- {
- mov ax,WORD PTR FileSize
- push ax
- and ax,CHUNKSIZE
- mov LastChunk,AX
- mov dx,WORD PTR FileSize+2
- pop ax
- and ax,NOTCHUNKSIZE
- mov cl,POWEROFCHUNK
- shr ax,cl
- mov cl,NOTPOWEROFCHUNK
- shl dx,cl
- add dx,ax
- mov NumChunks,dx
- }
-
- if ((LONG)-1 == _llseek ( hSource, 0L, 0 ))
- {
- _lclose ( hSource );
- _lclose ( hDest );
- return FALSE;
- }
-
- for ( i = 0; (((WORD)i < NumChunks) && (!err)); i++ )
- {
- err |= ( (NumBytes = _lread ( hSource, Buffer, CHUNKSIZE+1 )) <= 0);
- if (!err) err |= ( (NumBytes =_lwrite ( hDest , Buffer, CHUNKSIZE+1 )) <= 0);
- }
-
- if ((LastChunk) && (!err))
- {
- err |= ( (NumBytes =_lread ( hSource, Buffer, LastChunk )) <= 0 );
- if (!err) err |= ( (NumBytes = _lwrite ( hDest , Buffer, LastChunk )) <= 0);
- }
-
- err |= (-1 == _lclose ( hSource ));
- err |= (-1 == _lclose ( hDest ));
-
- return !err;
- }
- //*******************************************************************
- void DosDelete ( char *szSrc )
- {
- HANDLE hSource;
- OFSTRUCT of1;
-
- hSource = OpenFile ( szSrc, &of1, OF_EXIST );
- if (hSource > 0) // If OK
- {
- hSource = OpenFile ( szSrc, &of1, OF_DELETE );
- _lclose ( hSource );
- }
- }
- //*******************************************************************
-