home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / com / inole2 / chap18 / cosmo1.0 / oleinst.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  7KB  |  260 lines

  1. /*
  2.  * OLEINST.C
  3.  *
  4.  * OLE-Specific installation-time functions that should be performed
  5.  * once when the application is first added to the system.
  6.  *
  7.  * Note that there are embedded strings in this file.  We do not use
  8.  * strings from the application wide source in rgpsz since we want this
  9.  * module to be reusable in installation programs.
  10.  *
  11.  * Copyright(c) Microsoft Corp. 1992-1994 All Rights Reserved
  12.  * Win32 version, January 1994
  13.  */
  14.  
  15.  
  16. #ifdef MAKEOLESERVER
  17.  
  18. #include <windows.h>
  19. #include "cosmo.h"
  20. #include "oleinst.h"
  21.  
  22.  
  23. /*
  24.  * FRegDBInstall
  25.  *
  26.  * Purpose:
  27.  *  Handles any installations the applciation must handle for the
  28.  *  registration database.  In this application, we just set up to
  29.  *  call FOLEInstall that handles the OLE specific strings.
  30.  *
  31.  * Parameters:
  32.  *  None
  33.  *
  34.  * Return Value:
  35.  *  BOOL            TRUE if installation succeeded, FALSE otherwise.
  36.  *
  37.  */
  38.  
  39. BOOL FAR PASCAL FRegDBInstall(void)
  40.     {
  41.     REGINSTALL  ri;
  42.     HMODULE     hMod;
  43.     LPSTR       rgszVerbs[OBJVERB_MAX];
  44.     char        szPath[CCHPATHMAX];
  45.  
  46.     /*
  47.      * Fill the REGINSTALL structure for calling FOLEServerInstall.
  48.      * Must do this after the strings are loaded since we use strings
  49.      * from the stringtable.
  50.      */
  51.     ri.pszServerName    =(LPSTR)rgpsz[IDS_FULLNAME];
  52.     ri.pszServerClass   =(LPSTR)rgpsz[IDS_CLASSCOSMO];
  53.     ri.pszHandlerPath   =(LPSTR)NULL;
  54.     ri.pszExt           =(LPSTR)rgpsz[IDS_DOTEXT];
  55.     ri.pszSetFormats    =(LPSTR)rgpsz[IDS_NATIVE];
  56.     ri.pszRequestFormats=(LPSTR)rgpsz[IDS_DATAFORMATS];
  57.     ri.fExecute         =FALSE;
  58.  
  59.     //Get the module path
  60.     hMod=GetModuleHandle(rgpsz[IDS_MODULE]);
  61.  
  62.     if (NULL==hMod)
  63.         return FALSE;
  64.  
  65.     GetModuleFileName(hMod, szPath, CCHPATHMAX);
  66.  
  67.     ri.pszServerPath    =(LPSTR)szPath;
  68.  
  69.     //Fill the array of pointers to verbs.
  70.     rgszVerbs[OBJVERB_EDIT]=(LPSTR)rgpsz[IDS_VERBEDIT];
  71.  
  72.     ri.ppszVerbs        =rgszVerbs;
  73.     ri.cVerbs           =OBJVERB_MAX;
  74.  
  75.     return FOLEServerInstall(&ri);
  76.     }
  77.  
  78.  
  79.  
  80.  
  81.  
  82. /*
  83.  * FOLEServerInstall
  84.  *
  85.  * Purpose:
  86.  *  Registers the server application and it's editable data types
  87.  *  with the registration database through the SHELL.DLL functions.
  88.  *
  89.  *  This function will register the server in the database if it
  90.  *  is not there already.
  91.  *
  92.  * Limitations:
  93.  *  FOLEServerInstall supports only a single class of data.
  94.  *
  95.  * Parameters:
  96.  *  pszServer       LPSTR name of the server.
  97.  *  pszData         LPSTR name of the data edited by the server.
  98.  *
  99.  * Return Value:
  100.  *  BOOL            TRUE if registration took place or the application
  101.  *                  was already registered, FALSE otherwise.
  102.  */
  103.  
  104.  
  105. BOOL FAR PASCAL FOLEServerInstall(LPREGINSTALL lpRI)
  106.     {
  107.     char        szKey[128];
  108.     char        szTemp[20];
  109.     DWORD       dw=128;
  110.     LONG        lRet;
  111.     UINT        iVerb;
  112.     LPSTR       *ppsz;
  113.  
  114.     //Check if this server is already around.
  115.     lRet=RegQueryValue(HKEY_CLASSES_ROOT, lpRI->pszServerClass, szKey, &dw);
  116.  
  117.     if ((LONG)ERROR_SUCCESS==lRet)
  118.         return TRUE;
  119.  
  120.  
  121.     /*
  122.      * Add various strings to the registration database:
  123.      *  1.  English-readable class name.
  124.      *  2.  Simple class name.
  125.      *  3.  Full path to the server executable.
  126.      *  4.  Full path to the handler DLL, if available.
  127.      *  5.  Verbs
  128.      *  6.  Any formats supported through OleSetData and OleRequestData.
  129.      *  7.  Full application path if the server supports StdExecute.
  130.      */
  131.  
  132.     //Validate essential strings
  133.     if (NULL==lpRI->pszServerClass)
  134.         return FALSE;
  135.  
  136.     if (NULL==lpRI->pszServerName)
  137.         return FALSE;
  138.  
  139.     if (NULL==lpRI->pszServerPath)
  140.         return FALSE;
  141.  
  142.     if (NULL==lpRI->pszExt)
  143.         return FALSE;
  144.  
  145.     if (NULL==lpRI->ppszVerbs || 0==lpRI->cVerbs)
  146.         return FALSE;
  147.  
  148.  
  149.     //1. English class name.  Subkey is ""
  150.     FKeyCreate(lpRI->pszServerClass, rgpsz[IDS_EMPTY], lpRI->pszServerName);
  151.  
  152.  
  153.     //2. Simple class name associated with the file extension.  Subkey is ""
  154.     FKeyCreate(lpRI->pszExt, rgpsz[IDS_EMPTY], lpRI->pszServerClass);
  155.  
  156.  
  157.     /*
  158.      * The server path, handler path, and verbs are all part of
  159.      * the \\classname\\protocol\\StdFileEditing\\ key.  Before
  160.      * creating any of the keys, create a string with the class
  161.      * name embedded.
  162.      */
  163.     wsprintf(szKey, "%s\\protocol\\StdFileEditing\\", lpRI->pszServerClass);
  164.  
  165.  
  166.     //3. Server path, append "server" onto szKey.
  167.     FKeyCreate(szKey, "server", lpRI->pszServerPath);
  168.  
  169.     //4. Add the handler path, if one exists, appening "handler" on szKey.
  170.     if (NULL!=lpRI->pszHandlerPath)
  171.         FKeyCreate(szKey, "handler", lpRI->pszHandlerPath);
  172.  
  173.     //5.  Add each verb.  Append to szKey "verb\\n" where n is a simple count.
  174.     ppsz=lpRI->ppszVerbs;
  175.  
  176.     for (iVerb=0; iVerb < lpRI->cVerbs; iVerb++)
  177.         {
  178.         //Create key and skip to next verb.
  179.         wsprintf(szTemp, "verb\\%d", iVerb);
  180.         FKeyCreate(szKey, szTemp, *ppsz++);
  181.         }
  182.  
  183.  
  184.     //6. Add the strings for formats, to "SetDataFormats" and "RequestDataFormats"
  185.     if (NULL!=lpRI->pszSetFormats)
  186.         FKeyCreate(szKey, "SetDataFormats", lpRI->pszSetFormats);
  187.  
  188.     if (NULL!=lpRI->pszSetFormats)
  189.         FKeyCreate(szKey, "RequestDataFormats", lpRI->pszRequestFormats);
  190.  
  191.  
  192.     //7. Add the StdExecute string, appending the server path.
  193.     if (lpRI->fExecute)
  194.         {
  195.         wsprintf(szKey, "%s\\protocol\\StdExecute\\", lpRI->pszServerClass);
  196.         FKeyCreate(szKey, "server", lpRI->pszServerPath);
  197.         }
  198.  
  199.     return TRUE;
  200.     }
  201.  
  202.  
  203.  
  204. /*
  205.  * FKeyCreate
  206.  *
  207.  * Purpose:
  208.  *  Short function to simplify the key creation process, centralizing
  209.  *  calls to RegCreateKey, RegSetValue, and RegCloseKey.  If the RegSetValue
  210.  *  fails, the key is deleted with RegDeleteKey.
  211.  *
  212.  * Parameters:
  213.  *  pszKey          LPSTR to keyname.
  214.  *  pszSubKey       LPSTR to last word in keyname.  This parameter is
  215.  *                  separate to enable calling RegDeleteKey on error.
  216.  *  pszValue        LPSTR to value to associate with keyname.
  217.  *
  218.  * Return Value:
  219.  *  BOOL            TRUE if registration succeeded, FALSE otherwise.
  220.  */
  221.  
  222. BOOL PASCAL FKeyCreate(LPSTR pszKey, LPSTR pszSubKey, LPSTR pszValue)
  223.     {
  224.     char        szKey[128];
  225.     HKEY        hKey;
  226.     UINT        cch;
  227.     LONG        lRet;
  228.  
  229.     cch=lstrlen(pszValue)+1;
  230.  
  231.     lstrcpy(szKey, pszKey);
  232.     lstrcat(szKey, pszSubKey);
  233.  
  234.     lRet=RegCreateKey(HKEY_CLASSES_ROOT, szKey, &hKey);
  235.  
  236.     if (lRet!=ERROR_SUCCESS)
  237.         return FALSE;
  238.  
  239.  
  240.     lRet=RegSetValue(HKEY_CLASSES_ROOT, szKey, REG_SZ, pszValue, cch);
  241.  
  242.     if (lRet!=ERROR_SUCCESS)
  243.         {
  244.         //Delete key if we could not set a value.
  245.         RegDeleteKey(hKey, pszSubKey);
  246.         return FALSE;
  247.         }
  248.  
  249.     lRet=RegCloseKey(hKey);
  250.  
  251.     if (lRet!=ERROR_SUCCESS)
  252.         return FALSE;
  253.  
  254.     return TRUE;
  255.     }
  256.  
  257.  
  258.  
  259. #endif   //MAKEOLESERVER
  260.