home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: InfoMgt / InfoMgt.zip / shr93.zip / SHRINST.C < prev    next >
Text File  |  1993-09-23  |  9KB  |  324 lines

  1. /*
  2.  * OS/2 Work Place Shell Sample Program - Installation
  3.  *
  4.  * Copyright (C) 1993 IBM Corporation
  5.  *
  6.  *   DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  7.  *   sample code created by IBM Corporation.  This sample code is
  8.  *   not part of any standard or IBM product and is provided to you
  9.  *   solely for the purpose of assisting you in the development of
  10.  *   your applications.  The code is provided "AS IS".  ALL
  11.  *   WARRANTIES ARE EXPRESSLY DISCLAIMED, INCLUDING THE IMPLIED
  12.  *   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  13.  *   PURPOSE.  IBM shall not be liable for any damages arising out
  14.  *   of your use of the sample code, even if IBM has been advised of
  15.  *   the possibility of such damages.
  16.  */
  17.  
  18. #define INCL_ERRORS
  19. #define INCL_WPCLASS
  20. #define INCL_WIN
  21. #define INCL_DOS
  22. #include <os2.h>
  23. #include <string.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <malloc.h>
  27.  
  28. #include "shrinst.h"
  29.  
  30. CHAR vszMessageTitle[] = "Address Book - Installation";
  31. CHAR vszSysDLLPath[CCHMAXPATH];
  32.  
  33. extern BOOL IsShareRegistered(VOID)
  34. {
  35.   POBJCLASS pObjectClass, pObjectClassNext;
  36.   ULONG cb;
  37.   BOOL bIsRegistered = FALSE;
  38.  
  39.   if (WinEnumObjectClasses(NULL, &cb))
  40.   {
  41.     pObjectClass = malloc(cb);
  42.  
  43.     if (pObjectClass)
  44.     {
  45.       if (WinEnumObjectClasses(pObjectClass, &cb))
  46.       {
  47.         pObjectClassNext = pObjectClass;
  48.  
  49.         while (pObjectClassNext && !bIsRegistered)
  50.         {
  51.           if (!strcmp(pObjectClassNext->pszClassName, "ShrAddressBook") ||
  52.               !strcmp(pObjectClassNext->pszClassName, "ShrPerson") ||
  53.               !strcmp(pObjectClassNext->pszClassName, "ShrFolder"))
  54.             bIsRegistered = TRUE;
  55.           else
  56.             pObjectClassNext = pObjectClassNext->pNext;
  57.         }
  58.  
  59.         free(pObjectClass);
  60.       }
  61.     }
  62.   }
  63.  
  64.   return bIsRegistered;
  65. }
  66.  
  67. extern BOOL ShrInstall(HWND hwndOwner, PSZ pszDLL)
  68. {
  69.   CHAR szText[256];
  70.   USHORT usResponse;
  71.   APIRET rc;
  72.   HMODULE hmod;
  73.   PSZ psz;
  74.  
  75.   sprintf(szText, "Register Address Book DLL '%s'?", pszDLL);
  76.   usResponse = WinMessageBox(HWND_DESKTOP, hwndOwner, szText,
  77.       vszMessageTitle, 0, MB_YESNO | MB_MOVEABLE | MB_ICONQUESTION);
  78.  
  79.   if (usResponse != MBID_YES)
  80.     return FALSE;
  81.  
  82.   rc = DosLoadModule(NULL, 0, pszDLL, &hmod);
  83.  
  84.   if (rc == ERROR_FILE_NOT_FOUND)
  85.   {
  86.     sprintf(szText, 
  87.         "Address Book DLL '%s' not found.", pszDLL);
  88.  
  89.     WinMessageBox(HWND_DESKTOP, hwndOwner, szText,
  90.        vszMessageTitle, 0, 
  91.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  92.     return FALSE;
  93.   }
  94.   else if (rc == ERROR_PATH_NOT_FOUND)
  95.   {
  96.     psz = strrchr(pszDLL, '\\');
  97.     *psz = '\0';
  98.  
  99.     sprintf(szText, "Path '%s' is not valid.", pszDLL);
  100.  
  101.     WinMessageBox(HWND_DESKTOP, hwndOwner, szText,
  102.        vszMessageTitle, 0, 
  103.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  104.     return FALSE;
  105.   }
  106.   else if (rc == ERROR_BAD_EXE_FORMAT)
  107.   {
  108.     sprintf(szText, 
  109.         "Address Book DLL '%s' is corrupted.  "
  110.         "Verify you downloaded it correctly and try again.",
  111.         pszDLL);
  112.  
  113.     WinMessageBox(HWND_DESKTOP, hwndOwner, szText,
  114.        vszMessageTitle, 0, 
  115.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  116.     return FALSE;
  117.   }
  118.   else if (rc != 0)
  119.   {
  120.     sprintf(szText, 
  121.         "DosLoadModule failed with a return code of %u.  "
  122.         "Type HELP SYS%u at an OS/2 command prompt for "
  123.         "a list of possible causes of the error.",
  124.         rc, rc);
  125.     WinMessageBox(HWND_DESKTOP, hwndOwner, szText,
  126.        vszMessageTitle, 0, 
  127.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  128.     return FALSE;
  129.   }
  130.  
  131.   if (!WinRegisterObjectClass("ShrPerson", pszDLL))
  132.   {
  133.     WinMessageBox(HWND_DESKTOP, hwndOwner,
  134.        "Unable to register Person class.", vszMessageTitle, 0,
  135.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  136.     return FALSE;
  137.   }
  138.  
  139.   if (!WinRegisterObjectClass("ShrFolder", pszDLL))
  140.   {
  141.     WinMessageBox(HWND_DESKTOP, hwndOwner,
  142.        "Unable to register ShrFolder class.",
  143.        vszMessageTitle, 0,
  144.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  145.     return FALSE;
  146.   }
  147.  
  148.   if (!WinRegisterObjectClass("ShrAddressBook", pszDLL))
  149.   {
  150.     WinMessageBox(HWND_DESKTOP, hwndOwner,
  151.        "Unable to register Address Book class.", 
  152.        vszMessageTitle, 0,
  153.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  154.     return FALSE;
  155.   }
  156.  
  157.   if (!WinCreateObject("ShrAddressBook", "Address Book", " ",
  158.       "<WP_DESKTOP>", CO_REPLACEIFEXISTS))
  159.   {
  160.     WinMessageBox(HWND_DESKTOP, hwndOwner,
  161.        "Unable to create Address Book.",
  162.        vszMessageTitle, 0,
  163.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  164.     return FALSE;
  165.   }
  166.   else
  167.   {
  168.     WinMessageBox(HWND_DESKTOP, hwndOwner,
  169.        "Address Book created.",
  170.        vszMessageTitle, 0,
  171.        MB_ENTER | MB_MOVEABLE);
  172.   }
  173.  
  174.   return TRUE;
  175. }
  176.  
  177. extern BOOL ShrUninstall(HWND hwndOwner)
  178. {
  179.   BOOL bSuccess;
  180.  
  181.   if (MBID_YES != WinMessageBox(HWND_DESKTOP, hwndOwner,
  182.        "About to uninstall.  Be certain that all instances of "
  183.        "the Address Book and Person objects are deleted before "
  184.        "choosing Yes to continue.",
  185.        vszMessageTitle, 0, 
  186.        MB_YESNO | MB_MOVEABLE | MB_WARNING | MB_DEFBUTTON2))
  187.     return FALSE;
  188.  
  189.   PrfWriteProfileData(HINI_USER, "ShrExample", NULL, NULL, 0);
  190.  
  191.   bSuccess = WinDeregisterObjectClass("ShrAddressBook");
  192.   bSuccess &= WinDeregisterObjectClass("ShrFolder");
  193.   bSuccess &= WinDeregisterObjectClass("ShrPerson");
  194.  
  195.   if (!bSuccess)
  196.   {
  197.     WinMessageBox(HWND_DESKTOP, hwndOwner,
  198.        "Unable to deregister Address Book and Person class.",
  199.        vszMessageTitle, 0,
  200.        MB_ENTER | MB_MOVEABLE | MB_ERROR);
  201.     return FALSE;
  202.   }
  203.  
  204.   WinMessageBox(HWND_DESKTOP, hwndOwner,
  205.      "Successfully uninstalled.",
  206.      vszMessageTitle, 0,
  207.      MB_ENTER | MB_MOVEABLE);
  208.  
  209.   return TRUE;
  210. }
  211.  
  212. extern MRESULT EXPENTRY ShrInstallDlgProc
  213.     (HWND hwndDlg, ULONG msg, MPARAM mp1, MPARAM mp2)
  214. {
  215.   MRESULT mr = NULL;
  216.   CHAR szDLL[CCHMAXPATH];
  217.   CHAR szText[256];
  218.   PSZ psz;
  219.   HWND hwndEntryField;
  220.  
  221.   switch (msg)
  222.   {
  223.     case WM_INITDLG:
  224.       hwndEntryField = WinWindowFromID(hwndDlg, ID_PATH);
  225.       WinSetWindowText(hwndEntryField, (PSZ) mp2);
  226.       mr = (MRESULT) WinSetFocus(HWND_DESKTOP, hwndEntryField);
  227.  
  228.       WinEnableControl(hwndDlg, ID_UNINSTALL, IsShareRegistered());
  229.       break;
  230.  
  231.     case WM_CLOSE:
  232.       WinDismissDlg(hwndDlg, MBID_CANCEL);
  233.       break;
  234.  
  235.     case WM_COMMAND:
  236.       if (SHORT1FROMMP(mp1) == MBID_OK)
  237.       {
  238.         WinQueryDlgItemText(hwndDlg, ID_PATH, sizeof(szDLL), szDLL);
  239.         if (strlen(szDLL) >= 2)
  240.         {
  241.           psz = strchr(szDLL, '\0') - 1;
  242.           if (*psz == '\\')
  243.             strcat(szDLL, "SHARE.DLL");
  244.           else
  245.             strcat(szDLL, "\\SHARE.DLL");
  246.  
  247.           strupr(szDLL);
  248.  
  249.           if (ShrInstall(hwndDlg, szDLL))
  250.             WinDismissDlg(hwndDlg, MBID_OK);
  251.         }
  252.         else
  253.         {
  254.           sprintf(szText, "The path must include the drive letter."
  255.               "  For example, %s.  Please try again.", vszSysDLLPath);
  256.           WinMessageBox(HWND_DESKTOP, HWND_DESKTOP, szText,
  257.               vszMessageTitle, 0, 
  258.               MB_ENTER | MB_MOVEABLE | MB_WARNING);
  259.         }
  260.       }
  261.       else if (SHORT1FROMMP(mp1) == MBID_CANCEL)
  262.       {
  263.         WinDismissDlg(hwndDlg, MBID_CANCEL);
  264.       }
  265.       else if (SHORT1FROMMP(mp1) == ID_UNINSTALL)
  266.       {
  267.         ShrUninstall(hwndDlg);
  268.         WinEnableControl(hwndDlg, ID_UNINSTALL, IsShareRegistered());
  269.       }
  270.       break;
  271.  
  272.     default:
  273.       mr = WinDefDlgProc(hwndDlg, msg, mp1, mp2);
  274.       break;
  275.   }
  276.   
  277.   return mr;
  278. }
  279.  
  280. extern int main(VOID);
  281.  
  282. int main()
  283. {
  284.   HMQ hmq;
  285.   HAB hab;
  286.   CHAR szPMWIN[CCHMAXPATH];
  287.   HMODULE hmodPMWIN;
  288.   PSZ psz;
  289.   HWND hwndDlg;
  290.   SWP swp;
  291.  
  292.   hab = WinInitialize(0);
  293.   hmq = WinCreateMsgQueue(hab, 0);
  294.  
  295.   if (!DosLoadModule(NULL, 0, "PMWIN", &hmodPMWIN))
  296.   {
  297.     DosQueryModuleName(hmodPMWIN, sizeof(szPMWIN), szPMWIN);
  298.     psz = strrchr(szPMWIN, '\\');
  299.     *psz = '\0';
  300.  
  301.     strcpy(vszSysDLLPath, szPMWIN);
  302.   }
  303.  
  304.   hwndDlg = WinLoadDlg(HWND_DESKTOP,
  305.       NULLHANDLE, ShrInstallDlgProc,
  306.       NULLHANDLE,
  307.       IDD_INSTALL, vszSysDLLPath);
  308.  
  309.   WinQueryWindowPos(hwndDlg, &swp);
  310.   swp.x = (WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN) - swp.cx) / 2;
  311.   swp.y = (WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN) - swp.cy) / 2;
  312.   swp.fl |= SWP_ACTIVATE | SWP_MOVE | SWP_ZORDER;
  313.   swp.hwndInsertBehind = HWND_TOP;
  314.  
  315.   WinSetMultWindowPos(NULLHANDLE, &swp, 1);
  316.  
  317.   WinProcessDlg(hwndDlg);
  318.  
  319.   WinDestroyMsgQueue(hmq);
  320.   WinTerminate(hab);
  321.  
  322.   return 0;
  323. }
  324.