1370b324cSopenharmony_ci// Windows/Registry.cpp
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#include "StdAfx.h"
4370b324cSopenharmony_ci
5370b324cSopenharmony_ci#include <wchar.h>
6370b324cSopenharmony_ci// #include <stdio.h>
7370b324cSopenharmony_ci
8370b324cSopenharmony_ci#ifndef _UNICODE
9370b324cSopenharmony_ci#include "../Common/StringConvert.h"
10370b324cSopenharmony_ci#endif
11370b324cSopenharmony_ci#include "Registry.h"
12370b324cSopenharmony_ci
13370b324cSopenharmony_ci#ifndef _UNICODE
14370b324cSopenharmony_ciextern bool g_IsNT;
15370b324cSopenharmony_ci#endif
16370b324cSopenharmony_ci
17370b324cSopenharmony_cinamespace NWindows {
18370b324cSopenharmony_cinamespace NRegistry {
19370b324cSopenharmony_ci
20370b324cSopenharmony_ci#define MYASSERT(expr) // _ASSERTE(expr)
21370b324cSopenharmony_ci#define MY_ASSUME(expr)
22370b324cSopenharmony_ci
23370b324cSopenharmony_ci/*
24370b324cSopenharmony_cistatic void Error()
25370b324cSopenharmony_ci{
26370b324cSopenharmony_ci  #ifdef _CONSOLE
27370b324cSopenharmony_ci  printf("\nregistry error\n");
28370b324cSopenharmony_ci  #else
29370b324cSopenharmony_ci  MessageBoxW(0, L"registry error", L"", 0);
30370b324cSopenharmony_ci  // exit(1);
31370b324cSopenharmony_ci  #endif
32370b324cSopenharmony_ci}
33370b324cSopenharmony_ci
34370b324cSopenharmony_ci#define MY_ASSUME(expr) { if (!(expr)) Error(); }
35370b324cSopenharmony_ci*/
36370b324cSopenharmony_ci
37370b324cSopenharmony_ciLONG CKey::Create(HKEY parentKey, LPCTSTR keyName,
38370b324cSopenharmony_ci    LPTSTR keyClass, DWORD options, REGSAM accessMask,
39370b324cSopenharmony_ci    LPSECURITY_ATTRIBUTES securityAttributes, LPDWORD disposition) throw()
40370b324cSopenharmony_ci{
41370b324cSopenharmony_ci  MY_ASSUME(parentKey != NULL);
42370b324cSopenharmony_ci  DWORD dispositionReal;
43370b324cSopenharmony_ci  HKEY key = NULL;
44370b324cSopenharmony_ci  LONG res = RegCreateKeyEx(parentKey, keyName, 0, keyClass,
45370b324cSopenharmony_ci      options, accessMask, securityAttributes, &key, &dispositionReal);
46370b324cSopenharmony_ci  if (disposition != NULL)
47370b324cSopenharmony_ci    *disposition = dispositionReal;
48370b324cSopenharmony_ci  if (res == ERROR_SUCCESS)
49370b324cSopenharmony_ci  {
50370b324cSopenharmony_ci    res = Close();
51370b324cSopenharmony_ci    _object = key;
52370b324cSopenharmony_ci  }
53370b324cSopenharmony_ci  return res;
54370b324cSopenharmony_ci}
55370b324cSopenharmony_ci
56370b324cSopenharmony_ciLONG CKey::Open(HKEY parentKey, LPCTSTR keyName, REGSAM accessMask) throw()
57370b324cSopenharmony_ci{
58370b324cSopenharmony_ci  MY_ASSUME(parentKey != NULL);
59370b324cSopenharmony_ci  HKEY key = NULL;
60370b324cSopenharmony_ci  LONG res = RegOpenKeyEx(parentKey, keyName, 0, accessMask, &key);
61370b324cSopenharmony_ci  if (res == ERROR_SUCCESS)
62370b324cSopenharmony_ci  {
63370b324cSopenharmony_ci    res = Close();
64370b324cSopenharmony_ci    MYASSERT(res == ERROR_SUCCESS);
65370b324cSopenharmony_ci    _object = key;
66370b324cSopenharmony_ci  }
67370b324cSopenharmony_ci  return res;
68370b324cSopenharmony_ci}
69370b324cSopenharmony_ci
70370b324cSopenharmony_ciLONG CKey::Close() throw()
71370b324cSopenharmony_ci{
72370b324cSopenharmony_ci  LONG res = ERROR_SUCCESS;
73370b324cSopenharmony_ci  if (_object != NULL)
74370b324cSopenharmony_ci  {
75370b324cSopenharmony_ci    res = RegCloseKey(_object);
76370b324cSopenharmony_ci    _object = NULL;
77370b324cSopenharmony_ci  }
78370b324cSopenharmony_ci  return res;
79370b324cSopenharmony_ci}
80370b324cSopenharmony_ci
81370b324cSopenharmony_ci// win95, win98: deletes sunkey and all its subkeys
82370b324cSopenharmony_ci// winNT to be deleted must not have subkeys
83370b324cSopenharmony_ciLONG CKey::DeleteSubKey(LPCTSTR subKeyName) throw()
84370b324cSopenharmony_ci{
85370b324cSopenharmony_ci  MY_ASSUME(_object != NULL);
86370b324cSopenharmony_ci  return RegDeleteKey(_object, subKeyName);
87370b324cSopenharmony_ci}
88370b324cSopenharmony_ci
89370b324cSopenharmony_ciLONG CKey::RecurseDeleteKey(LPCTSTR subKeyName) throw()
90370b324cSopenharmony_ci{
91370b324cSopenharmony_ci  CKey key;
92370b324cSopenharmony_ci  LONG res = key.Open(_object, subKeyName, KEY_READ | KEY_WRITE);
93370b324cSopenharmony_ci  if (res != ERROR_SUCCESS)
94370b324cSopenharmony_ci    return res;
95370b324cSopenharmony_ci  FILETIME fileTime;
96370b324cSopenharmony_ci  const UInt32 kBufSize = MAX_PATH + 1; // 256 in ATL
97370b324cSopenharmony_ci  DWORD size = kBufSize;
98370b324cSopenharmony_ci  TCHAR buffer[kBufSize];
99370b324cSopenharmony_ci  while (RegEnumKeyEx(key._object, 0, buffer, &size, NULL, NULL, NULL, &fileTime) == ERROR_SUCCESS)
100370b324cSopenharmony_ci  {
101370b324cSopenharmony_ci    res = key.RecurseDeleteKey(buffer);
102370b324cSopenharmony_ci    if (res != ERROR_SUCCESS)
103370b324cSopenharmony_ci      return res;
104370b324cSopenharmony_ci    size = kBufSize;
105370b324cSopenharmony_ci  }
106370b324cSopenharmony_ci  key.Close();
107370b324cSopenharmony_ci  return DeleteSubKey(subKeyName);
108370b324cSopenharmony_ci}
109370b324cSopenharmony_ci
110370b324cSopenharmony_ci
111370b324cSopenharmony_ci/////////////////////////
112370b324cSopenharmony_ci// Value Functions
113370b324cSopenharmony_ci
114370b324cSopenharmony_cistatic inline UInt32 BoolToUINT32(bool value) {  return (value ? 1: 0); }
115370b324cSopenharmony_cistatic inline bool UINT32ToBool(UInt32 value) {  return (value != 0); }
116370b324cSopenharmony_ci
117370b324cSopenharmony_ci
118370b324cSopenharmony_ciLONG CKey::DeleteValue(LPCTSTR name) throw()
119370b324cSopenharmony_ci{
120370b324cSopenharmony_ci  MY_ASSUME(_object != NULL);
121370b324cSopenharmony_ci  return ::RegDeleteValue(_object, name);
122370b324cSopenharmony_ci}
123370b324cSopenharmony_ci
124370b324cSopenharmony_ci#ifndef _UNICODE
125370b324cSopenharmony_ciLONG CKey::DeleteValue(LPCWSTR name)
126370b324cSopenharmony_ci{
127370b324cSopenharmony_ci  MY_ASSUME(_object != NULL);
128370b324cSopenharmony_ci  if (g_IsNT)
129370b324cSopenharmony_ci    return ::RegDeleteValueW(_object, name);
130370b324cSopenharmony_ci  return DeleteValue(name == 0 ? 0 : (LPCSTR)GetSystemString(name));
131370b324cSopenharmony_ci}
132370b324cSopenharmony_ci#endif
133370b324cSopenharmony_ci
134370b324cSopenharmony_ciLONG CKey::SetValue(LPCTSTR name, UInt32 value) throw()
135370b324cSopenharmony_ci{
136370b324cSopenharmony_ci  MY_ASSUME(_object != NULL);
137370b324cSopenharmony_ci  return RegSetValueEx(_object, name, 0, REG_DWORD,
138370b324cSopenharmony_ci      (const BYTE *)&value, sizeof(UInt32));
139370b324cSopenharmony_ci}
140370b324cSopenharmony_ci
141370b324cSopenharmony_ciLONG CKey::SetValue(LPCTSTR name, bool value) throw()
142370b324cSopenharmony_ci{
143370b324cSopenharmony_ci  return SetValue(name, BoolToUINT32(value));
144370b324cSopenharmony_ci}
145370b324cSopenharmony_ci
146370b324cSopenharmony_ciLONG CKey::SetValue(LPCTSTR name, LPCTSTR value) throw()
147370b324cSopenharmony_ci{
148370b324cSopenharmony_ci  MYASSERT(value != NULL);
149370b324cSopenharmony_ci  MY_ASSUME(_object != NULL);
150370b324cSopenharmony_ci  return RegSetValueEx(_object, name, 0, REG_SZ,
151370b324cSopenharmony_ci      (const BYTE *)value, ((DWORD)lstrlen(value) + 1) * sizeof(TCHAR));
152370b324cSopenharmony_ci}
153370b324cSopenharmony_ci
154370b324cSopenharmony_ci/*
155370b324cSopenharmony_ciLONG CKey::SetValue(LPCTSTR name, const CSysString &value)
156370b324cSopenharmony_ci{
157370b324cSopenharmony_ci  MYASSERT(value != NULL);
158370b324cSopenharmony_ci  MY_ASSUME(_object != NULL);
159370b324cSopenharmony_ci  return RegSetValueEx(_object, name, NULL, REG_SZ,
160370b324cSopenharmony_ci      (const BYTE *)(const TCHAR *)value, (value.Len() + 1) * sizeof(TCHAR));
161370b324cSopenharmony_ci}
162370b324cSopenharmony_ci*/
163370b324cSopenharmony_ci
164370b324cSopenharmony_ci#ifndef _UNICODE
165370b324cSopenharmony_ci
166370b324cSopenharmony_ciLONG CKey::SetValue(LPCWSTR name, LPCWSTR value)
167370b324cSopenharmony_ci{
168370b324cSopenharmony_ci  MYASSERT(value != NULL);
169370b324cSopenharmony_ci  MY_ASSUME(_object != NULL);
170370b324cSopenharmony_ci  if (g_IsNT)
171370b324cSopenharmony_ci    return RegSetValueExW(_object, name, 0, REG_SZ,
172370b324cSopenharmony_ci      (const BYTE * )value, (DWORD)((wcslen(value) + 1) * sizeof(wchar_t)));
173370b324cSopenharmony_ci  return SetValue(name == 0 ? 0 : (LPCSTR)GetSystemString(name),
174370b324cSopenharmony_ci    value == 0 ? 0 : (LPCSTR)GetSystemString(value));
175370b324cSopenharmony_ci}
176370b324cSopenharmony_ci
177370b324cSopenharmony_ci#endif
178370b324cSopenharmony_ci
179370b324cSopenharmony_ci
180370b324cSopenharmony_ciLONG CKey::SetValue(LPCTSTR name, const void *value, UInt32 size) throw()
181370b324cSopenharmony_ci{
182370b324cSopenharmony_ci  MYASSERT(value != NULL);
183370b324cSopenharmony_ci  MY_ASSUME(_object != NULL);
184370b324cSopenharmony_ci  return RegSetValueEx(_object, name, 0, REG_BINARY,
185370b324cSopenharmony_ci      (const BYTE *)value, size);
186370b324cSopenharmony_ci}
187370b324cSopenharmony_ci
188370b324cSopenharmony_ciLONG SetValue(HKEY parentKey, LPCTSTR keyName, LPCTSTR valueName, LPCTSTR value)
189370b324cSopenharmony_ci{
190370b324cSopenharmony_ci  MYASSERT(value != NULL);
191370b324cSopenharmony_ci  CKey key;
192370b324cSopenharmony_ci  LONG res = key.Create(parentKey, keyName);
193370b324cSopenharmony_ci  if (res == ERROR_SUCCESS)
194370b324cSopenharmony_ci    res = key.SetValue(valueName, value);
195370b324cSopenharmony_ci  return res;
196370b324cSopenharmony_ci}
197370b324cSopenharmony_ci
198370b324cSopenharmony_ciLONG CKey::SetKeyValue(LPCTSTR keyName, LPCTSTR valueName, LPCTSTR value) throw()
199370b324cSopenharmony_ci{
200370b324cSopenharmony_ci  MYASSERT(value != NULL);
201370b324cSopenharmony_ci  CKey key;
202370b324cSopenharmony_ci  LONG res = key.Create(_object, keyName);
203370b324cSopenharmony_ci  if (res == ERROR_SUCCESS)
204370b324cSopenharmony_ci    res = key.SetValue(valueName, value);
205370b324cSopenharmony_ci  return res;
206370b324cSopenharmony_ci}
207370b324cSopenharmony_ci
208370b324cSopenharmony_ciLONG CKey::QueryValue(LPCTSTR name, UInt32 &value) throw()
209370b324cSopenharmony_ci{
210370b324cSopenharmony_ci  DWORD type = 0;
211370b324cSopenharmony_ci  DWORD count = sizeof(DWORD);
212370b324cSopenharmony_ci  LONG res = RegQueryValueEx(_object, name, NULL, &type,
213370b324cSopenharmony_ci    (LPBYTE)&value, &count);
214370b324cSopenharmony_ci  MYASSERT((res != ERROR_SUCCESS) || (type == REG_DWORD));
215370b324cSopenharmony_ci  MYASSERT((res != ERROR_SUCCESS) || (count == sizeof(UInt32)));
216370b324cSopenharmony_ci  return res;
217370b324cSopenharmony_ci}
218370b324cSopenharmony_ci
219370b324cSopenharmony_ciLONG CKey::QueryValue(LPCTSTR name, bool &value) throw()
220370b324cSopenharmony_ci{
221370b324cSopenharmony_ci  UInt32 uintValue = BoolToUINT32(value);
222370b324cSopenharmony_ci  LONG res = QueryValue(name, uintValue);
223370b324cSopenharmony_ci  value = UINT32ToBool(uintValue);
224370b324cSopenharmony_ci  return res;
225370b324cSopenharmony_ci}
226370b324cSopenharmony_ci
227370b324cSopenharmony_ciLONG CKey::GetValue_IfOk(LPCTSTR name, UInt32 &value) throw()
228370b324cSopenharmony_ci{
229370b324cSopenharmony_ci  UInt32 newVal;
230370b324cSopenharmony_ci  LONG res = QueryValue(name, newVal);
231370b324cSopenharmony_ci  if (res == ERROR_SUCCESS)
232370b324cSopenharmony_ci    value = newVal;
233370b324cSopenharmony_ci  return res;
234370b324cSopenharmony_ci}
235370b324cSopenharmony_ci
236370b324cSopenharmony_ciLONG CKey::GetValue_IfOk(LPCTSTR name, bool &value) throw()
237370b324cSopenharmony_ci{
238370b324cSopenharmony_ci  bool newVal = false;
239370b324cSopenharmony_ci  LONG res = QueryValue(name, newVal);
240370b324cSopenharmony_ci  if (res == ERROR_SUCCESS)
241370b324cSopenharmony_ci    value = newVal;
242370b324cSopenharmony_ci  return res;
243370b324cSopenharmony_ci}
244370b324cSopenharmony_ci
245370b324cSopenharmony_ciLONG CKey::QueryValue(LPCTSTR name, LPTSTR value, UInt32 &count) throw()
246370b324cSopenharmony_ci{
247370b324cSopenharmony_ci  DWORD type = 0;
248370b324cSopenharmony_ci  LONG res = RegQueryValueEx(_object, name, NULL, &type, (LPBYTE)value, (DWORD *)&count);
249370b324cSopenharmony_ci  MYASSERT((res != ERROR_SUCCESS) || (type == REG_SZ) || (type == REG_MULTI_SZ) || (type == REG_EXPAND_SZ));
250370b324cSopenharmony_ci  return res;
251370b324cSopenharmony_ci}
252370b324cSopenharmony_ci
253370b324cSopenharmony_ciLONG CKey::QueryValue(LPCTSTR name, CSysString &value)
254370b324cSopenharmony_ci{
255370b324cSopenharmony_ci  value.Empty();
256370b324cSopenharmony_ci  DWORD type = 0;
257370b324cSopenharmony_ci  UInt32 curSize = 0;
258370b324cSopenharmony_ci  LONG res = RegQueryValueEx(_object, name, NULL, &type, NULL, (DWORD *)&curSize);
259370b324cSopenharmony_ci  if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
260370b324cSopenharmony_ci    return res;
261370b324cSopenharmony_ci  UInt32 curSize2 = curSize;
262370b324cSopenharmony_ci  res = QueryValue(name, value.GetBuf(curSize), curSize2);
263370b324cSopenharmony_ci  if (curSize > curSize2)
264370b324cSopenharmony_ci    curSize = curSize2;
265370b324cSopenharmony_ci  value.ReleaseBuf_CalcLen(curSize / sizeof(TCHAR));
266370b324cSopenharmony_ci  return res;
267370b324cSopenharmony_ci}
268370b324cSopenharmony_ci
269370b324cSopenharmony_ci
270370b324cSopenharmony_ci#ifndef _UNICODE
271370b324cSopenharmony_ci
272370b324cSopenharmony_ciLONG CKey::QueryValue(LPCWSTR name, LPWSTR value, UInt32 &count)
273370b324cSopenharmony_ci{
274370b324cSopenharmony_ci  DWORD type = 0;
275370b324cSopenharmony_ci  LONG res = RegQueryValueExW(_object, name, NULL, &type, (LPBYTE)value, (DWORD *)&count);
276370b324cSopenharmony_ci  MYASSERT((res != ERROR_SUCCESS) || (type == REG_SZ) || (type == REG_MULTI_SZ) || (type == REG_EXPAND_SZ));
277370b324cSopenharmony_ci  return res;
278370b324cSopenharmony_ci}
279370b324cSopenharmony_ci
280370b324cSopenharmony_ciLONG CKey::QueryValue(LPCWSTR name, UString &value)
281370b324cSopenharmony_ci{
282370b324cSopenharmony_ci  value.Empty();
283370b324cSopenharmony_ci  DWORD type = 0;
284370b324cSopenharmony_ci  UInt32 curSize = 0;
285370b324cSopenharmony_ci
286370b324cSopenharmony_ci  LONG res;
287370b324cSopenharmony_ci
288370b324cSopenharmony_ci  if (g_IsNT)
289370b324cSopenharmony_ci  {
290370b324cSopenharmony_ci    res = RegQueryValueExW(_object, name, NULL, &type, NULL, (DWORD *)&curSize);
291370b324cSopenharmony_ci    if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
292370b324cSopenharmony_ci      return res;
293370b324cSopenharmony_ci    UInt32 curSize2 = curSize;
294370b324cSopenharmony_ci    res = QueryValue(name, value.GetBuf(curSize), curSize2);
295370b324cSopenharmony_ci    if (curSize > curSize2)
296370b324cSopenharmony_ci      curSize = curSize2;
297370b324cSopenharmony_ci    value.ReleaseBuf_CalcLen(curSize / sizeof(wchar_t));
298370b324cSopenharmony_ci  }
299370b324cSopenharmony_ci  else
300370b324cSopenharmony_ci  {
301370b324cSopenharmony_ci    AString vTemp;
302370b324cSopenharmony_ci    res = QueryValue(name == 0 ? 0 : (LPCSTR)GetSystemString(name), vTemp);
303370b324cSopenharmony_ci    value = GetUnicodeString(vTemp);
304370b324cSopenharmony_ci  }
305370b324cSopenharmony_ci
306370b324cSopenharmony_ci  return res;
307370b324cSopenharmony_ci}
308370b324cSopenharmony_ci
309370b324cSopenharmony_ci#endif
310370b324cSopenharmony_ci
311370b324cSopenharmony_ci
312370b324cSopenharmony_ciLONG CKey::QueryValue(LPCTSTR name, void *value, UInt32 &count) throw()
313370b324cSopenharmony_ci{
314370b324cSopenharmony_ci  DWORD type = 0;
315370b324cSopenharmony_ci  LONG res = RegQueryValueEx(_object, name, NULL, &type, (LPBYTE)value, (DWORD *)&count);
316370b324cSopenharmony_ci  MYASSERT((res != ERROR_SUCCESS) || (type == REG_BINARY));
317370b324cSopenharmony_ci  return res;
318370b324cSopenharmony_ci}
319370b324cSopenharmony_ci
320370b324cSopenharmony_ci
321370b324cSopenharmony_ciLONG CKey::QueryValue(LPCTSTR name, CByteBuffer &value, UInt32 &dataSize)
322370b324cSopenharmony_ci{
323370b324cSopenharmony_ci  DWORD type = 0;
324370b324cSopenharmony_ci  dataSize = 0;
325370b324cSopenharmony_ci  LONG res = RegQueryValueEx(_object, name, NULL, &type, NULL, (DWORD *)&dataSize);
326370b324cSopenharmony_ci  if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
327370b324cSopenharmony_ci    return res;
328370b324cSopenharmony_ci  value.Alloc(dataSize);
329370b324cSopenharmony_ci  return QueryValue(name, (BYTE *)value, dataSize);
330370b324cSopenharmony_ci}
331370b324cSopenharmony_ci
332370b324cSopenharmony_ciLONG CKey::EnumKeys(CSysStringVector &keyNames)
333370b324cSopenharmony_ci{
334370b324cSopenharmony_ci  keyNames.Clear();
335370b324cSopenharmony_ci  CSysString keyName;
336370b324cSopenharmony_ci  for (DWORD index = 0; ; index++)
337370b324cSopenharmony_ci  {
338370b324cSopenharmony_ci    const unsigned kBufSize = MAX_PATH + 1; // 256 in ATL
339370b324cSopenharmony_ci    FILETIME lastWriteTime;
340370b324cSopenharmony_ci    UInt32 nameSize = kBufSize;
341370b324cSopenharmony_ci    LONG result = ::RegEnumKeyEx(_object, index, keyName.GetBuf(kBufSize),
342370b324cSopenharmony_ci        (DWORD *)&nameSize, NULL, NULL, NULL, &lastWriteTime);
343370b324cSopenharmony_ci    keyName.ReleaseBuf_CalcLen(kBufSize);
344370b324cSopenharmony_ci    if (result == ERROR_NO_MORE_ITEMS)
345370b324cSopenharmony_ci      break;
346370b324cSopenharmony_ci    if (result != ERROR_SUCCESS)
347370b324cSopenharmony_ci      return result;
348370b324cSopenharmony_ci    keyNames.Add(keyName);
349370b324cSopenharmony_ci  }
350370b324cSopenharmony_ci  return ERROR_SUCCESS;
351370b324cSopenharmony_ci}
352370b324cSopenharmony_ci
353370b324cSopenharmony_ciLONG CKey::SetValue_Strings(LPCTSTR valueName, const UStringVector &strings)
354370b324cSopenharmony_ci{
355370b324cSopenharmony_ci  size_t numChars = 0;
356370b324cSopenharmony_ci
357370b324cSopenharmony_ci  unsigned i;
358370b324cSopenharmony_ci
359370b324cSopenharmony_ci  for (i = 0; i < strings.Size(); i++)
360370b324cSopenharmony_ci    numChars += strings[i].Len() + 1;
361370b324cSopenharmony_ci
362370b324cSopenharmony_ci  CObjArray<wchar_t> buffer(numChars);
363370b324cSopenharmony_ci  size_t pos = 0;
364370b324cSopenharmony_ci
365370b324cSopenharmony_ci  for (i = 0; i < strings.Size(); i++)
366370b324cSopenharmony_ci  {
367370b324cSopenharmony_ci    const UString &s = strings[i];
368370b324cSopenharmony_ci    size_t size = s.Len() + 1;
369370b324cSopenharmony_ci    wmemcpy(buffer + pos, s, size);
370370b324cSopenharmony_ci    pos += size;
371370b324cSopenharmony_ci  }
372370b324cSopenharmony_ci  return SetValue(valueName, buffer, (UInt32)numChars * sizeof(wchar_t));
373370b324cSopenharmony_ci}
374370b324cSopenharmony_ci
375370b324cSopenharmony_ciLONG CKey::GetValue_Strings(LPCTSTR valueName, UStringVector &strings)
376370b324cSopenharmony_ci{
377370b324cSopenharmony_ci  strings.Clear();
378370b324cSopenharmony_ci  CByteBuffer buffer;
379370b324cSopenharmony_ci  UInt32 dataSize = 0;
380370b324cSopenharmony_ci  LONG res = QueryValue(valueName, buffer, dataSize);
381370b324cSopenharmony_ci  if (res != ERROR_SUCCESS)
382370b324cSopenharmony_ci    return res;
383370b324cSopenharmony_ci  if (dataSize > buffer.Size())
384370b324cSopenharmony_ci    return E_FAIL;
385370b324cSopenharmony_ci  if (dataSize % sizeof(wchar_t) != 0)
386370b324cSopenharmony_ci    return E_FAIL;
387370b324cSopenharmony_ci
388370b324cSopenharmony_ci  const wchar_t *data = (const wchar_t *)(const void *)(const Byte  *)buffer;
389370b324cSopenharmony_ci  size_t numChars = dataSize / sizeof(wchar_t);
390370b324cSopenharmony_ci  size_t prev = 0;
391370b324cSopenharmony_ci  UString s;
392370b324cSopenharmony_ci
393370b324cSopenharmony_ci  for (size_t i = 0; i < numChars; i++)
394370b324cSopenharmony_ci  {
395370b324cSopenharmony_ci    if (data[i] == 0)
396370b324cSopenharmony_ci    {
397370b324cSopenharmony_ci      s = data + prev;
398370b324cSopenharmony_ci      strings.Add(s);
399370b324cSopenharmony_ci      prev = i + 1;
400370b324cSopenharmony_ci    }
401370b324cSopenharmony_ci  }
402370b324cSopenharmony_ci
403370b324cSopenharmony_ci  return res;
404370b324cSopenharmony_ci}
405370b324cSopenharmony_ci
406370b324cSopenharmony_ci}}
407