diff options
author | Sebastien Barre <sebastien.barre@kitware.com> | 2002-04-01 18:34:38 (GMT) |
---|---|---|
committer | Sebastien Barre <sebastien.barre@kitware.com> | 2002-04-01 18:34:38 (GMT) |
commit | 92897bf3a8df0def74a3af36015dc3d7ba69aa25 (patch) | |
tree | 027d7efc4785fafc3b73b66b59f40da1b2d3e73c /Source/cmSystemTools.cxx | |
parent | c7e18a0408d6e93b19596e1a6ae99ac08a58eb00 (diff) | |
download | CMake-92897bf3a8df0def74a3af36015dc3d7ba69aa25.zip CMake-92897bf3a8df0def74a3af36015dc3d7ba69aa25.tar.gz CMake-92897bf3a8df0def74a3af36015dc3d7ba69aa25.tar.bz2 |
ENH: add functions to API (read, write, delete registry key value)
Diffstat (limited to 'Source/cmSystemTools.cxx')
-rw-r--r-- | Source/cmSystemTools.cxx | 191 |
1 files changed, 178 insertions, 13 deletions
diff --git a/Source/cmSystemTools.cxx b/Source/cmSystemTools.cxx index 2e330fc..da6ea4c 100644 --- a/Source/cmSystemTools.cxx +++ b/Source/cmSystemTools.cxx @@ -194,17 +194,17 @@ void cmSystemTools::ReplaceString(std::string& source, } } -#if defined(_WIN32) && !defined(__CYGWIN__) - -// Get the data of key value. +// Read a registry value. // Example : // HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath // => will return the data of the "default" value of the key // HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root // => will return the data of the "Root" value of the key -bool ReadAValue(std::string &res, const char *key) + +bool cmSystemTools::ReadRegistryValue(const char *key, std::string &value) { - // find the primary key +#if defined(_WIN32) && !defined(__CYGWIN__) + std::string primary = key; std::string second; std::string valuename; @@ -214,11 +214,13 @@ bool ReadAValue(std::string &res, const char *key) { return false; } + size_t valuenamepos = primary.find(";"); if (valuenamepos != std::string::npos) { valuename = primary.substr(valuenamepos+1); } + second = primary.substr(start+1, valuenamepos-start-1); primary = primary.substr(0, start); @@ -245,8 +247,11 @@ bool ReadAValue(std::string &res, const char *key) } HKEY hKey; - if(RegOpenKeyEx(primaryKey, second.c_str(), - 0, KEY_READ, &hKey) != ERROR_SUCCESS) + if(RegOpenKeyEx(primaryKey, + second.c_str(), + 0, + KEY_READ, + &hKey) != ERROR_SUCCESS) { return false; } @@ -255,19 +260,181 @@ bool ReadAValue(std::string &res, const char *key) DWORD dwType, dwSize; dwSize = 1023; char data[1024]; - if(RegQueryValueEx(hKey, (LPTSTR)valuename.c_str(), NULL, &dwType, - (BYTE *)data, &dwSize) == ERROR_SUCCESS) + if(RegQueryValueEx(hKey, + (LPTSTR)valuename.c_str(), + NULL, + &dwType, + (BYTE *)data, + &dwSize) == ERROR_SUCCESS) { if (dwType == REG_SZ) { - res = data; + value = data; return true; } } } +#endif + return false; +} + + +// Write a registry value. +// Example : +// HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath +// => will set the data of the "default" value of the key +// HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root +// => will set the data of the "Root" value of the key + +bool cmSystemTools::WriteRegistryValue(const char *key, const char *value) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + + std::string primary = key; + std::string second; + std::string valuename; + + size_t start = primary.find("\\"); + if (start == std::string::npos) + { + return false; + } + + size_t valuenamepos = primary.find(";"); + if (valuenamepos != std::string::npos) + { + valuename = primary.substr(valuenamepos+1); + } + + second = primary.substr(start+1, valuenamepos-start-1); + primary = primary.substr(0, start); + + HKEY primaryKey; + if (primary == "HKEY_CURRENT_USER") + { + primaryKey = HKEY_CURRENT_USER; + } + if (primary == "HKEY_CURRENT_CONFIG") + { + primaryKey = HKEY_CURRENT_CONFIG; + } + if (primary == "HKEY_CLASSES_ROOT") + { + primaryKey = HKEY_CLASSES_ROOT; + } + if (primary == "HKEY_LOCAL_MACHINE") + { + primaryKey = HKEY_LOCAL_MACHINE; + } + if (primary == "HKEY_USERS") + { + primaryKey = HKEY_USERS; + } + + HKEY hKey; + DWORD dwDummy; + if(RegCreateKeyEx(primaryKey, + second.c_str(), + 0, + "", + REG_OPTION_NON_VOLATILE, + KEY_WRITE, + NULL, + &hKey, + &dwDummy) != ERROR_SUCCESS) + { + return false; + } + else + { + if(RegSetValueEx(hKey, + (LPTSTR)valuename.c_str(), + 0, + REG_SZ, + (CONST BYTE *)value, + (DWORD)(strlen(value) + 1)) == ERROR_SUCCESS) + { + return true; + } + } +#endif return false; } + + +// Delete a registry value. +// Example : +// HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.1\InstallPath +// => will delete the data of the "default" value of the key +// HKEY_LOCAL_MACHINE\SOFTWARE\Scriptics\Tcl\8.4;Root +// => will delete the data of the "Root" value of the key + +bool cmSystemTools::DeleteRegistryValue(const char *key) +{ +#if defined(_WIN32) && !defined(__CYGWIN__) + + std::string primary = key; + std::string second; + std::string valuename; + + size_t start = primary.find("\\"); + if (start == std::string::npos) + { + return false; + } + + size_t valuenamepos = primary.find(";"); + if (valuenamepos != std::string::npos) + { + valuename = primary.substr(valuenamepos+1); + } + + second = primary.substr(start+1, valuenamepos-start-1); + primary = primary.substr(0, start); + + HKEY primaryKey; + if (primary == "HKEY_CURRENT_USER") + { + primaryKey = HKEY_CURRENT_USER; + } + if (primary == "HKEY_CURRENT_CONFIG") + { + primaryKey = HKEY_CURRENT_CONFIG; + } + if (primary == "HKEY_CLASSES_ROOT") + { + primaryKey = HKEY_CLASSES_ROOT; + } + if (primary == "HKEY_LOCAL_MACHINE") + { + primaryKey = HKEY_LOCAL_MACHINE; + } + if (primary == "HKEY_USERS") + { + primaryKey = HKEY_USERS; + } + + HKEY hKey; + if(RegOpenKeyEx(primaryKey, + second.c_str(), + 0, + KEY_WRITE, + &hKey) != ERROR_SUCCESS) + { + return false; + } + else + { + if(RegDeleteValue(hKey, + (LPTSTR)valuename.c_str()) == ERROR_SUCCESS) + { + return true; + } + } #endif + return false; +} + // replace replace with with as many times as it shows up in source. // write the result into source. @@ -288,7 +455,7 @@ void cmSystemTools::ExpandRegistryValues(std::string& source) // the arguments are the second match std::string key = regEntry.match(1); std::string val; - if (ReadAValue(val,key.c_str())) + if (ReadRegistryValue(key.c_str(), val)) { std::string reg = "["; reg += key + "]"; @@ -305,8 +472,6 @@ void cmSystemTools::ExpandRegistryValues(std::string& source) } - - std::string cmSystemTools::EscapeQuotes(const char* str) { std::string result = ""; |