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

  1. //    Microworks ObjectMate  2.6
  2. //
  3. //    ObjectMate Installation
  4. //
  5. //    Copyright 1992-94 Microworks Sydney, Australia.
  6. //
  7. //  INSTALL.CPP
  8.  
  9. /* Install is the installation initialization program. It expands the main
  10.  * installation program MWINSTAL.EX_ into the Windows system subdirectory
  11.  * and checks to see if there is an old version of SFX200.DLL. If it finds
  12.  * one, or if SFX200.DLL is not found, it copies the new version into the
  13.  * Windows system subdirectory. Like MWInstal, Install is a generic Windows
  14.  * program to ensure the smallest possible executable file size.
  15.  *
  16.  * Files must be compressed with COMPRESS.EXE using the /r option followed by the
  17.  * the files full path name. You can compress whole directories or all files with
  18.  * the same extension using wild cards. For example, 'compress /r c:\temp\*.*'
  19.  * compresses all the files in the c:\temp directory.
  20.  *
  21.  * When you compress a file with compress using the /r option, the original filename
  22.  * is stored in the compressed file and is the file name returned by GetExpandedName.
  23.  * You can specify the compressed file name at the time of compression but if you do
  24.  * the original file name is not stored in the compressed file and must be specifed
  25.  * at the time of expansion.
  26.  */
  27.  
  28. #include "install.h"
  29.  
  30. //    WinMain
  31.  
  32. int PASCAL
  33. WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdParam, int nCmdShow)
  34. {
  35.     char      systemPath[MAXPATH];
  36.     char      systemDrive[MAXDRIVE];
  37.     char      systemDir[MAXDIR];
  38.     DLGPROC   splashproc;
  39.     HINSTANCE hSFXLib;
  40.     int       i, usage;
  41.  
  42.     if (!hPrevInstance)
  43.     {
  44.         WNDCLASS wndclass;
  45.         memset(&wndclass, 0, sizeof(WNDCLASS));
  46.         wndclass.lpszClassName = "SPLASHDIALOG";
  47.         wndclass.lpfnWndProc   = (WNDPROC) DefaultProc;
  48.         wndclass.cbClsExtra    = 0;
  49.         wndclass.cbWndExtra    = DLGWINDOWEXTRA;
  50.         wndclass.hInstance     = hInstance;
  51.         wndclass.hIcon         = 0;
  52.         wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  53.         wndclass.hbrBackground = 0;
  54.         wndclass.lpszMenuName  = 0;
  55.         wndclass.style         = 0;
  56.         if (!RegisterClass(&wndclass)) exit(0);
  57.     }
  58.     else
  59.         return 0;
  60.  
  61.     /* Retrieve the full path of install.exe and use it to extract the drive and
  62.      * directory of the files to be installed.
  63.      */
  64.     char startApp[MAXPATH];
  65.     GetModuleFileName(hInstance, startApp, MAXPATH - 1);
  66.     fnsplit(startApp, FromDrive, FromDir, 0, 0);
  67.     lstrcat(lstrcpy(lpCmdParam, FromDrive), FromDir);
  68.     if (lpCmdParam[lstrlen(lpCmdParam) - 1] == '\\')
  69.         lpCmdParam[lstrlen(lpCmdParam) - 1] = '\0';
  70.         
  71.     /* Load lzexpand.dll to decompress the main installation program and
  72.      * SFX200.DLL
  73.      */
  74.     SetErrorMode(SEM_NOOPENFILEERRORBOX);
  75.     LZLib = LoadLibrary("lzexpand.dll");
  76.     if (LZLib < HINSTANCE_ERROR)
  77.     {
  78.         ErrorMsg("Unable to load lzexpand.dll. Make sure this file is in your "
  79.                  "Windows system subdirectory and run Install again.");
  80.         return 0;
  81.     }
  82.  
  83.     /* Load dialog splash panel so user has something to look at while install
  84.      * copies the main installation program and SFX200.DLL into Windows system
  85.      * subdirectory.
  86.      */
  87.     splashproc = (DLGPROC) MakeProcInstance((FARPROC) SplashProc, hInstance);
  88.     SplashWnd = CreateDialog(hInstance, "SPLASHDIALOG", 0, splashproc);
  89.     ShowWindow(SplashWnd, nCmdShow);
  90.     UpdateWindow(SplashWnd);
  91.     HourGlass(TRUE);
  92.  
  93.     /* Get the drive and directory name for the Windows system
  94.      * subdirectory.
  95.      */
  96.     GetSystemDirectory(systemPath, MAXPATH - 1);
  97.     if (systemPath[lstrlen(systemPath) - 1] != '\\')
  98.     {
  99.         lstrcat(systemPath, "\\");
  100.     }
  101.     fnsplit(systemPath, systemDrive, systemDir, 0, 0);
  102.  
  103.     /* Check to see if he user has a old version of SFX200.DLL and if so,
  104.      * if its loaded into memory remove it and load the new version  of
  105.      * SFX200.DLL.
  106.      *
  107.      * If SFX200.DLL was loaded into memory prior to installation, removing it 
  108.      * will cause the programs that use it to crash when the installation is 
  109.      * finished. To prevent this from happening Windows should be restarted. 
  110.      * If SFX200.DLL was not loaded into memory prior to installation, Windows 
  111.      * doesn't need to be restarted. lpCmdParam is used to pass this information
  112.      * onto MWINSTAL.EXE. 
  113.      *
  114.      * lpCmdParam passes the installation path to MWINSTAL.EXE, so it knows 
  115.      * where to find the installation files. At the beginning of WinMain the 
  116.      * terminal backslash was stripped from the installation directory path. 
  117.      * If SFX200.DLL was loaded into memory prior to installation the backslash 
  118.      * re appended.  
  119.      */
  120.     hSFXLib = LoadLibrary("SFX200.DLL");
  121.     if (hSFXLib >= HINSTANCE_ERROR)
  122.     {
  123.         UINT (far* lpfnGetSFXVersion)();
  124.         (FARPROC) lpfnGetSFXVersion = GetProcAddress(hSFXLib, "GetSFXVersion");
  125.         UINT version = (UINT)lpfnGetSFXVersion();
  126.         if (version < SFXVERSION)
  127.         {
  128.             usage = GetModuleUsage(hSFXLib);
  129.             if (usage > 1)
  130.                 lstrcat(lpCmdParam, "\\");
  131.             for (i = 0; i < usage; i++)
  132.             {
  133.                 FreeLibrary(hSFXLib);
  134.             }
  135.             goto LoadSFX200;
  136.         }
  137.         else
  138.             goto NoLoadSFX200;
  139.     }
  140.     LoadSFX200:
  141.     ExpandFile(systemDrive, systemDir, "SFX200.DL_");
  142.  
  143.     NoLoadSFX200:
  144.     ExpandFile(systemDrive, systemDir, "MWINSTAL.EX_");
  145.     FreeLibrary(LZLib);
  146.     FreeLibrary(hSFXLib);
  147.     ShowWindow(SplashWnd, SW_HIDE);
  148.     if (ShellExecute(0, 0, "MWINSTAL.EXE", lpCmdParam, ".\\", SW_NORMAL) < HINSTANCE_ERROR)
  149.         ErrorMsg("Unable to execute mwinstall.exe. You can run mwnstall.exe "
  150.                  "by specifying its directory path as a command line parameter.");  
  151.     DestroyWindow(SplashWnd);
  152.     HourGlass(FALSE);
  153.     return 0;
  154. }
  155.  
  156. //    DefaultProc
  157.  
  158. long FAR PASCAL _export
  159. DefaultProc(HWND hDlg, UINT message, WPARAM wParam , LPARAM lParam)
  160. {
  161.     return DefDlgProc(hDlg, message, wParam, lParam);
  162. }
  163.  
  164. //    SplashProc
  165.  
  166. BOOL CALLBACK _export
  167. SplashProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  168. {
  169.     switch (message)
  170.     {
  171.            case WM_INITDIALOG:
  172.             CenterWindow(hDlg);
  173.             break;
  174.        
  175.         case WM_CTLCOLOR:
  176.             switch (HIWORD(lParam))
  177.             {
  178.                 case CTLCOLOR_STATIC:
  179.                     SetBkMode((HDC)wParam, TRANSPARENT);
  180.                     return (BOOL)GetStockObject(LTGRAY_BRUSH);
  181.  
  182.                 case CTLCOLOR_DLG:
  183.                     return (BOOL)GetStockObject(LTGRAY_BRUSH);
  184.  
  185.                 default:
  186.                     return FALSE;
  187.             }
  188.  
  189.         case WM_DESTROY:
  190.             PostQuitMessage(0);
  191.             break;
  192.  
  193.         default:
  194.             return FALSE;       
  195.     }
  196.     return TRUE;
  197. }
  198.  
  199. //    CenterWindow
  200.  
  201. void
  202. CenterWindow(HWND hWndCenter)
  203. {
  204.     RECT center;
  205.     RECT desktop;
  206.     int  x, y, w, h;
  207.  
  208.     GetWindowRect(GetDesktopWindow(), &desktop);
  209.     GetWindowRect(hWndCenter, ¢er);
  210.     w = (center.right - center.left);
  211.     h = (center.bottom - center.top);
  212.     x  = ((desktop.right - desktop.left) - w) / 2;
  213.     y  = ((desktop.bottom - desktop.top) - h) / 2;
  214.     MoveWindow(hWndCenter, x, y, w, h, TRUE);
  215. }
  216.  
  217. //    HourGlass
  218.  
  219. void
  220. HourGlass(int on)
  221. {
  222.     static HCURSOR hCursorSave;
  223.     if (on)
  224.         hCursorSave = SetCursor(LoadCursor(0, IDC_WAIT));
  225.     else
  226.         SetCursor(hCursorSave);
  227. }
  228.  
  229. //    ErrorMsg
  230.  
  231. void
  232. ErrorMsg(char far* msg)
  233. {
  234.     MessageBeep(MB_ICONSTOP);
  235.     MessageBox(GetActiveWindow(), msg, "Installation Error", MB_ICONSTOP);
  236. }
  237.  
  238. //    ExpandFile
  239.  
  240. int
  241. ExpandFile(char* drive, char* dir, char far* filename)
  242. {
  243.     char src[MAXPATH];
  244.     char dest[MAXPATH];
  245.     char ExpandedFile[MAXFILE];
  246.     char ExpandedExt[MAXEXT];
  247.  
  248.     /* Get the expanded file name
  249.      */
  250.     sprintf(src, "%s%s%s", FromDrive, FromDir, filename);
  251.     if (GetExpandedName(src, dest) == -1)
  252.     {
  253.         char text[80];
  254.         sprintf(text, "Unable to expand the file %s.\r\nIt's missing!", src);
  255.         ErrorMsg(text);
  256.         return FALSE;
  257.     }
  258.  
  259.     /* Extract the expanded file name and extension and build the destination path
  260.      */
  261.     fnsplit(dest, 0, 0, ExpandedFile, ExpandedExt);
  262.     sprintf(dest, "%s%s%s%s", drive, dir, ExpandedFile, ExpandedExt);
  263.  
  264.     OFSTRUCT ofSrc, ofDst;
  265.     HFILE in = LZOpenFile(src, &ofSrc, OF_READ);
  266.     if (in == -1)
  267.     {
  268.         ErrorMsg("Unable to open compressed file. Ensure that compressed installation "
  269.                  "files are in the same directroy as install.exe and run Install again.");
  270.         return FALSE;
  271.     }
  272.     HFILE out = LZOpenFile(dest, &ofDst, OF_CREATE);
  273.     if (out == -1)
  274.     {
  275.         ErrorMsg("Unable to create uncompressed file.");
  276.         return FALSE;
  277.     }  
  278.  
  279.     /* Decompress the file
  280.      */
  281.     LZCopy(in, out);
  282.     LZClose(in);
  283.     LZClose(out);
  284.     return TRUE;
  285. }
  286.  
  287.