1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
|
// Registry Access class (updated) by David Overton (david@insomniavisions.com).
// These will compile either ANSI or UNICODE.
#include "windows/cregistry.h"
// Returns true if the OS is Windows NT or Windows 2000
bool CRegistry::IsWinNTor2K()
{
OSVERSIONINFO OsVer;
GetVersionEx(&OsVer);
return (OsVer.dwPlatformId == VER_PLATFORM_WIN32_NT)!=0;
}
// Creates a key specified by pszSubKey - you can't create
// keys directly under HKEY_LOCAL_MACHINE in Windows NT or 2000
// just for an extra bit of info.
bool CRegistry::CreateKey(HKEY hKeyRoot, LPCTSTR pszSubKey)
{
HKEY hKey;
DWORD dwFunc;
LONG lRet;
lRet = RegCreateKeyEx(
hKeyRoot,
pszSubKey,
0,
(LPTSTR)NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE,
(LPSECURITY_ATTRIBUTES)NULL,
&hKey,
&dwFunc
);
if(lRet==ERROR_SUCCESS) {
RegCloseKey(hKey);
hKey = (HKEY)NULL;
return true;
}
SetLastError((DWORD)lRet);
return false;
}
bool CRegistry::DeleteKey(HKEY hKeyRoot, LPCTSTR pszSubKey)
{
DWORD dwRet=ERROR_SUCCESS;
if(IsWinNTor2K()) {
// WinNT/2K will not allow you to delete keys which have
// subkeys/values inside them. MS's platform SDK tells you
// to use the SHDeleteKey function in shlwapi.dll. This dll
// is not available on NT platforms without IE 4.0 or later.
// Because of this I first attempt to delete the key in the
// hope that it is empty. If that is not possible I load shlwapi
// and call the function in that. This prevents the app bombing
// out if the dll can't be found.
if(RegDeleteKey(hKeyRoot, pszSubKey)!=ERROR_SUCCESS) {
HINSTANCE hLibInst = LoadLibrary(_T("shlwapi.dll"));
if(!hLibInst) {
throw ERROR_NO_SHLWAPI_DLL;
}
#if defined(UNICODE) || defined(_UNICODE)
SHDELKEYPROC DeleteKeyRecursive = (SHDELKEYPROC)GetProcAddress(hLibInst, "SHDeleteKeyW");
#else
SHDELKEYPROC DeleteKeyRecursive = (SHDELKEYPROC)GetProcAddress(hLibInst, "SHDeleteKeyA");
#endif
if(!DeleteKeyRecursive) {
FreeLibrary(hLibInst);
throw ERROR_NO_SHDELETEKEY;
}
dwRet = DeleteKeyRecursive(hKeyRoot, pszSubKey);
FreeLibrary(hLibInst);
}
}
else {
// Windows 9x will allow RegDeleteKey to delete keys
// even if they have subkeys/values.
dwRet = RegDeleteKey(hKeyRoot, pszSubKey);
}
if(dwRet == ERROR_SUCCESS)
return true;
SetLastError(dwRet);
return false;
}
// Deletes a value from a given subkey and root
bool CRegistry::DeleteValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue)
{
HKEY hKey;
LONG lRes;
if((lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_SET_VALUE, &hKey))!=ERROR_SUCCESS) {
SetLastError((DWORD)lRes);
return false;
}
lRes = RegDeleteValue(hKey, pszValue);
RegCloseKey(hKey);
if(lRes==ERROR_SUCCESS)
return true;
SetLastError(lRes);
return false;
}
// Fetch a binary value. If the size specified by rdwSize is too small, rdwSize will
// be set to the correct size.
bool CRegistry::GetBinaryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, PVOID pBuffer, DWORD& rdwSize)
{
HKEY hKey;
DWORD dwType = REG_BINARY;
DWORD dwSize = rdwSize;
LONG lRes = 0;
if((lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_READ, &hKey))!=ERROR_SUCCESS) {
SetLastError((DWORD)lRes);
return false;
}
lRes = RegQueryValueEx(hKey, pszValue, 0, &dwType, (LPBYTE)pBuffer, &dwSize);
rdwSize = dwSize;
RegCloseKey(hKey);
if(lRes!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
if(dwType!=REG_BINARY) {
throw ERROR_WRONG_TYPE;
}
return true;
}
// Fetch a little endian DWORD from the registry (see platform SDK "Registry Value Types")
bool CRegistry::GetDWORDValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, DWORD &rdwBuff)
{
HKEY hKey;
DWORD dwType = REG_DWORD;
DWORD dwSize = sizeof(DWORD);
DWORD dwValue = 0;
LONG lRes;
rdwBuff = 0;
if((lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_READ, &hKey))!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
lRes = RegQueryValueEx(hKey, pszValue, 0, &dwType, (LPBYTE)&dwValue, &dwSize);
RegCloseKey(hKey);
if(dwType!=REG_DWORD)
throw ERROR_WRONG_TYPE;
if(lRes!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
rdwBuff = dwValue;
return true;
}
// Retrieve a string value. If the given buffer for the string is too small (specified
// by rdwSize), rdwSize is increased to the correct value. If the buffer is bigger than
// the retrieved string, rdwSize is set to the length of the string (in bytes) including
// the terminating null.
bool CRegistry::GetStringValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, LPTSTR pszBuffer, DWORD& rdwSize)
{
HKEY hKey;
DWORD dwType = REG_SZ;
LONG lRes;
DWORD dwBufferSize = rdwSize;
if(!pszBuffer)
throw ERROR_INVALID_BUFFER;
if((lRes=RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_READ, &hKey))!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
lRes = RegQueryValueEx(hKey, pszValue, NULL, &dwType, (unsigned char*)pszBuffer, &dwBufferSize);
RegCloseKey(hKey);
rdwSize = dwBufferSize;
if(dwType!=REG_SZ)
throw ERROR_WRONG_TYPE;
if(lRes!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
return true;
}
// Writes a binary value to the registry
bool CRegistry::SetBinaryValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, PVOID pData, DWORD dwSize)
{
HKEY hKey;
LONG lRes = 0;
if((lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_WRITE, &hKey))!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
lRes = RegSetValueEx(hKey, pszValue, 0, REG_BINARY, reinterpret_cast<BYTE*>(pData), dwSize);
RegCloseKey(hKey);
if(lRes!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
return true;
}
// Writes a DWORD value to the registry
bool CRegistry::SetDWORDValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, DWORD dwValue)
{
HKEY hKey;
LONG lRes;
if((lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_WRITE, &hKey))!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
lRes = RegSetValueEx(hKey, pszValue,0,REG_DWORD,reinterpret_cast<BYTE*>(&dwValue),sizeof(DWORD));
RegCloseKey(hKey);
if(lRes!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
return true;
}
// Writes a string to the registry.
bool CRegistry::SetStringValue(HKEY hKeyRoot, LPCTSTR pszSubKey, LPCTSTR pszValue, LPCTSTR pszString)
{
HKEY hKey;
LONG lRes;
DWORD dwSize = lstrlen(pszString) * sizeof(TCHAR);
if((lRes = RegOpenKeyEx(hKeyRoot, pszSubKey, 0, KEY_WRITE, &hKey))!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
lRes = RegSetValueEx(hKey, pszValue, 0, REG_SZ,
(BYTE*)(pszString), dwSize);
RegCloseKey(hKey);
if(lRes!=ERROR_SUCCESS) {
SetLastError(lRes);
return false;
}
return true;
}
|