xref: /reactos/sdk/lib/cicero/cicreg.h (revision dad056e0)
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 #pragma once
9 
10 class CicRegKey
11 {
12 public:
13     HKEY m_hKey;
14 
15     CicRegKey() : m_hKey(NULL) { }
16     ~CicRegKey() { Close(); }
17 
18     operator HKEY() { return m_hKey; }
19 
20     LSTATUS Open(HKEY hKey, LPCTSTR lpSubKey, REGSAM samDesired = KEY_READ);
21     LSTATUS Create(HKEY hKey, LPCTSTR lpSubKey);
22     void Close();
23 
24     LSTATUS QueryDword(LPCTSTR pszValueName, LPDWORD pdwValue);
25     LSTATUS SetDword(LPCTSTR pszValueName, DWORD dwValue);
26 
27     LSTATUS QuerySz(LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValueMax);
28     LSTATUS SetSz(LPCTSTR pszValueName, LPCTSTR pszValue);
29     LSTATUS SetSzW(LPCWSTR pszValueName, LPCWSTR pszValue);
30 
31     LSTATUS DeleteValue(LPCTSTR pszValueName);
32     LSTATUS DeleteSubKey(LPCTSTR lpSubKey);
33     LSTATUS RecurseDeleteKey(LPCTSTR lpSubKey);
34 
35     LSTATUS EnumValue(DWORD dwIndex, LPTSTR lpValueName, DWORD cchValueName);
36 };
37 
38 /***********************************************************************/
39 
40 // FIXME: Here, directly using C++ methods causes compile errors... Why?
41 EXTERN_C LSTATUS _cicRegKey_Open(CicRegKey& self, HKEY hKey, LPCTSTR lpSubKey, REGSAM samDesired);
42 EXTERN_C LSTATUS _cicRegKey_Create(CicRegKey& self, HKEY hKey, LPCTSTR lpSubKey);
43 EXTERN_C LSTATUS _cicRegKey_RecurseDeleteKey(CicRegKey& self, LPCTSTR lpSubKey);
44 
45 EXTERN_C LSTATUS
46 _cicRegKey_EnumValue(CicRegKey& self, DWORD dwIndex, LPTSTR lpValueName, DWORD cchValueName);
47 
48 EXTERN_C LSTATUS
49 _cicRegKey_QuerySz(CicRegKey& self, LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValueMax);
50 
51 inline LSTATUS CicRegKey::Open(HKEY hKey, LPCTSTR lpSubKey, REGSAM samDesired)
52 {
53     return _cicRegKey_Open(*this, hKey, lpSubKey, samDesired);
54 }
55 
56 inline LSTATUS CicRegKey::Create(HKEY hKey, LPCTSTR lpSubKey)
57 {
58     return _cicRegKey_Create(*this, hKey, lpSubKey);
59 }
60 
61 inline LSTATUS CicRegKey::QueryDword(LPCTSTR pszValueName, LPDWORD pdwValue)
62 {
63     DWORD cbData = sizeof(DWORD);
64     return ::RegQueryValueEx(m_hKey, pszValueName, 0, NULL, (LPBYTE)pdwValue, &cbData);
65 }
66 
67 inline LSTATUS CicRegKey::SetDword(LPCTSTR pszValueName, DWORD dwValue)
68 {
69     return ::RegSetValueEx(m_hKey, pszValueName, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue));
70 }
71 
72 inline LSTATUS CicRegKey::SetSz(LPCTSTR pszValueName, LPCTSTR pszValue)
73 {
74     DWORD cbValue = (lstrlen(pszValue) + 1) * sizeof(TCHAR);
75     return ::RegSetValueEx(m_hKey, pszValueName, 0, REG_SZ, (LPBYTE)pszValue, cbValue);
76 }
77 
78 inline LSTATUS CicRegKey::SetSzW(LPCWSTR pszValueName, LPCWSTR pszValue)
79 {
80     DWORD cbValue = (lstrlenW(pszValue) + 1) * sizeof(WCHAR);
81     return ::RegSetValueExW(m_hKey, pszValueName, 0, REG_SZ, (LPBYTE)pszValue, cbValue);
82 }
83 
84 inline LSTATUS CicRegKey::QuerySz(LPCTSTR pszValueName, LPTSTR pszValue, DWORD cchValueMax)
85 {
86     return _cicRegKey_QuerySz(*this, pszValueName, pszValue, cchValueMax);
87 }
88 
89 inline void CicRegKey::Close()
90 {
91     if (!m_hKey)
92         return;
93 
94     ::RegCloseKey(m_hKey);
95     m_hKey = NULL;
96 }
97 
98 inline LSTATUS CicRegKey::DeleteValue(LPCTSTR pszValueName)
99 {
100     return ::RegDeleteValue(m_hKey, pszValueName);
101 }
102 
103 inline LSTATUS CicRegKey::DeleteSubKey(LPCTSTR lpSubKey)
104 {
105     return ::RegDeleteKey(m_hKey, lpSubKey);
106 }
107 
108 inline LSTATUS CicRegKey::EnumValue(DWORD dwIndex, LPTSTR lpValueName, DWORD cchValueName)
109 {
110     return _cicRegKey_EnumValue(*this, dwIndex, lpValueName, cchValueName);
111 }
112 
113 inline LSTATUS CicRegKey::RecurseDeleteKey(LPCTSTR lpSubKey)
114 {
115     return _cicRegKey_RecurseDeleteKey(*this, lpSubKey);
116 }
117