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