home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / c / mobjm260 / install.pa_ / install.pa
Encoding:
Text File  |  1994-09-06  |  9.3 KB  |  312 lines

  1. (*********************************************************************)
  2. (*                                                                   *)
  3. (*          Microworks ObjectMate 2.6                                                        *)
  4. (*                                                                   *)
  5. (*     Windows Interface Develpment Kit for the Borland Languages.   *)
  6. (*                                                                   *)
  7. (*         INSTALL.PAS : Installation Initialization Program                 *)
  8. (*                                                                                                                             *)
  9. (*     Copyright 1992-94 Microworks Sydney, Australia.                 *)
  10. (*                                                                   *)
  11. (*********************************************************************)
  12.  
  13. (* Install is the installation initialization program. It expands the main
  14.  * installation program MWINSTAL.EX_ into the Windows system subdirectory
  15.  * and checks to see if there is an old version of SFX200.DLL. If it finds
  16.  * one, or if SFX200.DLL is not found, it copies the new version into the
  17.  * Windows system subdirectory. Like MWInstal, Install is a generic Windows
  18.  * program to ensure the smallest possible executable file size.
  19.  *
  20.  * Files must be compressed with COMPRESS.EXE using the /r option followed by the
  21.  * the files full path name. You can compress whole directories or all files with
  22.  * the same extension using wild cards. For example, 'compress /r c:\temp\*.*'
  23.  * compresses all the files in the c:\temp directory.
  24.  *
  25.  * When you compress a file with compress using the /r option, the original filename
  26.  * is stored in the compressed file and is the file name returned by GetExpandedName.
  27.  * You can specify the compressed file name at the time of compression but if you do
  28.  * the original file name is not stored in the compressed file and must be specifed
  29.  * at the time of expansion.
  30.  *)
  31.  
  32. program Install;
  33.  
  34. uses WinTypes, WinProcs, WinDos, Win31, ShellAPI, LZExpand;
  35.  
  36. {$R Install.res}
  37.  
  38. const
  39.  
  40.         { Current SFX200.DLL version number -> 2.6 }
  41.     SFXVERSION = $0260;
  42.  
  43. var
  44.  
  45.     GetSFXVersion : function : Word;
  46.     FromDir       : array[0..fsDirectory] of Char;
  47.     ExpandLib     : THandle;
  48.     SplashWnd     : HWnd;
  49.  
  50. {********** CenterWindow **********}
  51.  
  52. procedure CenterWindow(hWndCenter: HWnd);
  53. var
  54.     Center     : TRect;
  55.     Desktop    : TRect;
  56.     X, Y, W, H : Integer;
  57. begin
  58.     GetWindowRect(GetDesktopWindow, Desktop);
  59.     GetWindowRect(hWndCenter, Center);
  60.     W := (Center.right - Center.left);
  61.     H := (Center.bottom - Center.top);
  62.     X := ((Desktop.right - Desktop.left) - W) div 2;
  63.     Y := ((Desktop.bottom - Desktop.top) - H) div 2;
  64.     MoveWindow(hWndCenter, X, Y, W, H, TRUE);
  65. end;
  66.  
  67. {********** ErrorMsg **********}
  68.  
  69. procedure ErrorMsg(Msg: PChar);
  70. begin
  71.     MessageBeep(MB_ICONSTOP);
  72.     MessageBox(GetActiveWindow, Msg, 'Installation Error', MB_ICONSTOP);
  73. end;
  74.  
  75. {********** ExpandFile **********}
  76.  
  77. function ExpandFile(Dir, FileName: PChar): BOOL;
  78. var
  79.     Src, Dest       : array[0..fsPathName] of Char;
  80.     ExpandedFile    : array[0..fsFileName] of Char;
  81.     ExpandedExt     : array[0..fsExtension] of Char;
  82.     FileStr         : PChar;
  83.     OFSrc, OFDst    : TOFStruct;
  84.     InFile, OutFile : Integer;
  85. begin
  86.     ExpandFile := FALSE;
  87.  
  88.     (* Get the expanded file name
  89.      *)
  90.     lstrcat(lstrcpy(Src, FromDir), FileName);
  91.     if GetExpandedName(Src, Dest) = -1 then
  92.     begin
  93.         FileStr := Src;
  94.         wvsprintf(Dest, 'Unable to expand the file %s. It''s missing!', FileStr);
  95.         ErrorMsg(Dest);
  96.         Exit;
  97.     end;
  98.  
  99.     (* Extract the expanded file name and extension and append them to the
  100.      * supplied installation dir.
  101.      *)
  102.     FileSplit(Dest, nil, ExpandedFile, ExpandedExt);
  103.     lstrcat(lstrcat(lstrcpy(Dest, Dir), ExpandedFile), ExpandedExt);
  104.  
  105.     (* Open source file
  106.      *)
  107.     InFile := LZOpenFile(Src, OFSrc, OF_READ);
  108.     if InFile = -1 then
  109.     begin
  110.         ErrorMsg('Unable to open compressed file. Ensure that compressed installation ' +
  111.                          'files are in the same directroy as install.exe and run Install again.');
  112.         Exit;
  113.     end;
  114.  
  115.     (* Open destination file
  116.      *)
  117.     OutFile := LZOpenFile(Dest, OFDst, OF_CREATE);
  118.     if OutFile = -1 then
  119.     begin
  120.         ErrorMsg('Unable to create uncompressed file.');
  121.         Exit;
  122.     end;
  123.  
  124.     (* Expand file
  125.      *)
  126.     LZCopy(InFile, OutFile);
  127.     LZClose(InFile);
  128.     LZClose(OutFile);
  129.     ExpandFile := TRUE;
  130. end;
  131.  
  132. {********** HourGlass **********}
  133.  
  134. procedure HourGlass(On: Boolean);
  135. const
  136.     CursorSave: HCursor = 0;
  137. begin
  138.     if On then
  139.         CursorSave := SetCursor(LoadCursor(0, IDC_WAIT))
  140.     else
  141.         SetCursor(CursorSave);
  142. end;
  143.  
  144. {********** SplashDlgProc **********}
  145.  
  146. function SplashDlgProc (Dlg: HWnd; Message, wParam: Word; lParam: Longint): BOOL; export;
  147. begin
  148.     SplashDlgProc := TRUE;
  149.  
  150.     case Message of
  151.  
  152.         WM_INITDIALOG:
  153.         begin
  154.             CenterWindow(Dlg);
  155.             Exit;
  156.         end;
  157.        
  158.         WM_CTLCOLOR:
  159.         begin
  160.             case HIWORD(lParam) of
  161.                 CTLCOLOR_DLG, CTLCOLOR_STATIC:
  162.                 begin
  163.                     SetBkMode(wParam, TRANSPARENT);
  164.                     SplashDlgProc := BOOL(GetStockObject(LTGRAY_BRUSH));
  165.                     Exit;
  166.                 end;
  167.             end;
  168.         end;
  169.  
  170.         WM_DESTROY:
  171.         begin
  172.             PostQuitMessage(0);
  173.             Exit;
  174.         end
  175.  
  176.     end;
  177.     SplashDlgProc := FALSE;
  178. end;
  179.  
  180. {********** DefaultProc **********}
  181.  
  182. function DefaultProc (Dlg: HWnd; Message, wParam: Word; lParam: Longint): Longint; export;
  183. begin
  184.     DefaultProc := DefDlgProc(Dlg, Message, wParam, lParam);
  185. end;
  186.  
  187. procedure WinMain;
  188. label
  189.     LoadSFX200, NoLoadSFX200;
  190. var
  191.     StartApp   : array[0..fsPathName] of char;
  192.     SystemPath : array[0..fsPathName] of Char;
  193.     SystemDir  : array[0..fsDirectory] of Char;
  194.     i, Usage   : Integer;
  195.     SplashProc : TFarProc;
  196.     SFXLib     : THandle;
  197.     Msg        : TMsg;
  198.     WndClass   : TWndClass;
  199.     Version    : Word;
  200. begin
  201.   if HPrevInst = 0 then
  202.   begin
  203.         FillChar(WndClass, SizeOf(TWndClass), #0);
  204.         WndClass.lpszClassName := 'SPLASHDIALOG';
  205.         WndClass.lpfnWndProc   := @DefaultProc;
  206.         WndClass.cbClsExtra    := 0;
  207.         WndClass.cbWndExtra    := DLGWINDOWEXTRA;
  208.         WndClass.hInstance     := HInstance;
  209.         WndClass.hIcon         := 0;
  210.         WndClass.hCursor       := LoadCursor(0, IDC_ARROW);
  211.         WndClass.hbrBackground := 0;
  212.         WndClass.lpszMenuName  := nil;
  213.         WndClass.style         := 0;
  214.         if not RegisterClass(WndClass) then Halt(255);
  215.     end
  216.     else
  217.         Halt(255);
  218.  
  219.     (* Retrieve the full path of install.exe and use it to extract the drive and
  220.      * directory of the files to be installed.
  221.      *)
  222.     GetModuleFileName(HInstance, StartApp, fsPathName - 1);
  223.     FileSplit(StartApp, FromDir, nil, nil);
  224.     lstrcpy(CmdLine, FromDir);
  225.     if CmdLine[lstrlen(CmdLine) - 1] = '\' then
  226.         CmdLine[lstrlen(CmdLine) - 1] := #0;
  227.  
  228.     (* Load lzexpand.dll to decompress the main installation program and
  229.      * SFX200.DLL
  230.      *)
  231.     SetErrorMode(SEM_NOOPENFILEERRORBOX);
  232.     ExpandLib := LoadLibrary('lzexpand.dll');
  233.     if ExpandLib < HINSTANCE_ERROR then
  234.     begin
  235.         ErrorMsg('Unable to load lzexpand.dll. Make sure this file is in your ' +
  236.                          'Windows system subdirectory and run Install again.');
  237.         Halt(255);
  238.     end;
  239.  
  240.     (* Load dialog splash panel so user has something to look at while install
  241.      * copies the main installation program and SFX200.DLL into Windows system
  242.      * subdirectory.
  243.      *)
  244.     SplashProc := MakeProcInstance(@SplashDlgProc, HInstance);
  245.     SplashWnd := CreateDialog(HInstance, 'SPLASHDIALOG', 0, SplashProc);
  246.     ShowWindow(SplashWnd, CmdShow);
  247.     UpdateWindow(SplashWnd);
  248.     HourGlass(TRUE);
  249.  
  250.     (* Retrieve the drive and directory name of the Windows system
  251.      * subdirectory.
  252.      *)
  253.     GetSystemDirectory(SystemPath, fsPathName - 1);
  254.     if SystemPath[lstrlen(SystemPath) - 1] <> '\' then
  255.         lstrcat(SystemPath, '\');
  256.     FileSplit(SystemPath, SystemDir, nil, nil);
  257.  
  258.     (* Check to see if he user has a old version of SFX200.DLL and if so,
  259.      * if its loaded into memory remove it and load the new version  of
  260.      * SFX200.DLL.
  261.      *
  262.      * If SFX200.DLL was loaded into memory prior to installation, removing it 
  263.      * will cause the programs that use it to crash when the installation is 
  264.      * finished. To prevent this from happening Windows should be restarted. 
  265.      * If SFX200.DLL was not loaded into memory prior to installation, Windows 
  266.      * doesn't need to be restarted. CmdLine is used to pass this information
  267.      * onto MWINSTAL.EXE. 
  268.      *
  269.      * CmdLine passes the installation path to MWINSTAL.EXE, so it knows
  270.      * where to find the installation files. At the beginning of WinMain the 
  271.      * terminal backslash was stripped from the installation directory path. 
  272.      * If SFX200.DLL was loaded into memory prior to installation the backslash 
  273.      * re appended.  
  274.      *)
  275.     SFXLib := LoadLibrary('SFX200.DLL');
  276.     if SFXLib >= HINSTANCE_ERROR then
  277.     begin
  278.         @GetSFXVersion := GetProcAddress(SFXLib, 'GetSFXVersion');
  279.         Version := GetSFXVersion;
  280.         if Version < SFXVERSION then
  281.         begin
  282.             Usage := GetModuleUsage(SFXLib);
  283.             if Usage > 1 then
  284.                 lstrcat(Cmdline, '\');
  285.             for i := 0 to Usage do
  286.                 FreeLibrary(SFXLib);
  287.             goto LoadSFX200;
  288.         end
  289.         else
  290.             goto NoLoadSFX200;
  291.     end;
  292.  
  293.     LoadSFX200:
  294.     ExpandFile(SystemDir, 'SFX200.DL_');
  295.  
  296.     NoLoadSFX200:
  297.     ExpandFile(SystemDir, 'MWINSTAL.EX_');
  298.     FreeLibrary(ExpandLib);
  299.     FreeLibrary(SFXLib);
  300.     ShowWindow(SplashWnd, SW_HIDE);
  301.     if (ShellExecute(0, nil, 'MWINSTAL.EXE', CmdLine, '.\', SW_NORMAL) < HINSTANCE_ERROR) then
  302.         ErrorMsg('Unable to execute oinstall.exe. You can run oinstall.exe ' +
  303.                          'by specifying its directory name as a command line parameter.');
  304.     DestroyWindow(SplashWnd);
  305.     HourGlass(FALSE);
  306.     Halt(Msg.wParam);
  307. end;
  308.  
  309. begin
  310.   WinMain;
  311. end.
  312.