1 /*############################################################################
2 # Copyright (C) Intel Corporation
4 # SPDX-License-Identifier: MIT
5 ############################################################################*/
7 #if !defined(MEDIASDK_UWP_DISPATCHER)
8 #include "windows/mfx_win_reg_key.h"
9 #include "windows/mfx_dispatcher_log.h"
11 #define TRACE_WINREG_ERROR(str, ...) DISPATCHER_LOG_ERROR((("[WINREG]: " str), __VA_ARGS__))
15 WinRegKey::WinRegKey(void) {
18 } // WinRegKey::WinRegKey(void)
20 WinRegKey::~WinRegKey(void) {
23 } // WinRegKey::~WinRegKey(void)
25 void WinRegKey::Release(void) {
26 // close the opened key
33 } // void WinRegKey::Release(void)
35 bool WinRegKey::Open(HKEY hRootKey, const wchar_t *pSubKey, REGSAM samDesired) {
40 // All operation are performed in this order by intention.
41 // It makes possible to reopen the keys, using itself as a base.
44 // try to the open registry key
45 lRes = RegOpenKeyExW(hRootKey, pSubKey, 0, samDesired, &hTemp);
46 if (ERROR_SUCCESS != lRes) {
47 DISPATCHER_LOG_OPERATION(SetLastError(lRes));
48 TRACE_WINREG_ERROR("Opening key \"%s\\%S\" : RegOpenKeyExW()==0x%x\n",
49 (HKEY_LOCAL_MACHINE == hRootKey) ? ("HKEY_LOCAL_MACHINE")
50 : (HKEY_CURRENT_USER == hRootKey) ? ("HKEY_CURRENT_USER")
57 // release the object before initialization
65 } // bool WinRegKey::Open(HKEY hRootKey, const wchar_t *pSubKey, REGSAM samDesired)
67 bool WinRegKey::Open(WinRegKey &rootKey, const wchar_t *pSubKey, REGSAM samDesired) {
68 return Open(rootKey.m_hKey, pSubKey, samDesired);
70 } // bool WinRegKey::Open(WinRegKey &rootKey, const wchar_t *pSubKey, REGSAM samDesired)
72 bool WinRegKey::QueryValueSize(const wchar_t *pValueName, DWORD type, LPDWORD pcbData) {
77 lRes = RegQueryValueExW(m_hKey, pValueName, NULL, &keyType, 0, pcbData);
78 if (ERROR_SUCCESS != lRes) {
79 DISPATCHER_LOG_OPERATION(SetLastError(lRes));
80 TRACE_WINREG_ERROR("Querying \"%S\" : RegQueryValueExA()==0x%x\n",
89 bool WinRegKey::Query(const wchar_t *pValueName, DWORD type, LPBYTE pData, LPDWORD pcbData) {
92 DWORD dstSize = (pcbData) ? (*pcbData) : (0);
95 lRes = RegQueryValueExW(m_hKey, pValueName, NULL, &keyType, pData, pcbData);
96 if (ERROR_SUCCESS != lRes) {
97 DISPATCHER_LOG_OPERATION(SetLastError(lRes));
98 TRACE_WINREG_ERROR("Querying \"%S\" : RegQueryValueExA()==0x%x\n",
105 if (keyType != type) {
106 TRACE_WINREG_ERROR("Querying \"%S\" : expectedType=%d, returned=%d\n",
113 // terminate the string only if pointers not NULL
114 if ((REG_SZ == type || REG_EXPAND_SZ == type) && NULL != pData && NULL != pcbData) {
115 wchar_t *pString = (wchar_t *)pData;
116 size_t NullEndingSizeBytes = sizeof(wchar_t); // size of string termination null character
117 if (dstSize < NullEndingSizeBytes) {
118 TRACE_WINREG_ERROR("Querying \"%S\" : buffer is too small for null-terminated string",
122 size_t maxStringLengthBytes = dstSize - NullEndingSizeBytes;
123 size_t maxStringIndex = dstSize / sizeof(wchar_t) - 1;
126 (maxStringLengthBytes < *pcbData) ? (maxStringIndex) : (*pcbData) / sizeof(wchar_t);
128 pString[lastIndex] = (wchar_t)0;
130 else if (REG_MULTI_SZ == type && NULL != pData && NULL != pcbData) {
131 wchar_t *pString = (wchar_t *)pData;
132 size_t NullEndingSizeBytes =
133 sizeof(wchar_t) * 2; // size of string termination null characters
134 if (dstSize < NullEndingSizeBytes) {
136 "Querying \"%S\" : buffer is too small for multi-line null-terminated string",
140 size_t maxStringLengthBytes = dstSize - NullEndingSizeBytes;
141 size_t maxStringIndex = dstSize / sizeof(wchar_t) - 1;
144 (maxStringLengthBytes < *pcbData) ? (maxStringIndex) : (*pcbData) / sizeof(wchar_t) + 1;
146 // last 2 bytes should be 0 in case of REG_MULTI_SZ
147 pString[lastIndex] = pString[lastIndex - 1] = (wchar_t)0;
152 } // bool WinRegKey::Query(const wchar_t *pValueName, DWORD type, LPBYTE pData, LPDWORD pcbData)
154 bool WinRegKey::EnumValue(DWORD index, wchar_t *pValueName, LPDWORD pcchValueName, LPDWORD pType) {
158 lRes = RegEnumValueW(m_hKey, index, pValueName, pcchValueName, 0, pType, NULL, NULL);
159 if (ERROR_SUCCESS != lRes) {
160 DISPATCHER_LOG_OPERATION(SetLastError(lRes));
166 } // bool WinRegKey::EnumValue(DWORD index, wchar_t *pValueName, LPDWORD pcchValueName, LPDWORD pType)
168 bool WinRegKey::EnumKey(DWORD index, wchar_t *pValueName, LPDWORD pcchValueName) {
172 lRes = RegEnumKeyExW(m_hKey, index, pValueName, pcchValueName, NULL, NULL, NULL, NULL);
173 if (ERROR_SUCCESS != lRes) {
174 DISPATCHER_LOG_OPERATION(SetLastError(lRes));
175 TRACE_WINREG_ERROR("EnumKey with index=%d: RegEnumKeyExW()==0x%x\n", index, GetLastError());
181 } // bool WinRegKey::EnumKey(DWORD index, wchar_t *pValueName, LPDWORD pcchValueName)
183 bool WinRegKey::QueryInfo(LPDWORD lpcSubkeys) {
186 lRes = RegQueryInfoKeyW(m_hKey, NULL, 0, 0, lpcSubkeys, 0, 0, 0, 0, 0, 0, 0);
187 if (ERROR_SUCCESS != lRes) {
188 TRACE_WINREG_ERROR("RegQueryInfoKeyW()==0x%x\n", lRes);
193 } //bool QueryInfo(LPDWORD lpcSubkeys);
197 #endif // #if !defined(MEDIASDK_UWP_DISPATCHER)