44#include < strsafe.h> // for StringCchPrintf()
55
66using namespace ATL ;
7- extern HRESULT GetHKCRRegistryKeyAndValue (PCWSTR pszSubKey, PCWSTR pszValueName, PWSTR pszData, DWORD cbData); // from Reg.cpp
7+
8+ //
9+ // FUNCTION: GetHKEYRegistryKeyAndValue
10+ //
11+ // PURPOSE: The function opens the given HKEY (HKCR, HKCU, etc) registry key and gets the data for the
12+ // specified registry value name.
13+ //
14+ // PARAMETERS:
15+ // * hRootKey - One of the reserved root key handles. For example:
16+ // HKEY_CLASSES_ROOT, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, HKEY_USERS or
17+ // * pszSubKey - specifies the registry key under HKCR. If the key does not
18+ // exist, the function returns an error.
19+ // * pszValueName - specifies the registry value to be retrieved. If
20+ // pszValueName is NULL, the function will get the default value.
21+ // * pszData - a pointer to a buffer that receives the value's string data.
22+ // * cbData - specifies the size of the buffer in bytes.
23+ //
24+ // RETURN VALUE:
25+ // If the function succeeds, it returns S_OK. Otherwise, it returns an
26+ // HRESULT error code. For example, if the specified registry key does not
27+ // exist or the data for the specified value name was not set, the function
28+ // returns COR_E_FILENOTFOUND (0x80070002).
29+ //
30+ HRESULT GetHKEYRegistryKeyAndValue (HKEY hRootKey, PCWSTR pszSubKey, PCWSTR pszValueName, PWSTR pszData, DWORD cbData)
31+ {
32+ HRESULT hr;
33+ HKEY hKey = NULL ;
34+
35+ // Try to open the specified registry key.
36+ hr = HRESULT_FROM_WIN32 (RegOpenKeyEx (hRootKey, pszSubKey, 0 ,
37+ KEY_READ, &hKey));
38+
39+ if (SUCCEEDED (hr))
40+ {
41+ // Get the data for the specified value name.
42+ hr = HRESULT_FROM_WIN32 (RegQueryValueEx (hKey, pszValueName, NULL ,
43+ NULL , reinterpret_cast <LPBYTE>(pszData), &cbData));
44+
45+ RegCloseKey (hKey);
46+ }
47+
48+ return hr;
49+ }
850
951HRESULT GetTypeLibGuid (_In_ HINSTANCE hInstTypeLib, _In_opt_z_ LPCOLESTR lpszIndex, GUID& guid)
1052{
@@ -53,7 +95,7 @@ HRESULT IsTypeLibRegisteredOnSystem(const GUID& guid, PCWSTR szVersion)
5395 return hr;
5496}
5597
56- HRESULT IsTypeLibRegisteredOnSystem ( _In_ LPTLIBATTR pTLibAttr)
98+ HRESULT IsTypeLibRegisteredUnderKey (HKEY hRootKey, _In_ const LPTLIBATTR pTLibAttr)
5799{
58100 if (NULL == pTLibAttr)
59101 return E_INVALIDARG;
@@ -84,15 +126,15 @@ HRESULT IsTypeLibRegisteredOnSystem(_In_ LPTLIBATTR pTLibAttr)
84126 return HRESULT_FROM_WIN32 (ERROR_INVALID_FLAGS);
85127 };
86128
87- // Check for the "HKCR \TypeLib\{guid}\{version}" key.
129+ // Check for the "HKEY_CLASSES_ROOT \TypeLib\{guid}\{version}" key.
88130 wchar_t szSubkey[MAX_PATH];
89131 hr = StringCchPrintf (szSubkey, ARRAYSIZE (szSubkey), L" TypeLib\\ %s\\ %s" , szGUID, szVersion);
90132 if (FAILED (hr))
91133 return hr;
92134
93135 // Get the TypeLib name
94136 wchar_t szTypeLibName[MAX_PATH];
95- hr = GetHKCRRegistryKeyAndValue ( szSubkey, nullptr , szTypeLibName, sizeof (szTypeLibName));
137+ hr = GetHKEYRegistryKeyAndValue (hRootKey, szSubkey, nullptr , szTypeLibName, sizeof (szTypeLibName));
96138 if (FAILED (hr))
97139 {
98140 if (hr == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND))
@@ -104,14 +146,14 @@ HRESULT IsTypeLibRegisteredOnSystem(_In_ LPTLIBATTR pTLibAttr)
104146 if (szTypeLibName[0 ] == L' \0 ' )
105147 return S_FALSE;
106148
107- // Check for the "HKCR \TypeLib\{guid}\{version}\0\{syskind}" key.
149+ // Check for the "HKEY_CLASSES_ROOT \TypeLib\{guid}\{version}\0\{syskind}" key.
108150 hr = StringCchPrintf (szSubkey, ARRAYSIZE (szSubkey), L" TypeLib\\ %s\\ %s\\ 0\\ %s" , szGUID, szVersion, szSysKind);
109151 if (FAILED (hr))
110152 return hr;
111153
112154 // Get the TypeLib dll path
113155 wchar_t szDllPath[MAX_PATH];
114- hr = GetHKCRRegistryKeyAndValue ( szSubkey, nullptr , szDllPath, sizeof (szDllPath));
156+ hr = GetHKEYRegistryKeyAndValue (hRootKey, szSubkey, nullptr , szDllPath, sizeof (szDllPath));
115157 if (FAILED (hr))
116158 {
117159 if (hr == HRESULT_FROM_WIN32 (ERROR_FILE_NOT_FOUND))
@@ -138,4 +180,16 @@ HRESULT IsTypeLibRegisteredOnSystem(_In_ LPTLIBATTR pTLibAttr)
138180 // return S_FALSE;
139181
140182 return S_OK;
141- }
183+ }
184+
185+ HRESULT IsTypeLibRegisteredOnSystem (_In_ const LPTLIBATTR pTLibAttr)
186+ {
187+ HRESULT hr = IsTypeLibRegisteredUnderKey (HKEY_CLASSES_ROOT, pTLibAttr);
188+ return hr;
189+ }
190+
191+ HRESULT IsTypeLibRegisteredForCurrentUser (_In_ const LPTLIBATTR pTLibAttr)
192+ {
193+ HRESULT hr = IsTypeLibRegisteredUnderKey (HKEY_CURRENT_USER, pTLibAttr);
194+ return hr;
195+ }
0 commit comments