registry.h
Upload User: geng8029
Upload Date: 2021-01-30
Package Size: 187k
Code Size: 7k
Category:

Audio program

Development Platform:

Visual C++

  1. //////////////////////////////////////////////////////////////////////////
  2. //
  3. // Registry.h: Registry helpers.
  4. // 
  5. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  6. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  7. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  8. // PARTICULAR PURPOSE.
  9. //
  10. // Copyright (c) Microsoft Corporation. All rights reserved.
  11. //
  12. //////////////////////////////////////////////////////////////////////////
  13. #pragma once
  14. namespace MediaFoundationSamples
  15. {
  16.     #ifndef CHARS_IN_GUID
  17.     const DWORD CHARS_IN_GUID = 39;
  18.     #endif
  19.     // Forward declares
  20.     HRESULT RegisterObject(HMODULE hModule, const GUID& guid, const TCHAR *sDescription, const TCHAR *sThreadingModel);
  21.     HRESULT UnregisterObject(const GUID& guid);
  22.     HRESULT CreateObjectKeyName(const GUID& guid, TCHAR *sName, DWORD cchMax);
  23.     HRESULT SetKeyValue(HKEY hKey, const TCHAR *sName, const TCHAR *sValue);
  24.     ///////////////////////////////////////////////////////////////////////
  25.     // Name: RegisterObject
  26.     // Desc: Creates the registry entries for a COM object.
  27.     //
  28.     // guid: The object's CLSID
  29.     // sDescription: Description of the object
  30.     // sThreadingMode: Threading model. e.g., "Both"
  31.     ///////////////////////////////////////////////////////////////////////
  32.     inline HRESULT RegisterObject(HMODULE hModule, const GUID& guid, const TCHAR *sDescription, const TCHAR *sThreadingModel)
  33.     {
  34.         HKEY hKey = NULL;
  35.         HKEY hSubkey = NULL;
  36.         TCHAR achTemp[MAX_PATH];
  37.         // Create the name of the key from the object's CLSID
  38.         HRESULT hr = CreateObjectKeyName(guid, achTemp, MAX_PATH);
  39.         // Create the new key.
  40.         if (SUCCEEDED(hr))
  41.         {
  42.             LONG lreturn = RegCreateKeyEx(
  43.                 HKEY_CLASSES_ROOT,
  44.                 (LPCTSTR)achTemp,     // subkey
  45.                 0,                    // reserved
  46.                 NULL,                 // class string (can be NULL)
  47.                 REG_OPTION_NON_VOLATILE,
  48.                 KEY_ALL_ACCESS,
  49.                 NULL,                 // security attributes
  50.                 &hKey,
  51.                 NULL                  // receives the "disposition" (is it a new or existing key)
  52.                 );
  53.             hr = __HRESULT_FROM_WIN32(lreturn);
  54.         }
  55.         // The default key value is a description of the object.
  56.         if (SUCCEEDED(hr))
  57.         {
  58.             hr = SetKeyValue(hKey, NULL, sDescription);
  59.         }
  60.         // Create the "InprocServer32" subkey
  61.         if (SUCCEEDED(hr))
  62.         {
  63.             const TCHAR *sServer = TEXT("InprocServer32");
  64.             LONG lreturn = RegCreateKeyEx(hKey, sServer, 0, NULL,
  65.                 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hSubkey, NULL);
  66.             hr = __HRESULT_FROM_WIN32(lreturn);
  67.         }
  68.         // The default value for this subkey is the path to the DLL.
  69.         // Get the name of the module ...
  70.         if (SUCCEEDED(hr))
  71.         {
  72.             DWORD res = GetModuleFileName(hModule, achTemp, MAX_PATH);
  73.             if (res == 0)
  74.             {
  75.                 hr = __HRESULT_FROM_WIN32(GetLastError());
  76.             }
  77.             if (res == MAX_PATH)
  78.             {
  79.                 hr = E_FAIL; // buffer too small
  80.             }
  81.         }
  82.         // ... and set the default key value.
  83.         if (SUCCEEDED(hr))
  84.         {
  85.             hr = SetKeyValue(hSubkey, NULL, achTemp);
  86.         }
  87.         // Add a new value to the subkey, for "ThreadingModel" = <threading model>
  88.         if (SUCCEEDED(hr))
  89.         {
  90.             hr = SetKeyValue(hSubkey, TEXT("ThreadingModel"), sThreadingModel);
  91.         }
  92.         // close hkeys
  93.         if (hSubkey != NULL)
  94.         {
  95.             RegCloseKey( hSubkey );
  96.         }
  97.         if (hKey != NULL)
  98.         {
  99.             RegCloseKey( hKey );
  100.         }
  101.         return hr;
  102.     }
  103.     ///////////////////////////////////////////////////////////////////////
  104.     // Name: UnregisterObject
  105.     // Desc: Deletes the registry entries for a COM object.
  106.     //
  107.     // guid: The object's CLSID
  108.     ///////////////////////////////////////////////////////////////////////
  109.     inline HRESULT UnregisterObject(const GUID& guid)
  110.     {
  111.         TCHAR achTemp[MAX_PATH];
  112.         HRESULT hr = CreateObjectKeyName(guid, achTemp, MAX_PATH);
  113.         if (SUCCEEDED(hr))
  114.         {
  115.             // Delete the key recursively.
  116.             DWORD res = RegDeleteTree(HKEY_CLASSES_ROOT, achTemp);
  117.             if (res == ERROR_SUCCESS)
  118.             {
  119.                 hr = S_OK;
  120.             }
  121.             else
  122.             {
  123.                 hr = __HRESULT_FROM_WIN32(res);
  124.             }
  125.         }
  126.         return hr;
  127.     }
  128.     ///////////////////////////////////////////////////////////////////////
  129.     // Name: CreateObjectKeyName
  130.     // Desc: Converts a CLSID into a string with the form "CLSID{clsid}"
  131.     ///////////////////////////////////////////////////////////////////////
  132.     inline HRESULT CreateObjectKeyName(const GUID& guid, TCHAR *sName, DWORD cchMax)
  133.     {
  134.       // convert CLSID uuid to string 
  135.       OLECHAR szCLSID[CHARS_IN_GUID];
  136.       HRESULT hr = StringFromGUID2(guid, szCLSID, CHARS_IN_GUID);
  137.       if (FAILED(hr))
  138.       {
  139.           return hr;
  140.       }
  141.       // Create a string of the form "CLSID{clsid}"
  142.       return StringCchPrintf(sName, cchMax, TEXT("CLSID\%ls"), szCLSID);
  143.     }
  144.     ///////////////////////////////////////////////////////////////////////
  145.     // Name: SetKeyValue
  146.     // Desc: Sets a string value (REG_SZ) for a registry key
  147.     //
  148.     // hKey:   Handle to the registry key.
  149.     // sName:  Name of the value. Use NULL for the default value.
  150.     // sValue: The string value.
  151.     ///////////////////////////////////////////////////////////////////////
  152.     inline HRESULT SetKeyValue(HKEY hKey, const TCHAR *sName, const TCHAR *sValue)
  153.     {
  154.         size_t cch = 0;
  155.         HRESULT hr = StringCchLength(sValue, MAXLONG, &cch);
  156.         if (SUCCEEDED(hr))
  157.         {
  158.             // Size must include NULL terminator, which is not counted in StringCchLength
  159.             DWORD  cbData = ((DWORD)cch + 1) * sizeof(TCHAR);
  160.             // set description string
  161.             LONG ret = RegSetValueEx(hKey, sName, 0, REG_SZ, (BYTE*)sValue, cbData);
  162.             if (ret == ERROR_SUCCESS)
  163.             {
  164.                 hr = S_OK;
  165.             }
  166.             else
  167.             {
  168.                 hr = HRESULT_FROM_WIN32(ret);
  169.             }
  170.         }
  171.         return hr;
  172.     }
  173. }; // namespace MediaFoundationSamples