xref: /reactos/sdk/lib/cicero/cicreg.cpp (revision 3a49e26f)
1 /*
2  * PROJECT:     ReactOS Cicero
3  * LICENSE:     LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4  * PURPOSE:     Cicero registry handling
5  * COPYRIGHT:   Copyright 2023 Katayama Hirofumi MZ <katayama.hirofumi.mz@gmail.com>
6  */
7 
8 #include "precomp.h"
9 #include <cicreg.h>
10 
11 EXTERN_C LSTATUS
12 _cicRegKey_Open(CicRegKey& self, HKEY hKey, LPCTSTR lpSubKey, REGSAM samDesired)
13 {
14     HKEY hNewKey;
15     LSTATUS error = ::RegOpenKeyEx(hKey, lpSubKey, 0, samDesired, &hNewKey);
16     if (error != ERROR_SUCCESS)
17         return error;
18 
19     self.Close();
20     self.m_hKey = hNewKey;
21     return error;
22 }
23 
24 EXTERN_C LSTATUS
25 _cicRegKey_Create(CicRegKey& self, HKEY hKey, LPCTSTR lpSubKey)
26 {
27     HKEY hNewKey;
28     LSTATUS error = ::RegCreateKeyEx(hKey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE,
29                                      KEY_ALL_ACCESS, NULL, &hNewKey, NULL);
30     if (error != ERROR_SUCCESS)
31         return error;
32 
33     self.Close();
34     self.m_hKey = hNewKey;
35     return error;
36 }
37 
38 EXTERN_C LSTATUS
39 _cicRegKey_EnumValue(CicRegKey& self, DWORD dwIndex, LPTSTR lpValueName, DWORD cchValueName)
40 {
41     DWORD dwSaveLen = cchValueName;
42     LSTATUS error = ::RegEnumValue(self.m_hKey, dwIndex, lpValueName, &cchValueName,
43                                    NULL, NULL, NULL, NULL);
44     if (dwSaveLen)
45         lpValueName[error == ERROR_SUCCESS ? dwSaveLen - 1 : 0] = 0;
46     return error;
47 }
48 
49 EXTERN_C LSTATUS
50 _cicRegKey_QuerySz(CicRegKey& self, LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValueMax)
51 {
52     DWORD cbValueMax = cchValueMax * sizeof(TCHAR);
53     LSTATUS error = ::RegQueryValueEx(self.m_hKey, pszValueName, 0, NULL, (LPBYTE)pszValue, &cbValueMax);
54     if (cchValueMax > 0)
55         pszValue[(error == ERROR_SUCCESS) ? (cchValueMax - 1) : 0] = UNICODE_NULL;
56     return error;
57 }
58 
59 EXTERN_C LSTATUS
60 _cicRegKey_RecurseDeleteKey(CicRegKey& self, LPCTSTR lpSubKey)
61 {
62     CicRegKey regKey;
63     LSTATUS error = regKey.Open(self.m_hKey, lpSubKey, KEY_READ | KEY_WRITE);
64     if (error != ERROR_SUCCESS)
65         return error;
66 
67     TCHAR szName[MAX_PATH];
68     DWORD cchName;
69     do
70     {
71         cchName = _countof(szName);
72         error = ::RegEnumKeyEx(regKey, 0, szName, &cchName, NULL, NULL, NULL, NULL);
73         if (error != ERROR_SUCCESS)
74             break;
75 
76         szName[_countof(szName) - 1] = UNICODE_NULL;
77         error = regKey.RecurseDeleteKey(szName);
78     } while (error == ERROR_SUCCESS);
79 
80     regKey.Close();
81 
82     return self.DeleteSubKey(lpSubKey);
83 }
84