00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <spl/configuration/windows/WinRegKey.h>
00018
00019 WinRegKey::WinRegKey()
00020 : m_hive(HiveUnknownError), m_hkey(0), m_path(), m_isOpen(false)
00021 {
00022 }
00023
00024 WinRegKey::WinRegKey(const WinRegKey& wrk)
00025 : m_hive(wrk.m_hive), m_hkey(wrk.m_hkey), m_path(wrk.m_path), m_isOpen(wrk.m_isOpen)
00026 {
00027 }
00028
00029 WinRegKey::WinRegKey(const String& pathAndKey, KeyHive hive)
00030 : m_hive(hive), m_hkey(0), m_path(pathAndKey), m_isOpen(false)
00031 {
00032 }
00033
00034 WinRegKey::~WinRegKey()
00035 {
00036 Close();
00037 }
00038
00039 String WinRegKey::KeyName() const
00040 {
00041 int pos;
00042 if ((pos = m_path.LastIndexOf('\\')) < 0)
00043 {
00044 return m_path;
00045 }
00046 return *m_path.Substring(pos);
00047 }
00048
00049 void WinRegKey::Open(bool forUpdate)
00050 {
00051 if (m_isOpen)
00052 {
00053 return;
00054 }
00055
00056 int flags = KEY_READ | KEY_ENUMERATE_SUB_KEYS;
00057 if (forUpdate)
00058 {
00059 flags |= KEY_WRITE | KEY_SET_VALUE | KEY_CREATE_SUB_KEY;
00060 }
00061
00062 int ret = RegOpenKeyEx
00063 (
00064 HKEY_CURRENT_USER,
00065 m_path,
00066 0,
00067 flags,
00068 &m_hkey
00069 );
00070
00071 if (ERROR_SUCCESS != ret)
00072 {
00073 throw new IOException("RegOpenKeyEx returned " + Int32::ToString(ret));
00074 }
00075
00076 m_isOpen = true;
00077 }
00078
00079 void WinRegKey::OpenForRead()
00080 {
00081 Open(false);
00082 }
00083
00084 void WinRegKey::OpenForReadWrite()
00085 {
00086 Open(true);
00087 }
00088
00089 void WinRegKey::Close()
00090 {
00091 if (m_isOpen)
00092 {
00093 ASSERT(0 != m_hkey);
00094 RegCloseKey(m_hkey);
00095 m_hkey = 0;
00096 m_isOpen = false;
00097 }
00098 }
00099
00100 void WinRegKey::DeleteSubKey(const String& keyName)
00101 {
00102 bool needToClose = !m_isOpen;
00103 if (!m_isOpen)
00104 {
00105 Open(true);
00106 }
00107 int ret = RegDeleteKey(m_hkey, (LPCSTR)keyName.GetChars());
00108
00109 if (needToClose)
00110 {
00111 Close();
00112 }
00113
00114 if (ERROR_SUCCESS != ret)
00115 {
00116 throw new IOException("RegDeleteKey returned " + Int32::ToString(ret));
00117 }
00118 }
00119
00120 void WinRegKey::DeleteSubTree(const String& keyName)
00121 {
00122 bool needToClose = !m_isOpen;
00123 if (!m_isOpen)
00124 {
00125 Open(true);
00126 }
00127
00128 int ret = RegDeleteTree(m_hkey, (LPCSTR)keyName.GetChars());
00129
00130 if (needToClose)
00131 {
00132 Close();
00133 }
00134
00135 if (ERROR_SUCCESS != ret)
00136 {
00137 throw new IOException("RegDeleteTree returned " + Int32::ToString(ret));
00138 }
00139 }
00140
00141 void WinRegKey::Set(int i)
00142 {
00143 bool needToClose = !m_isOpen;
00144 if (!m_isOpen)
00145 {
00146 Open(true);
00147 }
00148
00149 int ret = RegSetValueEx(m_hkey, NULL, 0, REG_DWORD, (BYTE*)&i, sizeof(int));
00150
00151 if (needToClose)
00152 {
00153 Close();
00154 }
00155
00156 if (ERROR_SUCCESS != ret)
00157 {
00158 throw new IOException("RegSetValueEx returned " + Int32::ToString(ret));
00159 }
00160 }
00161
00162 void WinRegKey::Set(const String& val)
00163 {
00164 bool needToClose = !m_isOpen;
00165 if (!m_isOpen)
00166 {
00167 Open(true);
00168 }
00169
00170 int ret = RegSetValueEx(m_hkey, NULL, 0, REG_SZ, (const BYTE*)val.GetChars(), val.Length());
00171
00172 if (needToClose)
00173 {
00174 Close();
00175 }
00176
00177 if (ERROR_SUCCESS != ret)
00178 {
00179 throw new IOException("RegSetValueEx returned " + Int32::ToString(ret));
00180 }
00181 }
00182
00183 void WinRegKey::SetSubKey(const String&key, const String& val)
00184 {
00185 bool needToClose = !m_isOpen;
00186 if (!m_isOpen)
00187 {
00188 Open(true);
00189 }
00190
00191 int ret = RegSetValueEx(m_hkey, key.GetChars(), 0, REG_SZ, (const BYTE*)val.GetChars(), val.Length());
00192
00193 if (needToClose)
00194 {
00195 Close();
00196 }
00197
00198 if (ERROR_SUCCESS != ret)
00199 {
00200 throw new IOException("RegSetValueEx returned " + Int32::ToString(ret));
00201 }
00202 }
00203
00204 List<WinRegKey> WinRegKey::ListSubKeys()
00205 {
00206 bool needToClose = !m_isOpen;
00207 if (!m_isOpen)
00208 {
00209 Open(true);
00210 }
00211
00212 List<WinRegKey> list;
00213 char keyName[128];
00214 DWORD keySize = sizeof(keyName);
00215 int pos = 0;
00216 int ret;
00217
00218 while (ERROR_SUCCESS == (ret = RegEnumKeyEx(m_hkey, pos++, (LPSTR)keyName, &keySize, 0, 0, 0, 0)))
00219 {
00220 list.Add(WinRegKey(keyName, m_hive));
00221 }
00222
00223 if (needToClose)
00224 {
00225 Close();
00226 }
00227
00228 if (ret != ERROR_NO_MORE_ITEMS)
00229 {
00230 throw new IOException("RegEnumKeyEx returned " + Int32::ToString(ret));
00231 }
00232
00233 return list;
00234 }
00235
00236 String WinRegKey::Value()
00237 {
00238 bool needToClose = !m_isOpen;
00239 if (!m_isOpen)
00240 {
00241 Open(true);
00242 }
00243
00244 DWORD dataType;
00245 char data[256];
00246 DWORD dataSize = sizeof(data);
00247 int ret = RegGetValue(m_hkey, NULL, NULL, 0, &dataType, data, &dataSize);
00248
00249 if (needToClose)
00250 {
00251 Close();
00252 }
00253
00254 if (ERROR_SUCCESS != ret)
00255 {
00256 throw new IOException("RegGetValue returned " + Int32::ToString(ret));
00257 }
00258
00259 return String(data);
00260 }
00261
00262 WinRegKey WinRegKey::Create(KeyHive hive, const String& pathAndKey)
00263 {
00264 int ret = RegCreateKeyEx((HKEY)hive, pathAndKey.GetChars(), 0, NULL, 0, 0, NULL, NULL, NULL);
00265 if (ERROR_SUCCESS != ret)
00266 {
00267 throw new IOException("RegCreateKeyEx returned " + Int32::ToString(ret));
00268 }
00269 return WinRegKey(pathAndKey, hive);
00270 }
00271
00272 bool WinRegKey::KeyExists(KeyHive hive, const String& pathAndKey)
00273 {
00274 int flags = KEY_READ;
00275 HKEY hkey = 0;
00276
00277 int ret = RegOpenKeyEx
00278 (
00279 HKEY_CURRENT_USER,
00280 pathAndKey.GetChars(),
00281 0,
00282 flags,
00283 &hkey
00284 );
00285 RegCloseKey(hkey);
00286
00287 return ERROR_SUCCESS == ret;
00288 }
00289
00290 #ifdef DEBUG
00291 void WinRegKey::ValidateMem() const
00292 {
00293 m_path.ValidateMem();
00294 }
00295
00296 void WinRegKey::CheckMem() const
00297 {
00298 m_path.CheckMem();
00299 }
00300 #endif