00001
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "precomp.h"
00028 #include "CCRegistry.h"
00029
00031 #define STRING_MAX 256
00032
00034 CCRegistry::RegistryOperation CCRegistry::eLastErrorOp = None;
00036 DWORD CCRegistry::dwLastError = 0;
00037
00046 std::tstring CCRegistry::GetString(HKEY hFromKey, LPCTSTR pPath, LPCTSTR pName, bool* pSuccess , DWORD dwExtraFlags )
00047 {
00048 HKEY hKey = NULL;
00049
00050 if ((dwLastError = RegOpenKeyEx(hFromKey, pPath, 0, KEY_READ, &hKey)) != ERROR_SUCCESS)
00051 {
00052 eLastErrorOp = OpenKey;
00053 if (pSuccess != NULL)
00054 *pSuccess = false;
00055 return _T("");
00056 }
00057
00058
00059 DWORD type;
00060 DWORD nSize = 0;
00061 if ((dwLastError = RegQueryValueEx(hKey, pName, NULL, &type, NULL, &nSize)) != ERROR_SUCCESS) {
00062 eLastErrorOp = QueryValueNull;
00063 RegCloseKey(hKey);
00064 if (pSuccess != NULL)
00065 *pSuccess = false;
00066 return _T("");
00067 }
00068
00069
00070 if (type != REG_SZ) {
00071 eLastErrorOp = WrongType;
00072 dwLastError = type;
00073 RegCloseKey(hKey);
00074 if (pSuccess != NULL)
00075 *pSuccess = false;
00076 return _T("");
00077 }
00078
00079 if (nSize == 0)
00080 {
00081 eLastErrorOp = SizeZero;
00082 dwLastError = 0;
00083 RegCloseKey(hKey);
00084 if (pSuccess != NULL)
00085 *pSuccess = false;
00086 return _T("");
00087 }
00088
00089 TCHAR* p = new TCHAR[(nSize / sizeof(TCHAR)) + 1];
00090
00091
00092 if ((dwLastError = RegQueryValueEx(hKey, pName, NULL, &type, (LPBYTE)p, &nSize)) != ERROR_SUCCESS) {
00093 eLastErrorOp = QueryValue;
00094 delete [] p;
00095 RegCloseKey(hKey);
00096 if (pSuccess != NULL)
00097 *pSuccess = false;
00098 return _T("");
00099 }
00100
00101
00102 std::tstring sRet = p;
00103 delete [] p;
00104 RegCloseKey(hKey);
00105 if (pSuccess != NULL)
00106 *pSuccess = true;
00107 return sRet;
00108 }
00109
00117 bool CCRegistry::SetString(HKEY hKey, LPCTSTR pPath, LPCTSTR pName, LPCTSTR pValue)
00118 {
00119
00120 HKEY hSubKey = NULL;
00121 if ((dwLastError = RegCreateKeyEx(hKey, pPath, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL)) != ERROR_SUCCESS)
00122 {
00123 eLastErrorOp = CreateKey;
00124 return false;
00125 }
00126
00127
00128 if ((dwLastError = RegSetValueEx(hSubKey, pName, 0, REG_SZ, (LPBYTE)pValue, (DWORD)(_tcslen(pValue) * sizeof(TCHAR)))) != ERROR_SUCCESS)
00129 {
00130 eLastErrorOp = SetValue;
00131 RegCloseKey(hSubKey);
00132 return false;
00133 }
00134
00135
00136 RegCloseKey(hSubKey);
00137 return true;
00138 }
00139
00145 bool CCRegistry::TestWrite(HKEY hKey, LPCTSTR pPath)
00146 {
00147
00148 HKEY hSubKey = NULL;
00149 if ((dwLastError = RegCreateKeyEx(hKey, pPath, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL)) != ERROR_SUCCESS)
00150 {
00151 eLastErrorOp = CreateKey;
00152 return false;
00153 }
00154
00155 RegCloseKey(hSubKey);
00156 return true;
00157 }
00158
00159
00167 bool CCRegistry::SetNumber(HKEY hFromKey, LPCTSTR pPath, LPCTSTR pName, DWORD dwValue)
00168 {
00169
00170 HKEY hKey = NULL;
00171 if ((dwLastError = RegCreateKeyEx(hFromKey, pPath, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL)) != ERROR_SUCCESS)
00172 {
00173 eLastErrorOp = CreateKey;
00174 return false;
00175 }
00176
00177
00178 if ((dwLastError = RegSetValueEx(hKey, pName, 0, REG_DWORD, (LPBYTE)&dwValue, sizeof(dwValue))) != ERROR_SUCCESS)
00179 {
00180 eLastErrorOp = SetValue;
00181 RegCloseKey(hKey);
00182 return false;
00183 }
00184
00185
00186 RegCloseKey(hKey);
00187 return true;
00188 }
00189
00198 DWORD CCRegistry::GetNumber(HKEY hFromKey, LPCTSTR pPath, LPCTSTR pName, bool* pSuccess )
00199 {
00200 HKEY hKey = NULL;
00201
00202 if ((dwLastError = RegOpenKeyEx(hFromKey, pPath, 0, KEY_READ, &hKey)) != ERROR_SUCCESS)
00203 {
00204 eLastErrorOp = OpenKey;
00205 if (pSuccess != NULL)
00206 *pSuccess = false;
00207 return -1;
00208 }
00209
00210
00211 DWORD type;
00212 DWORD nSize = 0;
00213 if (RegQueryValueEx(hKey, pName, NULL, &type, NULL, &nSize) != ERROR_SUCCESS)
00214 {
00215
00216 RegCloseKey(hKey);
00217 if (pSuccess != NULL)
00218 *pSuccess = false;
00219 return -1;
00220 }
00221
00222
00223 DWORD dwRet;
00224 if ((type != REG_DWORD) || (nSize != sizeof(dwRet)))
00225 {
00226
00227 RegCloseKey(hKey);
00228 if (pSuccess != NULL)
00229 *pSuccess = false;
00230 return -1;
00231 }
00232
00233
00234 if (RegQueryValueEx(hKey, pName, NULL, &type, (LPBYTE)&dwRet, &nSize) != ERROR_SUCCESS)
00235 {
00236
00237 RegCloseKey(hKey);
00238 if (pSuccess != NULL)
00239 *pSuccess = false;
00240 return -1;
00241 }
00242
00243
00244 RegCloseKey(hKey);
00245 if (pSuccess != NULL)
00246 *pSuccess = true;
00247 return dwRet;
00248 }
00249
00258 bool CCRegistry::GetBinary(HKEY hFromKey, LPCTSTR pPath, LPCTSTR pName, unsigned char*& pBuffer, int& nBytes)
00259 {
00260 HKEY hKey = NULL;
00261
00262 if ((dwLastError = RegOpenKeyEx(hFromKey, pPath, 0, KEY_READ, &hKey)) != ERROR_SUCCESS)
00263 {
00264 eLastErrorOp = OpenKey;
00265 return false;
00266 }
00267
00268
00269 DWORD type;
00270 DWORD nSize = 0;
00271 if ((dwLastError = RegQueryValueEx(hKey, pName, NULL, &type, NULL, &nSize)) != ERROR_SUCCESS)
00272 {
00273 eLastErrorOp = QueryValueNull;
00274 RegCloseKey(hKey);
00275 return false;
00276 }
00277
00278
00279 if (type != REG_BINARY)
00280 {
00281 eLastErrorOp = WrongType;
00282 dwLastError = type;
00283 RegCloseKey(hKey);
00284 return false;
00285 }
00286
00287 if (nSize == 0)
00288 {
00289 eLastErrorOp = SizeZero;
00290 dwLastError = 0;
00291 RegCloseKey(hKey);
00292 return false;
00293 }
00294
00295 unsigned char* pNewBuf = new unsigned char[nSize];
00296
00297
00298 if ((dwLastError = RegQueryValueEx(hKey, pName, NULL, &type, pNewBuf, &nSize)) != ERROR_SUCCESS)
00299 {
00300 eLastErrorOp = QueryValue;
00301 delete [] pNewBuf;
00302 RegCloseKey(hKey);
00303 return false;
00304 }
00305
00306
00307 RegCloseKey(hKey);
00308
00309 pBuffer = pNewBuf;
00310 nBytes = nSize;
00311 return true;
00312 }
00313
00322 bool CCRegistry::SetBinary(HKEY hKey, LPCTSTR pPath, LPCTSTR pName, unsigned char* pBuffer, int nBytes)
00323 {
00324
00325 HKEY hSubKey = NULL;
00326 if ((dwLastError = RegCreateKeyEx(hKey, pPath, 0, _T(""), REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hSubKey, NULL)) != ERROR_SUCCESS)
00327 {
00328 eLastErrorOp = CreateKey;
00329 return false;
00330 }
00331
00332
00333 if ((dwLastError = RegSetValueEx(hSubKey, pName, 0, REG_BINARY, pBuffer, nBytes)) != ERROR_SUCCESS)
00334 {
00335 eLastErrorOp = SetValue;
00336 RegCloseKey(hKey);
00337 return false;
00338 }
00339
00340
00341 RegCloseKey(hSubKey);
00342 return true;
00343 }