home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples1.exe / Profiler / RegUtil.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  9.6 KB  |  420 lines

  1. //*****************************************************************************
  2. // RegUtil.cpp
  3. //
  4. // This module contains a set of functions that can be used to access the
  5. // regsitry.
  6. //
  7. // Copyright (c) Microsoft Corp., 1996, All rights reserved.
  8. //*****************************************************************************
  9. #include "RegUtil.h"
  10.  
  11.  
  12. /***************************************************************************************
  13.  *    Method:
  14.  *
  15.  *
  16.  *    Purpose:
  17.  *
  18.  *
  19.  *    Parameters: 
  20.  *
  21.  *
  22.  *    Return value:
  23.  *
  24.  *
  25.  *    Notes:
  26.  *    Set an entry in the registry of the form:
  27.  *                    HKEY_CLASSES_ROOT\szKey\szSubkey = szValue
  28.  *    If szSubkey or szValue are NULL, omit them from the above expression.
  29.  ***************************************************************************************/
  30. BOOL REGUTIL::SetKeyAndValue( const char *szKey,
  31.                               const char *szSubkey,
  32.                               const char *szValue )
  33. {
  34.     HKEY hKey;                  // handle to the new reg key.
  35.     char rcKey[MAX_LENGTH]; // buffer for the full key name.
  36.  
  37.  
  38.     // init the key with the base key name.
  39.     strcpy( rcKey, szKey );
  40.  
  41.     // append the subkey name (if there is one).
  42.     if ( szSubkey != NULL )
  43.     {
  44.         strcat( rcKey, "\\" );
  45.         strcat( rcKey, szSubkey );
  46.     }
  47.  
  48.     // create the registration key.
  49.     if (RegCreateKeyExA( HKEY_CLASSES_ROOT, 
  50.                          rcKey, 
  51.                          0, 
  52.                          NULL,
  53.                          REG_OPTION_NON_VOLATILE, 
  54.                          KEY_ALL_ACCESS, 
  55.                          NULL,
  56.                          &hKey, 
  57.                          NULL ) == ERROR_SUCCESS )
  58.     {
  59.         // set the value (if there is one).
  60.         if ( szValue != NULL )
  61.         {
  62.             RegSetValueExA( hKey, 
  63.                             NULL, 
  64.                             0, 
  65.                             REG_SZ, 
  66.                             (BYTE *) szValue,
  67.                             ( strlen( szValue ) + 1 ) * sizeof( char ) );
  68.         }
  69.         
  70.         RegCloseKey( hKey );
  71.     
  72.         
  73.         return TRUE;
  74.     }    
  75.  
  76.  
  77.     return FALSE;    
  78.  
  79. } // REGUTIL::SetKeyAndValue
  80.  
  81.  
  82. /***************************************************************************************
  83.  *    Method:
  84.  *
  85.  *
  86.  *    Purpose:
  87.  *
  88.  *
  89.  *    Parameters: 
  90.  *
  91.  *
  92.  *    Return value:
  93.  *
  94.  *
  95.  *    Notes:
  96.  *    Delete an entry in the registry of the form:
  97.  *                    HKEY_CLASSES_ROOT\szKey\szSubkey = szValue
  98.  ***************************************************************************************/
  99. BOOL REGUTIL::DeleteKey( const char *szKey,
  100.                           const char *szSubkey )
  101. {
  102.     char rcKey[MAX_LENGTH]; // buffer for the full key name.
  103.  
  104.  
  105.     // init the key with the base key name.
  106.     strcpy( rcKey, szKey );
  107.  
  108.     // append the subkey name (if there is one).
  109.     if ( szSubkey != NULL )
  110.     {
  111.         strcat( rcKey, "\\" );
  112.         strcat( rcKey, szSubkey );
  113.     }
  114.  
  115.     // delete the registration key.
  116.     RegDeleteKeyA( HKEY_CLASSES_ROOT, rcKey );
  117.     
  118.     
  119.     return TRUE;
  120.  
  121. } // REGUTIL::DeleteKey
  122.  
  123.  
  124. /***************************************************************************************
  125.  *    Method:
  126.  *
  127.  *
  128.  *    Purpose:
  129.  *    Open the key, create a new keyword and value pair under it.
  130.  *
  131.  *    Parameters: 
  132.  *
  133.  *
  134.  *    Return value:
  135.  *
  136.  *
  137.  *    Notes:
  138.  *                   
  139.  *
  140.  ***************************************************************************************/
  141. BOOL REGUTIL::SetRegValue( const char *szKeyName,
  142.                            const char *szKeyword,
  143.                            const char *szValue )
  144. {
  145.     HKEY hKey; // handle to the new reg key.
  146.  
  147.     // create the registration key.
  148.     if ( RegCreateKeyExA( HKEY_CLASSES_ROOT, 
  149.                           szKeyName, 
  150.                           0, 
  151.                           NULL,
  152.                           REG_OPTION_NON_VOLATILE, 
  153.                           KEY_ALL_ACCESS, 
  154.                           NULL,
  155.                           &hKey, 
  156.                           NULL) == ERROR_SUCCESS )
  157.     {
  158.         // set the value (if there is one).
  159.         if ( szValue != NULL )
  160.         {
  161.             RegSetValueExA( hKey, 
  162.                             szKeyword, 
  163.                             0, 
  164.                             REG_SZ, 
  165.                             (BYTE *)szValue, 
  166.                             ( strlen( szValue ) + 1 ) * sizeof( char ) );
  167.         }
  168.  
  169.         RegCloseKey( hKey );
  170.  
  171.         
  172.         return TRUE;
  173.     }
  174.  
  175.     
  176.     return FALSE;
  177.  
  178. } // REGUTIL::SetRegValue
  179.  
  180.  
  181. /***************************************************************************************
  182.  *    Method:
  183.  *
  184.  *
  185.  *    Purpose:
  186.  *    Does standard registration of a CoClass with a progid.
  187.  *
  188.  *    Parameters: 
  189.  *
  190.  *
  191.  *    Return value:
  192.  *
  193.  *
  194.  *    Notes:
  195.  *                   
  196.  *
  197.  ***************************************************************************************/
  198. HRESULT REGUTIL::RegisterCOMClass( REFCLSID    rclsid,
  199.                                    const char *szDesc,                    
  200.                                    const char *szProgIDPrefix,    
  201.                                    int    iVersion,                
  202.                                    const char *szClassProgID,    
  203.                                    const char *szThreadingModel,
  204.                                    const char *szModule ) 
  205. {
  206.     HRESULT    hr;
  207.     char rcCLSID[MAX_LENGTH];            // CLSID\\szID.
  208.     char rcProgID[MAX_LENGTH];            // szProgIDPrefix.szClassProgID
  209.     char rcIndProgID[MAX_LENGTH];        // rcProgID.iVersion
  210.     char rcInproc[MAX_LENGTH + 2];         // CLSID\\InprocServer32
  211.  
  212.  
  213.     // format the prog ID values.
  214.     sprintf( rcIndProgID, "%s.%s", szProgIDPrefix, szClassProgID ) ;
  215.     sprintf( rcProgID, "%s.%d", rcIndProgID, iVersion );
  216.  
  217.     // do the initial portion.
  218.     hr =  RegisterClassBase( rclsid, 
  219.                              szDesc, 
  220.                              rcProgID, 
  221.                              rcIndProgID, 
  222.                              rcCLSID );
  223.     if ( SUCCEEDED( hr ) )
  224.     {
  225.         // set the server path.
  226.         SetKeyAndValue( rcCLSID, "InprocServer32", szModule );
  227.  
  228.         // add the threading model information.
  229.         sprintf( rcInproc, "%s\\%s", rcCLSID, "InprocServer32" );
  230.         SetRegValue( rcInproc, "ThreadingModel", szThreadingModel );
  231.     }    
  232.     
  233.  
  234.     return hr;
  235.  
  236. } // REGUTIL::RegisterCOMClass
  237.  
  238.  
  239. /***************************************************************************************
  240.  *    Method:
  241.  *
  242.  *
  243.  *    Purpose:
  244.  *    Register the basics for a in proc server.
  245.  *
  246.  *    Parameters: 
  247.  *
  248.  *
  249.  *    Return value:
  250.  *
  251.  *
  252.  *    Notes:
  253.  *                   
  254.  *
  255.  ***************************************************************************************/
  256. HRESULT REGUTIL::RegisterClassBase( REFCLSID rclsid,
  257.                                     const char *szDesc,                    
  258.                                     const char *szProgID,                
  259.                                     const char *szIndepProgID,            
  260.                                     char *szOutCLSID )                
  261. {
  262.     // create some base key strings.
  263.  
  264.     char szID[64];        // the class ID to register.
  265.     OLECHAR    szWID[64]; // helper for the class ID to register.
  266.  
  267.  
  268.     StringFromGUID2( rclsid, szWID, NumItems( szWID ) );
  269.     WideCharToMultiByte( CP_ACP, 
  270.                          0, 
  271.                          szWID, 
  272.                          -1, 
  273.                          szID, 
  274.                          sizeof( szID ), 
  275.                          NULL, 
  276.                          NULL );
  277.  
  278.     strcpy( szOutCLSID, "CLSID\\" );
  279.     strcat( szOutCLSID, szID );
  280.  
  281.     // create ProgID keys.
  282.     SetKeyAndValue( szProgID, NULL, szDesc );
  283.     SetKeyAndValue( szProgID, "CLSID", szID );
  284.  
  285.     // create VersionIndependentProgID keys.
  286.     SetKeyAndValue( szIndepProgID, NULL, szDesc );
  287.     SetKeyAndValue( szIndepProgID, "CurVer", szProgID );
  288.     SetKeyAndValue( szIndepProgID, "CLSID", szID );
  289.  
  290.     // create entries under CLSID.
  291.     SetKeyAndValue( szOutCLSID, NULL, szDesc );
  292.     SetKeyAndValue( szOutCLSID, "ProgID", szProgID );
  293.     SetKeyAndValue( szOutCLSID, "VersionIndependentProgID", szIndepProgID );
  294.     SetKeyAndValue( szOutCLSID, "NotInsertable", NULL );
  295.     
  296.     
  297.     return S_OK;
  298.  
  299. } // REGUTIL::RegisterClassBase
  300.  
  301.  
  302. /***************************************************************************************
  303.  *    Method:
  304.  *
  305.  *
  306.  *    Purpose:
  307.  *    Unregister the basic information in the system registry for a given object
  308.  *    class
  309.  *
  310.  *    Parameters: 
  311.  *
  312.  *
  313.  *    Return value:
  314.  *
  315.  *
  316.  *    Notes:
  317.  *                   
  318.  *
  319.  ***************************************************************************************/
  320. HRESULT REGUTIL::UnregisterCOMClass( REFCLSID rclsid,           
  321.                                      const char *szProgIDPrefix,
  322.                                      int iVersion,              
  323.                                      const char *szClassProgID )
  324. {
  325.     char szID[64];           // the class ID to unregister.
  326.     char rcCLSID[64];       // CLSID\\szID.
  327.     OLECHAR    szWID[64];       // helper for the class ID to unregister.
  328.     char rcProgID[128];       // szProgIDPrefix.szClassProgID
  329.     char rcIndProgID[128]; // rcProgID.iVersion
  330.  
  331.  
  332.     // format the prog ID values.
  333.     sprintf( rcProgID, "%s.%s", szProgIDPrefix, szClassProgID );
  334.     sprintf( rcIndProgID, "%s.%d", rcProgID, iVersion );
  335.  
  336.     UnregisterClassBase( rclsid, rcProgID, rcIndProgID, rcCLSID );
  337.     DeleteKey( rcCLSID, "InprocServer32" );
  338.  
  339.     StringFromGUID2(rclsid, szWID, NumItems( szWID ) );
  340.     WideCharToMultiByte( CP_ACP, 
  341.                          0, 
  342.                          szWID, 
  343.                          -1, 
  344.                          szID, 
  345.                          sizeof( szID ), 
  346.                          NULL, 
  347.                          NULL );
  348.  
  349.     DeleteKey( "CLSID", rcCLSID );
  350.     
  351.     
  352.     return S_OK;
  353.  
  354. } // REGUTIL::UnregisterCOMClass
  355.  
  356.  
  357. /***************************************************************************************
  358.  *    Method:
  359.  *
  360.  *
  361.  *    Purpose:
  362.  *    Delete the basic settings for an inproc server.
  363.  *
  364.  *    Parameters: 
  365.  *
  366.  *
  367.  *    Return value:
  368.  *
  369.  *
  370.  *    Notes:
  371.  *                   
  372.  *
  373.  ***************************************************************************************/
  374. HRESULT REGUTIL::UnregisterClassBase( REFCLSID rclsid,
  375.                                       const char *szProgID,
  376.                                       const char *szIndepProgID,
  377.                                       char *szOutCLSID )
  378. {
  379.     char szID[64];        // the class ID to register.
  380.     OLECHAR    szWID[64]; // helper for the class ID to register.
  381.  
  382.  
  383.     StringFromGUID2( rclsid, szWID, NumItems( szWID ) );
  384.     WideCharToMultiByte( CP_ACP, 
  385.                          0, 
  386.                          szWID, 
  387.                          -1, 
  388.                          szID, 
  389.                          sizeof( szID ), 
  390.                          NULL, 
  391.                          NULL );
  392.  
  393.     strcpy( szOutCLSID, "CLSID\\" );
  394.     strcat( szOutCLSID, szID );
  395.  
  396.     // delete the version independant prog ID settings.
  397.     DeleteKey( szIndepProgID, "CurVer" );
  398.     DeleteKey( szIndepProgID, "CLSID" );
  399.     RegDeleteKeyA( HKEY_CLASSES_ROOT, szIndepProgID );
  400.  
  401.  
  402.     // delete the prog ID settings.
  403.     DeleteKey( szProgID, "CLSID" );
  404.     RegDeleteKeyA( HKEY_CLASSES_ROOT, szProgID );
  405.  
  406.  
  407.     // delete the class ID settings.
  408.     DeleteKey( szOutCLSID, "ProgID" );
  409.     DeleteKey( szOutCLSID, "VersionIndependentProgID" );
  410.     DeleteKey( szOutCLSID, "NotInsertable" );
  411.     RegDeleteKeyA( HKEY_CLASSES_ROOT, szOutCLSID );
  412.     
  413.     
  414.     return S_OK;
  415.  
  416. } // REGUTIL::UnregisterClassBase
  417.  
  418.  
  419. // End of File
  420.