ReactOS  0.4.11-dev-946-g431643b
regutils.c
Go to the documentation of this file.
1 #include "precomp.h"
2 #include "utils.h"
3 #include "regutils.h"
4 
10 {
11  HKEY hSubKey = NULL;
12 
13  if (RegOpenKeyExW(hRootKey, KeyName, 0, KEY_ENUMERATE_SUB_KEYS, &hSubKey) == ERROR_SUCCESS)
14  {
15  HKEY hEntryKey = NULL;
16 
17  LRESULT lError = ERROR_SUCCESS;
18  DWORD dwIndex = 0;
19  WCHAR szValueName[MAX_VALUE_NAME] = L"";
20  DWORD dwValueLength = ARRAYSIZE(szValueName);
21 
22  while ( (lError = RegEnumKeyExW(hSubKey, dwIndex, szValueName, &dwValueLength, NULL, NULL, NULL, NULL)) != ERROR_NO_MORE_ITEMS )
23  {
24  if ( (lError == ERROR_SUCCESS) && (RegOpenKeyExW(hSubKey, szValueName, 0, KEY_QUERY_VALUE, &hEntryKey) == ERROR_SUCCESS) )
25  {
27  while (pTable && pTable->QueryRoutine)
28  {
29  pTable->QueryRoutine(hRootKey, KeyName, szValueName, hEntryKey, Context, pTable->EntryContext);
30  ++pTable;
31  }
32 
33  RegCloseKey(hEntryKey);
34  }
35 
36  ++dwIndex;
37  dwValueLength = ARRAYSIZE(szValueName);
38  szValueName[0] = L'\0';
39  }
40 
41  RegCloseKey(hSubKey);
42  }
43 
44  return ERROR_SUCCESS;
45 }
46 
47 //
48 // Idea taken from RtlQueryRegistryValues (see DDK).
49 //
50 LRESULT
55 {
57  HKEY hSubKey = NULL;
58 
59  if ( (res = RegOpenKeyExW(hRootKey, KeyName, 0, KEY_QUERY_VALUE, &hSubKey)) == ERROR_SUCCESS )
60  {
61  DWORD dwIndex = 0, dwType = 0;
62  WCHAR szValueName[MAX_VALUE_NAME] = L"";
63  LPBYTE lpData = NULL;
64  DWORD dwValueLength = ARRAYSIZE(szValueName), dwDataLength = 0;
65 
66  while (RegEnumValueW(hSubKey, dwIndex, szValueName, &dwValueLength, NULL, &dwType, NULL, &dwDataLength) != ERROR_NO_MORE_ITEMS)
67  {
68  ++dwValueLength;
69  lpData = (LPBYTE)MemAlloc(0, dwDataLength);
70 
71  if (RegEnumValueW(hSubKey, dwIndex, szValueName, &dwValueLength, NULL, &dwType, lpData, &dwDataLength) == ERROR_SUCCESS)
72  {
74  while (pTable && pTable->QueryRoutine)
75  {
76  pTable->QueryRoutine(hRootKey, KeyName, szValueName, dwType, lpData, dwDataLength, Context, pTable->EntryContext);
77  ++pTable;
78  }
79  }
80 
81  MemFree(lpData); lpData = NULL;
82 
83  ++dwIndex;
84  dwValueLength = ARRAYSIZE(szValueName), dwDataLength = 0;
85  szValueName[0] = L'\0';
86  }
87 
88  RegCloseKey(hSubKey);
89  }
90 
91  return res;
92 }
93 
94 LONG
96  IN LPCWSTR lpSubKey OPTIONAL,
97  IN LPCWSTR lpValue OPTIONAL,
98  OUT LPDWORD lpData OPTIONAL)
99 {
100  LONG lRet = ERROR_SUCCESS;
101  HKEY hEntryKey = NULL;
102 
103  //
104  // Open the sub-key, if any. Otherwise,
105  // use the given registry key handle.
106  //
107  if (lpSubKey)
108  {
109  lRet = RegOpenKeyExW(hKey, lpSubKey, 0, KEY_QUERY_VALUE, &hEntryKey);
110  }
111  else
112  {
113  if (hKey != INVALID_HANDLE_VALUE)
114  {
115  // TODO: Ensure that hKey has the KEY_QUERY_VALUE right.
116  hEntryKey = hKey;
117  lRet = ERROR_SUCCESS;
118  }
119  else
120  {
121  lRet = ERROR_INVALID_HANDLE;
122  }
123  }
124 
125  if (lRet == ERROR_SUCCESS)
126  {
127  DWORD dwType = 0,
128  dwRegData = 0,
129  dwBufSize = sizeof(dwRegData /* DWORD */);
130 
131  lRet = RegQueryValueExW(hEntryKey, lpValue, NULL, &dwType, (LPBYTE)&dwRegData, &dwBufSize);
132 
133  if (lRet == ERROR_SUCCESS)
134  {
135  if ( (dwType == REG_DWORD) && (dwBufSize == sizeof(DWORD)) )
136  {
137  if (lpData)
138  *lpData = dwRegData;
139  }
140  else
141  {
142  lRet = ERROR_UNSUPPORTED_TYPE;
143  }
144  }
145  else if (lRet == ERROR_MORE_DATA)
146  {
147  if (dwType != REG_DWORD)
148  {
149  lRet = ERROR_UNSUPPORTED_TYPE;
150  }
151  }
152 
153  // Close the opened sub-key.
154  if (lpSubKey)
155  RegCloseKey(hEntryKey);
156  }
157 
158  return lRet;
159 }
160 
161 LONG
163  IN LPCWSTR lpSubKey OPTIONAL,
164  IN LPCWSTR lpValue OPTIONAL,
165  IN BOOL bCreateKeyIfDoesntExist,
166  IN DWORD dwData)
167 {
168  LONG lRet = ERROR_SUCCESS;
169  HKEY hEntryKey = NULL;
170 
171  //
172  // Open (or create) the sub-key, if any.
173  // Otherwise, use the given registry key handle.
174  //
175  if (lpSubKey)
176  {
177  if (bCreateKeyIfDoesntExist)
178  lRet = RegCreateKeyExW(hKey, lpSubKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, NULL, &hEntryKey, NULL);
179  else
180  lRet = RegOpenKeyExW(hKey, lpSubKey, 0, KEY_SET_VALUE, &hEntryKey);
181  }
182  else
183  {
184  if (hKey != INVALID_HANDLE_VALUE)
185  {
186  // TODO: Ensure that hKey has the KEY_QUERY_VALUE right.
187  hEntryKey = hKey;
188  lRet = ERROR_SUCCESS;
189  }
190  else
191  {
192  lRet = ERROR_INVALID_HANDLE;
193  }
194  }
195 
196  //
197  // Opening successful, can set the value now.
198  //
199  if (lRet == ERROR_SUCCESS)
200  {
201  lRet = RegSetValueExW(hEntryKey, lpValue, 0, REG_DWORD, (LPBYTE)&dwData, sizeof(dwData /* DWORD */));
202 
203  // Close the opened (or created) sub-key.
204  if (lpSubKey)
205  RegCloseKey(hEntryKey);
206  }
207 
208  return lRet;
209 }
_Must_inspect_result_ _Out_ PNDIS_STATUS _In_ NDIS_HANDLE _In_ ULONG _Out_ PNDIS_STRING KeyName
Definition: ndis.h:4693
_In_ PCWSTR _Inout_ _At_ QueryTable _Pre_unknown_ PRTL_QUERY_REGISTRY_TABLE QueryTable
Definition: rtlfuncs.h:3988
#define IN
Definition: typedefs.h:38
LONG RegSetDWORDValue(IN HKEY hKey, IN LPCWSTR lpSubKey OPTIONAL, IN LPCWSTR lpValue OPTIONAL, IN BOOL bCreateKeyIfDoesntExist, IN DWORD dwData)
Definition: regutils.c:162
BOOL MemFree(IN PVOID lpMem)
Definition: utils.c:26
#define ERROR_SUCCESS
Definition: deptool.c:10
#define KEY_SET_VALUE
Definition: nt_native.h:1017
#define ERROR_NO_MORE_ITEMS
Definition: compat.h:95
__wchar_t WCHAR
Definition: xmlstorage.h:180
PVOID MemAlloc(IN DWORD dwFlags, IN DWORD dwBytes)
Definition: utils.c:33
LRESULT RegQueryRegistryKeys(IN HKEY hRootKey, IN LPCWSTR KeyName, IN PQUERY_REGISTRY_KEYS_TABLE QueryTable, IN PVOID Context)
Definition: regutils.c:6
#define ERROR_INVALID_HANDLE
Definition: compat.h:88
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
static HANDLE ULONG_PTR dwData
Definition: file.c:35
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define INVALID_HANDLE_VALUE
Definition: compat.h:391
LONG WINAPI RegCreateKeyExW(_In_ HKEY hKey, _In_ LPCWSTR lpSubKey, _In_ DWORD Reserved, _In_opt_ LPWSTR lpClass, _In_ DWORD dwOptions, _In_ REGSAM samDesired, _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes, _Out_ PHKEY phkResult, _Out_opt_ LPDWORD lpdwDisposition)
Definition: reg.c:1094
LONG WINAPI RegCloseKey(HKEY hKey)
Definition: reg.c:423
unsigned char * LPBYTE
Definition: typedefs.h:52
long LONG
Definition: pedump.c:60
LONG WINAPI RegEnumValueW(_In_ HKEY hKey, _In_ DWORD index, _Out_ LPWSTR value, _Inout_ PDWORD val_count, _Reserved_ PDWORD reserved, _Out_opt_ PDWORD type, _Out_opt_ LPBYTE data, _Inout_opt_ PDWORD count)
Definition: reg.c:2867
smooth NULL
Definition: ftsmooth.c:416
#define MAX_VALUE_NAME
Definition: control.c:23
#define ERROR_UNSUPPORTED_TYPE
Definition: winerror.h:988
#define REG_OPTION_NON_VOLATILE
Definition: nt_native.h:1057
LONG WINAPI RegSetValueExW(_In_ HKEY hKey, _In_ LPCWSTR lpValueName, _In_ DWORD Reserved, _In_ DWORD dwType, _In_ CONST BYTE *lpData, _In_ DWORD cbData)
Definition: reg.c:4917
unsigned int BOOL
Definition: ntddk_ex.h:94
LONG WINAPI RegQueryValueExW(_In_ HKEY hkeyorg, _In_ LPCWSTR name, _In_ LPDWORD reserved, _In_ LPDWORD type, _In_ LPBYTE data, _In_ LPDWORD count)
Definition: reg.c:4134
PQUERY_REGISTRY_VALUES_ROUTINE QueryRoutine
Definition: regutils.h:63
LONG RegGetDWORDValue(IN HKEY hKey, IN LPCWSTR lpSubKey OPTIONAL, IN LPCWSTR lpValue OPTIONAL, OUT LPDWORD lpData OPTIONAL)
Definition: regutils.c:95
unsigned long DWORD
Definition: ntddk_ex.h:95
LRESULT RegQueryRegistryValues(IN HKEY hRootKey, IN LPCWSTR KeyName, IN PQUERY_REGISTRY_VALUES_TABLE QueryTable, IN PVOID Context)
Definition: regutils.c:51
static const WCHAR L[]
Definition: oid.c:1087
#define ERROR_MORE_DATA
Definition: dderror.h:13
PQUERY_REGISTRY_KEYS_ROUTINE QueryRoutine
Definition: regutils.h:29
#define KEY_QUERY_VALUE
Definition: nt_native.h:1016
#define OUT
Definition: typedefs.h:39
GLuint res
Definition: glext.h:9613
uint32_t * LPDWORD
Definition: typedefs.h:57
LONG WINAPI RegOpenKeyExW(HKEY hKey, LPCWSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)
Definition: reg.c:3381
static const EHCI_PERIOD pTable[]
Definition: usbehci.c:29
LONG WINAPI RegEnumKeyExW(_In_ HKEY hKey, _In_ DWORD dwIndex, _Out_ LPWSTR lpName, _Inout_ LPDWORD lpcbName, _Reserved_ LPDWORD lpReserved, _Out_opt_ LPWSTR lpClass, _Inout_opt_ LPDWORD lpcbClass, _Out_opt_ PFILETIME lpftLastWriteTime)
Definition: reg.c:2541
LONG_PTR LRESULT
Definition: windef.h:209
#define REG_DWORD
Definition: sdbapi.c:539
#define KEY_ENUMERATE_SUB_KEYS
Definition: nt_native.h:1019
PULONG MinorVersion OPTIONAL
Definition: CrossNt.h:68