registry.h revision 731df977c0511bca2206b5f333555b1205ff1f43
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(); 42 43 // Determine the nth value's name. 44 bool ReadName(int index, std::wstring* name); 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, DWORD* dtype); 59 bool ReadValue(const wchar_t* name, std::wstring* value); 60 bool ReadValueDW(const wchar_t* name, DWORD* value); 61 62 bool WriteValue(const wchar_t* name, const void* data, DWORD dsize, 63 DWORD dtype); 64 bool WriteValue(const wchar_t* name, const wchar_t* value); 65 bool WriteValue(const wchar_t* name, DWORD value); 66 67 // Starts watching the key to see if any of its values have changed. 68 // The key must have been opened with the KEY_NOTIFY access privelege. 69 bool StartWatching(); 70 71 // If StartWatching hasn't been called, always returns false. 72 // Otherwise, returns true if anything under the key has changed. 73 // This can't be const because the |watch_event_| may be refreshed. 74 bool HasChanged(); 75 76 // Will automatically be called by destructor if not manually called 77 // beforehand. Returns true if it was watching, false otherwise. 78 bool StopWatching(); 79 80 inline bool IsWatching() const { return watch_event_ != 0; } 81 HANDLE watch_event() const { return watch_event_; } 82 HKEY Handle() const { return key_; } 83 84 private: 85 HKEY key_; // The registry key being iterated. 86 HANDLE watch_event_; 87 88 DISALLOW_COPY_AND_ASSIGN(RegKey); 89}; 90 91// Iterates the entries found in a particular folder on the registry. 92// For this application I happen to know I wont need data size larger 93// than MAX_PATH, but in real life this wouldn't neccessarily be 94// adequate. 95class RegistryValueIterator { 96 public: 97 RegistryValueIterator(HKEY root_key, const wchar_t* folder_key); 98 99 ~RegistryValueIterator(); 100 101 DWORD ValueCount() const; 102 103 // True while the iterator is valid. 104 bool Valid() const; 105 106 // Advances to the next registry entry. 107 void operator++(); 108 109 const wchar_t* Name() const { return name_; } 110 const wchar_t* Value() const { return value_; } 111 DWORD ValueSize() const { return value_size_; } 112 DWORD Type() const { return type_; } 113 114 int Index() const { return index_; } 115 116 private: 117 // Read in the current values. 118 bool Read(); 119 120 // The registry key being iterated. 121 HKEY key_; 122 123 // Current index of the iteration. 124 int index_; 125 126 // Current values. 127 wchar_t name_[MAX_PATH]; 128 wchar_t value_[MAX_PATH]; 129 DWORD value_size_; 130 DWORD type_; 131 132 DISALLOW_COPY_AND_ASSIGN(RegistryValueIterator); 133}; 134 135class RegistryKeyIterator { 136 public: 137 RegistryKeyIterator(HKEY root_key, const wchar_t* folder_key); 138 139 ~RegistryKeyIterator(); 140 141 DWORD SubkeyCount() const; 142 143 // True while the iterator is valid. 144 bool Valid() const; 145 146 // Advances to the next entry in the folder. 147 void operator++(); 148 149 const wchar_t* Name() const { return name_; } 150 151 int Index() const { return index_; } 152 153 private: 154 // Read in the current values. 155 bool Read(); 156 157 // The registry key being iterated. 158 HKEY key_; 159 160 // Current index of the iteration. 161 int index_; 162 163 wchar_t name_[MAX_PATH]; 164 165 DISALLOW_COPY_AND_ASSIGN(RegistryKeyIterator); 166}; 167 168} // namespace win 169} // namespace base 170 171#endif // BASE_WIN_REGISTRY_H_ 172