blob: 81f39e63133ea0f801a83df82dfa3cb4490239ab [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004// All Rights Reserved.
5
6#ifndef BASE_REGISTRY_H__
7#define BASE_REGISTRY_H__
8
9#include <windows.h>
10#include <tchar.h>
11#include <shlwapi.h>
12#include <string>
13
14// The shared file uses a bunch of header files that define types that we don't.
15// To avoid changing much code from the standard version, and also to avoid
16// polluting our namespace with extra types we don't want, we define these types
17// here with the preprocessor and undefine them at the end of the file.
18#define tchar TCHAR
19#define CTP const tchar*
20#define tstr std::basic_string<tchar>
21
22// RegKey
23// Utility class to read from and manipulate the registry.
24// Registry vocabulary primer: a "key" is like a folder, in which there
25// are "values", which are <name,data> pairs, with an associated data type.
26
27class RegKey {
28 public:
29 RegKey(HKEY rootkey = NULL, CTP subkey = NULL, REGSAM access = KEY_READ);
30 // start there
31
32 ~RegKey() { this->Close(); }
33
34 bool Create(HKEY rootkey, CTP subkey, REGSAM access = KEY_READ);
35
36 bool CreateWithDisposition(HKEY rootkey, CTP subkey, DWORD* disposition,
37 REGSAM access = KEY_READ);
38
39 bool Open(HKEY rootkey, CTP subkey, REGSAM access = KEY_READ);
40
41 // Create a subkey (or open if exists)
42 bool CreateKey(CTP name, REGSAM access);
43
44 // Open a subkey
45 bool OpenKey(CTP name, REGSAM access);
46
47 // all done, eh?
48 void Close();
49
50 DWORD ValueCount(); // Count of the number of value extant
51
52 bool ReadName(int index, tstr* name); // Determine the Nth value's name
53
54 // True while the key is valid
55 bool Valid() const { return NULL != key_; }
56
57 // Kill key and everything that liveth below it; please be careful out there
58 bool DeleteKey(CTP name);
59
60 // Delete a single value within the key
61 bool DeleteValue(CTP name);
62
63 bool ValueExists(CTP name);
64 bool ReadValue(CTP name, void * data, DWORD * dsize, DWORD * dtype = NULL);
65 bool ReadValue(CTP name, tstr * value);
66 bool ReadValueDW(CTP name, DWORD * value); // Named to differ from tstr*
67
68 bool WriteValue(CTP name, const void * data, DWORD dsize,
69 DWORD dtype = REG_BINARY);
70 bool WriteValue(CTP name, CTP value);
71 bool WriteValue(CTP name, DWORD value);
72
73 // StartWatching()
74 // Start watching the key to see if any of its values have changed.
75 // The key must have been opened with the KEY_NOTIFY access
76 // privelege.
77 bool StartWatching();
78
79 // HasChanged()
80 // If StartWatching hasn't been called, always returns false.
81 // Otherwise, returns true if anything under the key has changed.
82 // This can't be const because the watch_event_ may be refreshed.
83 bool HasChanged();
84
85 // StopWatching()
86 // Will automatically be called by destructor if not manually called
87 // beforehand. Returns true if it was watching, false otherwise.
88 bool StopWatching();
89
90 inline bool IsWatching() const { return watch_event_ != 0; }
91 HKEY Handle() const { return key_; }
92
93 private:
94 HKEY key_; // the registry key being iterated
95 HANDLE watch_event_;
96};
97
98
99// Standalone registry functions -- sorta deprecated, they now map to
100// using RegKey
101
102
103// Add a raw data to the registry -- you can pass NULL for the data if
104// you just want to create a key
105inline bool AddToRegistry(HKEY root_key, CTP key, CTP value_name,
106 void const * data, DWORD dsize,
107 DWORD dtype = REG_BINARY) {
108 return RegKey(root_key, key, KEY_WRITE).WriteValue(value_name, data, dsize,
109 dtype);
110}
111
112// Convenience routine to add a string value to the registry
113inline bool AddToRegistry(HKEY root_key, CTP key, CTP value_name, CTP value) {
114 return AddToRegistry(root_key, key, value_name, value,
115 sizeof(*value) * (lstrlen(value) + 1), REG_SZ);
116}
117
118// Read raw data from the registry -- pass something as the dtype
119// parameter if you care to learn what type the value was stored as
120inline bool ReadFromRegistry(HKEY root_key, CTP key, CTP value_name,
121 void* data, DWORD* dsize, DWORD* dtype = NULL) {
122 return RegKey(root_key, key).ReadValue(value_name, data, dsize, dtype);
123}
124
125
126// Delete a value or a key from the registry
127inline bool DeleteFromRegistry(HKEY root_key, CTP subkey, CTP value_name) {
128 if (value_name)
129 return ERROR_SUCCESS == ::SHDeleteValue(root_key, subkey, value_name);
130 else
131 return ERROR_SUCCESS == ::SHDeleteKey(root_key, subkey);
132}
133
134
135
136// delete a key and all subkeys from the registry
137inline bool DeleteKeyFromRegistry(HKEY root_key, CTP key_path, CTP key_name) {
138 RegKey key;
139 return key.Open(root_key, key_path, KEY_WRITE)
140 && key.DeleteKey(key_name);
141}
142
143
144// Iterates the entries found in a particular folder on the registry.
145// For this application I happen to know I wont need data size larger
146// than MAX_PATH, but in real life this wouldn't neccessarily be
147// adequate.
148class RegistryValueIterator {
149 public:
150 // Specify a key in construction
151 RegistryValueIterator(HKEY root_key, LPCTSTR folder_key);
152
153 ~RegistryValueIterator();
154
155 DWORD ValueCount() const; // count of the number of subkeys extant
156
157 bool Valid() const; // true while the iterator is valid
158
159 void operator++(); // advance to the next entry in the folder
160
161 // The pointers returned by these functions are statics owned by the
162 // Name and Value functions
163 CTP Name() const { return name_; }
164 CTP Value() const { return value_; }
165 DWORD ValueSize() const { return value_size_; }
166 DWORD Type() const { return type_; }
167
168 int Index() const { return index_; }
169
170 private:
171 bool Read(); // read in the current values
172
173 HKEY key_; // the registry key being iterated
174 int index_; // current index of the iteration
175
176 // Current values
177 TCHAR name_[MAX_PATH];
178 TCHAR value_[MAX_PATH];
179 DWORD value_size_;
180 DWORD type_;
181};
182
183
184class RegistryKeyIterator {
185 public:
186 // Specify a parent key in construction
187 RegistryKeyIterator(HKEY root_key, LPCTSTR folder_key);
188
189 ~RegistryKeyIterator();
190
191 DWORD SubkeyCount() const; // count of the number of subkeys extant
192
193 bool Valid() const; // true while the iterator is valid
194
195 void operator++(); // advance to the next entry in the folder
196
197 // The pointer returned by Name() is a static owned by the function
198 CTP Name() const { return name_; }
199
200 int Index() const { return index_; }
201
202 private:
203 bool Read(); // read in the current values
204
205 HKEY key_; // the registry key being iterated
206 int index_; // current index of the iteration
207
208 // Current values
209 TCHAR name_[MAX_PATH];
210};
211
212
213// Register a COM object with the most usual properties.
214bool RegisterCOMServer(const tchar* guid, const tchar* name,
215 const tchar* modulepath);
216bool RegisterCOMServer(const tchar* guid, const tchar* name, HINSTANCE module);
217bool UnregisterCOMServer(const tchar* guid);
218
219// undo the local types defined above
220#undef tchar
221#undef CTP
222#undef tstr
223
224#endif // BASE_REGISTRY_H__
license.botf003cfe2008-08-24 09:55:55 +0900225