1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/installer/mini_installer/regkey.h"
7 #include "chrome/installer/mini_installer/mini_installer_constants.h"
8 #include "chrome/installer/mini_installer/mini_string.h"
10 namespace mini_installer
{
12 LONG
RegKey::Open(HKEY key
, const wchar_t* sub_key
, REGSAM access
) {
14 return ::RegOpenKeyEx(key
, sub_key
, NULL
, access
, &key_
);
17 LONG
RegKey::ReadSZValue(const wchar_t* value_name
,
19 size_t value_size
) const {
21 DWORD byte_length
= static_cast<DWORD
>(value_size
* sizeof(wchar_t));
22 LONG result
= ::RegQueryValueEx(key_
, value_name
, NULL
, &type
,
23 reinterpret_cast<BYTE
*>(value
),
25 if (result
== ERROR_SUCCESS
) {
27 result
= ERROR_NOT_SUPPORTED
;
28 } else if (byte_length
== 0) {
30 } else if (value
[byte_length
/sizeof(wchar_t) - 1] != L
'\0') {
31 if ((byte_length
/ sizeof(wchar_t)) < value_size
)
32 value
[byte_length
/ sizeof(wchar_t)] = L
'\0';
34 result
= ERROR_MORE_DATA
;
40 LONG
RegKey::ReadDWValue(const wchar_t* value_name
, DWORD
* value
) const {
42 DWORD byte_length
= sizeof(*value
);
43 LONG result
= ::RegQueryValueEx(key_
, value_name
, NULL
, &type
,
44 reinterpret_cast<BYTE
*>(value
),
46 if (result
== ERROR_SUCCESS
) {
47 if (type
!= REG_DWORD
) {
48 result
= ERROR_NOT_SUPPORTED
;
49 } else if (byte_length
!= sizeof(*value
)) {
50 result
= ERROR_NO_DATA
;
56 LONG
RegKey::WriteSZValue(const wchar_t* value_name
, const wchar_t* value
) {
57 return ::RegSetValueEx(key_
, value_name
, 0, REG_SZ
,
58 reinterpret_cast<const BYTE
*>(value
),
59 (lstrlen(value
) + 1) * sizeof(wchar_t));
62 LONG
RegKey::WriteDWValue(const wchar_t* value_name
, DWORD value
) {
63 return ::RegSetValueEx(key_
, value_name
, 0, REG_DWORD
,
64 reinterpret_cast<const BYTE
*>(&value
),
68 void RegKey::Close() {
77 bool RegKey::ReadSZValue(HKEY root_key
, const wchar_t *sub_key
,
78 const wchar_t *value_name
, wchar_t *value
,
81 return (key
.Open(root_key
, sub_key
, KEY_QUERY_VALUE
) == ERROR_SUCCESS
&&
82 key
.ReadSZValue(value_name
, value
, size
) == ERROR_SUCCESS
);
85 // Opens the Google Update ClientState key for a product. This finds only
86 // registry entries for Chrome; it does not support the Chromium registry
88 bool OpenClientStateKey(HKEY root_key
, const wchar_t* app_guid
,
89 REGSAM access
, RegKey
* key
) {
90 StackString
<MAX_PATH
> client_state_key
;
91 return client_state_key
.assign(kClientStateKeyBase
) &&
92 client_state_key
.append(app_guid
) &&
94 client_state_key
.get(),
95 access
| KEY_WOW64_32KEY
) == ERROR_SUCCESS
);
98 } // namespace mini_installer