1 /*
2  * PROJECT:         ReactOS Utility Manager Resources DLL (UManDlg.dll)
3  * LICENSE:         GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4  * PURPOSE:         Registry functions for Utility Manager settings management
5  * COPYRIGHT:       Copyright 2020 Bișoc George (fraizeraust99 at gmail dot com)
6  */
7 
8 /* INCLUDES *******************************************************************/
9 
10 #include "umandlg.h"
11 
12 /* GLOBALS ********************************************************************/
13 
14 UTILMAN_GLOBALS Globals;
15 REGISTRY_DATA RegData;
16 REGISTRY_SETTINGS Settings;
17 
18 /* DEFINES ********************************************************************/
19 
20 #define ACCESS_UTILMAN_KEY L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Accessibility\\Utility Manager"
21 #define UTILMAN_KEY L"SOFTWARE\\Microsoft\\Utility Manager"
22 #define OSK_KEY L"On-Screen Keyboard"
23 #define MAGNIFIER_KEY L"Magnifier"
24 
25 /* FUNCTIONS ******************************************************************/
26 
27 /**
28  * @InitAppRegKey
29  *
30  * Initialize a key. The function may not necessarily create it but open the key
31  * if it already exists. The disposition pointed lpdwDisposition determines that.
32  * This is a function helper.
33  *
34  * @param[in]   hPredefinedKey
35  *     The predefined key (e.g. HKEY_CLASSES_ROOT).
36  *
37  * @param[in]   lpszSubKey
38  *      The path to the sub key to be created.
39  *
40  * @param[out]   phKey
41  *      A pointer that receives a handle to the key given by the function.
42  *
43  * @param[out]   lpdwDisposition
44  *      A pointer that receives the disposition given by the function.
45  *
46  * @return
47  *      Returns TRUE if the function successfully created a key (or opened it),
48  *      FALSE otherwise for failure.
49  *
50  */
51 BOOL InitAppRegKey(IN HKEY hPredefinedKey,
52                    IN LPCWSTR lpszSubKey,
53                    OUT PHKEY phKey,
54                    OUT LPDWORD lpdwDisposition)
55 {
56     LONG lResult;
57 
58     lResult = RegCreateKeyExW(hPredefinedKey,
59                               lpszSubKey,
60                               0,
61                               NULL,
62                               0,
63                               KEY_WRITE,
64                               NULL,
65                               phKey,
66                               lpdwDisposition);
67     if (lResult != ERROR_SUCCESS)
68     {
69         DPRINT("InitAppRegKey(): Failed to create the following key (or open the key) of path \"%S\". The error code is \"%li\".\n", lpszSubKey, lResult);
70         return FALSE;
71     }
72 
73     return TRUE;
74 }
75 
76 /**
77  * @QueryAppSettings
78  *
79  * Query the setting from the application's key. This is a function helper.
80  *
81  * @param[in]   hKey
82  *     A handle to a key.
83  *
84  * @param[in]   lpszSubKey
85  *      The path to a sub-key.
86  *
87  * @param[in]   lpszRegValue
88  *      The registry value where we need to get the data from.
89  *
90  * @param[out]   ReturnedData
91  *      An arbitrary pointer that receives the returned data. Being arbitrary,
92  *      the data can be of any type.
93  *
94  * @param[inout]   lpdwSizeData
95  *      A pointer to the returned data pointed by ReturnedData parameter that
96  *      retrieves the size of the aforementioned data, in bytes.
97  *
98  * @return
99  *      Returns TRUE if the function successfully loaded the value we wanted,
100  *      FALSE otherwise for failure.
101  *
102  */
103 BOOL QueryAppSettings(IN HKEY hKey,
104                       IN LPCWSTR lpszSubKey,
105                       IN LPCWSTR lpszRegValue,
106                       OUT PVOID ReturnedData,
107                       IN OUT LPDWORD lpdwSizeData)
108 {
109     LONG lResult;
110     HKEY hKeyQueryValue;
111 
112     lResult = RegOpenKeyExW(hKey,
113                             lpszSubKey,
114                             0,
115                             KEY_READ,
116                             &hKeyQueryValue);
117     if (lResult != ERROR_SUCCESS)
118     {
119         DPRINT("QueryAppSettings(): Failed to open the key of path \"%S\". The error code is \"%li\".\n", lpszSubKey, lResult);
120         return FALSE;
121     }
122 
123     lResult = RegQueryValueExW(hKeyQueryValue,
124                                lpszRegValue,
125                                NULL,
126                                NULL,
127                                (LPBYTE)&ReturnedData,
128                                lpdwSizeData);
129     if (lResult != ERROR_SUCCESS)
130     {
131         DPRINT("QueryAppSettings(): Failed to query the data from value \"%S\". The error code is \"%li\".\n", lpszRegValue, lResult);
132         RegCloseKey(hKeyQueryValue);
133         return FALSE;
134     }
135 
136     RegCloseKey(hKeyQueryValue);
137     return TRUE;
138 }
139 
140 /**
141  * @SaveAppSettings
142  *
143  * Save an application's setting data to the Registry. This is a function helper.
144  *
145  * @param[in]   hKey
146  *     A handle to a key.
147  *
148  * @param[in]   lpszRegValue
149  *      The path to the sub key where the value needs to be created.
150  *
151  * @param[out]   dwRegType
152  *      The type of registry value to be created (e.g. a REG_DWORD).
153  *
154  * @param[in]    Data
155  *      A pointer to an arbitrary data for the value to be set. Being arbitrary,
156  *      the data can be of any type (in conformity with the registry type pointed by
157  *      dwRegType) otherwise the function might lead to a undefined behaviour.
158  *
159  * @param[in]    cbSize
160  *      The size of the buffer data pointed by Data parameter, in bytes.
161  *
162  * @return
163  *      Returns TRUE if the function successfully saved the application's setting,
164  *      FALSE otherwise for failure.
165  *
166  */
167 BOOL SaveAppSettings(IN HKEY hKey,
168                      IN LPCWSTR lpszRegValue,
169                      IN DWORD dwRegType,
170                      IN PVOID Data,
171                      IN DWORD cbSize)
172 {
173     LONG lResult;
174     HKEY hKeySetValue;
175 
176     lResult = RegOpenKeyExW(hKey,
177                             NULL,
178                             0,
179                             KEY_SET_VALUE,
180                             &hKeySetValue);
181     if (lResult != ERROR_SUCCESS)
182     {
183         DPRINT("SaveAppSettings(): Failed to open the key, the error code is \"%li\"!\n", lResult);
184         return FALSE;
185     }
186 
187     lResult = RegSetValueExW(hKeySetValue,
188                              lpszRegValue,
189                              0,
190                              dwRegType,
191                              (LPBYTE)&Data,
192                              cbSize);
193     if (lResult != ERROR_SUCCESS)
194     {
195         DPRINT("SaveAppSettings(): Failed to set the \"%S\" value with data, the error code is \"%li\"!\n", lpszRegValue, lResult);
196         RegCloseKey(hKeySetValue);
197         return FALSE;
198     }
199 
200     RegCloseKey(hKeySetValue);
201     return TRUE;
202 }
203