registry.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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.
4
5#ifndef BASE_WIN_REGISTRY_H_
6#define BASE_WIN_REGISTRY_H_
7#pragma once
8
9#include <windows.h>
10#include <string>
11
12#include "base/basictypes.h"
13
14namespace base {
15namespace win {
16
17// Utility class to read, write and manipulate the Windows Registry.
18// Registry vocabulary primer: a "key" is like a folder, in which there
19// are "values", which are <name, data> pairs, with an associated data type.
20class RegKey {
21 public:
22  RegKey();
23  RegKey(HKEY rootkey, const wchar_t* subkey, REGSAM access);
24  ~RegKey();
25
26  bool Create(HKEY rootkey, const wchar_t* subkey, REGSAM access);
27
28  bool CreateWithDisposition(HKEY rootkey, const wchar_t* subkey,
29                             DWORD* disposition, REGSAM access);
30
31  bool Open(HKEY rootkey, const wchar_t* subkey, REGSAM access);
32
33  // Creates a subkey or open it if it already exists.
34  bool CreateKey(const wchar_t* name, REGSAM access);
35
36  // Opens a subkey
37  bool OpenKey(const wchar_t* name, REGSAM access);
38
39  void Close();
40
41  DWORD ValueCount() const;
42
43  // Determine the nth value's name.
44  bool ReadName(int index, std::wstring* name) const;
45
46  // True while the key is valid.
47  bool Valid() const { return key_ != NULL; }
48
49  // Kill a key and everything that live below it; please be careful when using
50  // it.
51  bool DeleteKey(const wchar_t* name);
52
53  // Deletes a single value within the key.
54  bool DeleteValue(const wchar_t* name);
55
56  bool ValueExists(const wchar_t* name);
57
58  bool ReadValue(const wchar_t* name, void* data, DWORD* dsize,
59                 DWORD* dtype) const;
60  bool ReadValue(const wchar_t* name, std::wstring* value) const;
61  bool ReadValueDW(const wchar_t* name, DWORD* value) const;
62
63  bool WriteValue(const wchar_t* name, const void* data, DWORD dsize,
64                  DWORD dtype);
65  bool WriteValue(const wchar_t* name, const wchar_t* value);
66  bool WriteValue(const wchar_t* name, DWORD value);
67
68  // Starts watching the key to see if any of its values have changed.
69  // The key must have been opened with the KEY_NOTIFY access privilege.
70  bool StartWatching();
71
72  // If StartWatching hasn't been called, always returns false.
73  // Otherwise, returns true if anything under the key has changed.
74  // This can't be const because the |watch_event_| may be refreshed.
75  bool HasChanged();
76
77  // Will automatically be called by destructor if not manually called
78  // beforehand.  Returns true if it was watching, false otherwise.
79  bool StopWatching();
80
81  inline bool IsWatching() const { return watch_event_ != 0; }
82  HANDLE watch_event() const { return watch_event_; }
83  HKEY Handle() const { return key_; }
84
85 private:
86  HKEY key_;  // The registry key being iterated.
87  HANDLE watch_event_;
88
89  DISALLOW_COPY_AND_ASSIGN(RegKey);
90};
91
92// Iterates the entries found in a particular folder on the registry.
93// For this application I happen to know I wont need data size larger
94// than MAX_PATH, but in real life this wouldn't neccessarily be
95// adequate.
96class RegistryValueIterator {
97 public:
98  RegistryValueIterator(HKEY root_key, const wchar_t* folder_key);
99
100  ~RegistryValueIterator();
101
102  DWORD ValueCount() const;
103
104  // True while the iterator is valid.
105  bool Valid() const;
106
107  // Advances to the next registry entry.
108  void operator++();
109
110  const wchar_t* Name() const { return name_; }
111  const wchar_t* Value() const { return value_; }
112  DWORD ValueSize() const { return value_size_; }
113  DWORD Type() const { return type_; }
114
115  int Index() const { return index_; }
116
117 private:
118  // Read in the current values.
119  bool Read();
120
121  // The registry key being iterated.
122  HKEY key_;
123
124  // Current index of the iteration.
125  int index_;
126
127  // Current values.
128  wchar_t name_[MAX_PATH];
129  wchar_t value_[MAX_PATH];
130  DWORD value_size_;
131  DWORD type_;
132
133  DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator);
134};
135
136class RegistryKeyIterator {
137 public:
138  RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key);
139
140  ~RegistryKeyIterator();
141
142  DWORD SubkeyCount() const;
143
144  // True while the iterator is valid.
145  bool Valid() const;
146
147  // Advances to the next entry in the folder.
148  void operator++();
149
150  const wchar_t* Name() const { return name_; }
151
152  int Index() const { return index_; }
153
154 private:
155  // Read in the current values.
156  bool Read();
157
158  // The registry key being iterated.
159  HKEY key_;
160
161  // Current index of the iteration.
162  int index_;
163
164  wchar_t name_[MAX_PATH];
165
166  DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator);
167};
168
169}  // namespace win
170}  // namespace base
171
172#endif  // BASE_WIN_REGISTRY_H_
173