bool CKNSettings::setPass(string strPass) { bool bReturn = true; m_strPass = strPass; // If we're using the registry, write the passed string to the Pass subkey if(m_bUseRegistry) { // If the key is open, close it. if(m_hKey) CloseKey(); // Opening the key for writing, so we pass false for the ForRead value. if(OpenKey(false)) { bReturn = SetRegValue("Pass", strPass); CloseKey(); } } return bReturn; }
int RegReadBytes(TCHAR *valuename, byte *value, int iSize) { static byte dwResult; LONG rc; DWORD dwType=REG_BINARY; DWORD dwSize=sizeof(byte); if (g_hkey==NULL) rc = OpenKey(); if (g_hkey != NULL) { //query the size of the data rc = RegQueryValueEx(g_hkey, valuename, NULL, &dwType, NULL, &dwSize); if (rc == ERROR_SUCCESS) { iSize = dwSize; byte* bResult=new byte[dwSize]; rc = RegQueryValueEx(g_hkey, valuename, NULL, &dwType, bResult, &dwSize); if (rc == ERROR_SUCCESS) { CloseKey(); memcpy(value, bResult, 20); delete bResult; return rc; } } } CloseKey(); return rc; }
bool CKNSettings::setPort(int iPort) { bool bReturn = true; m_iPort = iPort; // If we're using the registry, write the passed int to the Port subkey if(m_bUseRegistry) { // If the key is open, close it. if(m_hKey) CloseKey(); // Opening the key for writing, so we pass false for the ForRead value. if(OpenKey(false)) { bReturn = SetRegValueNum("Port", iPort); CloseKey(); } } return bReturn; }
/*lint -e774 */ bool RegKey::GetKeyValue(LPCTSTR pszKeyName, LPCTSTR pszValueName, BYTE& pValue, DWORD dwValueLength , bool bActive /* = false */, HKEY hBaseKey /* = HKEY_CURRENT_USER */, LPCTSTR pszMachineName /* = NULL */) { if ((pszKeyName == NULL) || (pszValueName == NULL) || (&pValue == NULL)) { iLastErrorCode_ = ERROR_BAD_ARGUMENTS; return false; } CloseKey(); // Close current active key and base key if remote // Open the key if (!OpenKey(pszKeyName, false, hBaseKey, pszMachineName)) return false; if (!GetValue(pszValueName, pValue, dwValueLength)) { if (!bActive) CloseKey(); return false; } if (!bActive) CloseKey(); // Close current active key and base key if remote iLastErrorCode_ = ERROR_SUCCESS; return true; }
// Opens a key, deletes a value from the key and makes key active if required bool RegKey::DeleteKeyValue(LPCTSTR pszKeyName, LPCTSTR pszValueName, bool bActive /* = false */, HKEY hBaseKey /* = HKEY_CURRENT_USER */, LPCTSTR pszMachineName /* = NULL */) { if (pszValueName == NULL || pszKeyName == NULL) { iLastErrorCode_ = ERROR_BAD_ARGUMENTS; return false; } CloseKey(); // Close current active key and base key if remote // Open the key if (!OpenKey(pszKeyName, false, hBaseKey, pszMachineName)) return false; if (!DeleteValue(pszValueName)) { if (!bActive) CloseKey(); return false; } if (!bActive) CloseKey(); // Close current active key and base key if remote iLastErrorCode_ = ERROR_SUCCESS; return true; }
bool RegKey::SetKeyValue(LPCTSTR pszKeyName, LPCTSTR pszValueName, const BYTE* pValue, DWORD dwValueLength , bool bCreateIfNoExist /* = false */, bool bActive /* = false */ , HKEY hBaseKey /* = HKEY_CURRENT_USER */, LPCTSTR pszMachineName /* = NULL */) { if (pszValueName == NULL || pValue == NULL || pszKeyName == NULL) { iLastErrorCode_ = ERROR_BAD_ARGUMENTS; return false; } CloseKey(); // Close current active key and base key if remote // Open / create the key if (!OpenKey(pszKeyName, bCreateIfNoExist, hBaseKey, pszMachineName)) return false; if (!SetValue(pszValueName, (BYTE*)pValue, dwValueLength)) { if (!bActive) CloseKey(); return false; } if (!bActive) CloseKey(); // Close current active key and base key if remote iLastErrorCode_ = ERROR_SUCCESS; return true; }
// Delete a Key bool RegKey::QuickDeleteKey(LPCTSTR pszKeyName, HKEY hBaseKey /* = HKEY_CURRENT_USER */) { if (pszKeyName == NULL) { iLastErrorCode_ = ERROR_BAD_ARGUMENTS; return false; } CloseKey(); // Close current active key and base key if remote TCHAR* pBackSlash; bool bRetValue = true; // You cannot delete a key if given a full path e.g. "A\\B" // We have to go back up a level to "A" and then delete // Take a copy of the keyName TCHAR* pszCopy = _tcsdup(pszKeyName); pBackSlash = _tcsrchr(pszCopy, _T('\\')); if (pBackSlash == NULL) // Not found { // No path, so specified key is just a subkey of active key bRetValue = RecursiveDelete(this, hBaseKey, pszKeyName); } else // Path specified { LPTSTR pszChildKeyName = pBackSlash + 1; *(pBackSlash) = _T('\0'); // Null terminate the parent string LPTSTR pszParentKey = (TCHAR*)pszCopy; HKEY hTempKey; LONG retValue = RegOpenKeyEx(hBaseKey, pszParentKey, NULL, KEY_ALL_ACCESS, &hTempKey); if (retValue == ERROR_SUCCESS) { bRetValue = RecursiveDelete(this, hTempKey, pszChildKeyName); RegCloseKey(hTempKey); } else { free(pszCopy); pszCopy = NULL; iLastErrorCode_ = GetLastError(); return false; } } free(pszCopy); if (!bRetValue) return false; CloseKey(); // Close current active key and base key if remote iLastErrorCode_ = ERROR_SUCCESS; return true; }
BOOL TRegistry::DeleteChildTreeW(const WCHAR *subKey) { WCHAR wbuf[256]; BOOL ret = TRUE; if (subKey && !OpenKeyW(subKey)) { return FALSE; } while (EnumKeyW(0, wbuf, wsizeof(wbuf))) { if (!(ret = DeleteChildTreeW(wbuf))) break; } if (subKey) { CloseKey(); ret = DeleteKeyW(subKey) ? ret : FALSE; } else { while (EnumValueW(0, wbuf, wsizeof(wbuf))) { if (!DeleteValueW(wbuf)) { ret = FALSE; break; } } } return ret; }
bool CKNSettings::GetSettingsFromRegistry() { // If opening the key for reading succeeds, get the registry values for // Server, Path, User, and Pass, and store them in their appropriate // variables. if(OpenKey(true)) { if(GetRegValue("Server", m_strServer) && GetRegValueNum("Port", (DWORD&) m_iPort) && GetRegValue("Path", m_strPath) && GetRegValue("User", m_strUser) && GetRegValue("Pass", m_strPass)) { // If getting all of the above values succeeds, close the key CloseKey(); return true; } } return false; }
bool RegUtils::IsRegKeyPresent(const HKEY& hRootHive, const CStdString& sSourceKeyName) { HKEY hKey = NULL; bool bPresent = RegOpenKeyEx(hRootHive, sSourceKeyName, 0, KEY_READ, &hKey) == ERROR_SUCCESS; CloseKey(hKey); return bPresent; }
/* subKey を指定した場合は subkey を含むキー以下を削除 subkey が NULL の場合、カレント の配下を削除 */ BOOL TRegistry::DeleteChildTree(LPSTR subKey) { char buf[100]; BOOL ret = TRUE; if (subKey != NULL && OpenKey(subKey) != TRUE) return FALSE; while (EnumKey(0, buf, sizeof(buf))) { if ((ret = DeleteChildTree(buf)) != TRUE) break; } if (subKey != NULL) { CloseKey(); ret = DeleteKey(subKey) ? ret : FALSE; } else { while (EnumValue(0, buf, sizeof(buf))) { if (DeleteValue(buf) != TRUE) { ret = FALSE; break; } } } return ret; }
// Restores a saved registry tree from the specified file to the specified key position bool RegKey::RestoreRegistry(LPCTSTR pszFileName, LPCTSTR pszKeyName, HKEY hBaseKey /* = HKEY_CURRENT_USER */, LPCTSTR pszMachineName /* = NULL */) { if (pszFileName == NULL || pszKeyName == NULL) { iLastErrorCode_ = ERROR_BAD_ARGUMENTS; return false; } CloseKey(); // Close current active key and base key if remote // Open the key if (!OpenKey(pszKeyName, true, hBaseKey, pszMachineName)) return false; LONG lRetValue = RegRestoreKey(hTheKey_, pszFileName, REG_OPTION_NON_VOLATILE); if (lRetValue == ERROR_CALL_NOT_IMPLEMENTED) // NT ONLY { iLastErrorCode_ = ERROR_CALL_NOT_IMPLEMENTED; return false; } if (lRetValue != ERROR_SUCCESS) { iLastErrorCode_ = GetLastError(); return false; } iLastErrorCode_ = ERROR_SUCCESS; return true; }
// Saves a registry tree into the specified file/path from the specified key position bool RegKey::SaveRegistry(LPCTSTR pszFileName, LPCTSTR pszKeyName, HKEY hBaseKey /* = HKEY_CURRENT_USER */, LPCTSTR pszMachineName /* = NULL */) { if (pszFileName == NULL || pszKeyName == NULL) { iLastErrorCode_ = ERROR_BAD_ARGUMENTS; return false; } CloseKey(); // Close current active key and base key if remote // Open the key if (!OpenKey(pszKeyName, false, hBaseKey, pszMachineName)) return false; LONG lRetValue = RegSaveKey(hTheKey_, pszFileName, NULL); if (lRetValue == ERROR_ALREADY_EXISTS || lRetValue == ERROR_REGISTRY_IO_FAILED) // Win NT / Win 95 { iLastErrorCode_ = ERROR_ALREADY_EXISTS; return false; } if (lRetValue != ERROR_SUCCESS) { iLastErrorCode_ = GetLastError(); return false; } iLastErrorCode_ = ERROR_SUCCESS; return true; }
void TRegistry::ChangeTopKey(HKEY top_key) { while (openCnt > 0) CloseKey(); topKey = top_key; }
//////////////////////////////////////////////////////////////////////////////////////////////// // Create a registry key //////////////////////////////////////////////////////////////////////////////////////////////// void CRegistry::CreateKey(HKEY MainKey, LPCTSTR SubKey) { HKEY hKey = NULL; DWORD Disposition = 0; RegCreateKeyEx(MainKey, SubKey, 0L, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &Disposition); CloseKey(hKey); }
layer_c::~layer_c() { DeleteValue(value); if (strlen(data) > 0) { SetValue(value,data); } CloseKey(); }
bool RegUtils::HaveWriteAccessToKey(const HKEY& hRootKey, const CStdString& sSubKey) { HKEY hKey; if (RegOpenKeyEx(hRootKey, sSubKey, 0, KEY_WRITE, &hKey)!=ERROR_SUCCESS) return false; CloseKey(hKey); return true; }
bool RegUtils::CreateKey(const HKEY& hRootKey, const CStdString& sName) { HKEY hKey; if (::RegCreateKey(hRootKey, sName.c_str(), &hKey) == ERROR_SUCCESS) { return CloseKey(hKey); } return false; }
bool RegUtils::SetRegStringValue(HKEY hKeyRoot, CStdString sSubKey, CStdString sValueName, CStdString sValue) { HKEY hKey; if (RegOpenKeyEx(hKeyRoot, sSubKey, 0, KEY_WRITE, &hKey) != ERROR_SUCCESS) { return false; } if (RegSetValueEx(hKey, sValueName, 0, REG_SZ, (BYTE*)sValue.GetBuffer(), (DWORD)sValue.length()*sizeof(TCHAR)) != ERROR_SUCCESS) { sValue.ReleaseBuffer(); CloseKey(hKey); return false; } sValue.ReleaseBuffer(); CloseKey(hKey); return true; }
CStdString RegUtils::GetStringValueFromRegistry(const HKEY& hKeyRoot, const CStdString& sSubKey, const CStdString& sValueName) { HKEY hKey; if (RegOpenKeyEx(hKeyRoot, sSubKey, 0, KEY_READ, &hKey) != ERROR_SUCCESS) { return _T(""); } CStdString sValue = GetStringValueFromRegistry(hKey, sValueName); CloseKey(hKey); return sValue; }
//RegReadDword int RegReadDword(TCHAR *valuename, DWORD *value) { static DWORD dwResult; LONG rc; DWORD dwType=REG_DWORD; DWORD dwSize=sizeof(DWORD); if (g_hkey==NULL) rc = OpenKey(); if (g_hkey != NULL) { rc = RegQueryValueEx(g_hkey, valuename, NULL, &dwType, (LPBYTE) value, &dwSize); if (rc == ERROR_SUCCESS) { CloseKey(); //*value = dwResult; return rc; } } CloseKey(); return rc; }
int RegReadByte(TCHAR *valuename, byte *value) { static byte dwResult; LONG rc; DWORD dwType=REG_BINARY; DWORD dwSize=sizeof(byte); if (g_hkey==NULL) rc = OpenKey(); if (g_hkey != NULL) { rc = RegQueryValueEx(g_hkey, valuename, NULL, &dwType, &dwResult, &dwSize); if (rc == ERROR_SUCCESS) { CloseKey(); *value = dwResult; return rc; } } CloseKey(); return rc; }
QString FhoReg::GetKeyDefaultValue(HKEY parentKey, QString &parentSubKeyName) { HKEY k = OpenKey(parentKey, parentSubKeyName, KEY_QUERY_VALUE); QString value; if (k != 0) { value = GetKeyDefaultValue(k); CloseKey(k); } return value; }
void TRegistry::SetRootKey(HKEY ARootKey) { if (FRootKey != ARootKey) { if (FCloseRootKey) { RegCloseKey(GetRootKey()); FCloseRootKey = false; } FRootKey = ARootKey; CloseKey(); } }
BOOL TRegistry::ChangeApp(LPCSTR company, LPSTR appName) { while (openCnt > 0) CloseKey(); topKey = HKEY_CURRENT_USER; char buf[100]; wsprintf(buf, "software\\%s", company); if (appName != NULL && *appName) wsprintf(buf + strlen(buf), "\\%s", appName); return CreateKey(buf); }
bool RegUtils::DeleteValue(HKEY hRootKey, CStdString sSubKey, CStdString sValueName) { HKEY hKey; if (RegOpenKeyEx(hRootKey, sSubKey, 0, KEY_WRITE, &hKey)!=ERROR_SUCCESS) return false; LONG lRetCode = RegDeleteValue(hKey, sValueName); CloseKey(hKey); if (lRetCode == ERROR_SUCCESS) return true; return !IsRegValuePresent(hRootKey, sSubKey, sValueName); }
void SettingsXML::CloseStorage() { HRESULT hr = S_OK; HANDLE hFile = NULL; bool bCanSave = false; CloseKey(); if (mb_Modified && mp_File) { // Путь к файлу проинициализирован в OpenKey _ASSERTE(m_Storage.pszFile && *m_Storage.pszFile); LPCWSTR pszXmlFile = m_Storage.pszFile; if (pszXmlFile) { hFile = CreateFile(pszXmlFile, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0); // XML-файл отсутсвует, или ошибка доступа if (hFile == INVALID_HANDLE_VALUE) { DWORD dwErrCode = GetLastError(); wchar_t szErr[MAX_PATH*2]; _wsprintf(szErr, SKIPLEN(countof(szErr)) L"Can't open file for writing!\n%s\nErrCode=0x%08X", pszXmlFile, dwErrCode); MBoxA(szErr); } else { CloseHandle(hFile); hFile = NULL; bCanSave = true; } if (bCanSave) { VARIANT vt; vt.vt = VT_BSTR; vt.bstrVal = ::SysAllocString(pszXmlFile); hr = mp_File->save(vt); VariantClear(&vt); } } } SafeRelease(mp_File); mb_Modified = false; mb_DataChanged = false; mb_Empty = false; mn_access = 0; }
//RegReadStr int RegReadStr(TCHAR *valuename, TCHAR *value) { static TCHAR szStr[MAX_PATH+1]; LONG rc; DWORD dwType=REG_SZ; DWORD dwSize=0; if (g_hkey == NULL) { if (OpenKey()==0) //use default g_hkey { dwSize = sizeof(szStr) * sizeof(TCHAR); rc = RegQueryValueEx(g_hkey, valuename, NULL, &dwType, (LPBYTE)szStr, &dwSize); if (rc == ERROR_SUCCESS) { CloseKey(); wcscpy(value, szStr); return 0; } } } else { //use already opened g_hkey dwSize = sizeof(szStr) * sizeof(TCHAR); rc = RegQueryValueEx(g_hkey, valuename, NULL, &dwType, (LPBYTE)szStr, &dwSize); if (rc == ERROR_SUCCESS) { CloseKey(); wcscpy(value, szStr); return 0; } } wcscpy(value, L""); CloseKey(); return -1; }
QStringList* FhoReg::EnumSubKeys(HKEY parentKey, QString &parentSubKeyName) { QStringList *resultList; HKEY k = OpenKey(parentKey, parentSubKeyName, KEY_ENUMERATE_SUB_KEYS); if (k != 0) { resultList = EnumSubKeys(k); CloseKey(k); } else { resultList = new QStringList(); } return resultList; }
bool RegUtils::RecursiveCopyKey(const HKEY& hRootHive, const CStdString& sSourceKeyName, const CStdString& sTargetKeyName) { HKEY hSourceKey = NULL; HKEY hTargetKey = NULL; long lRetVal = 0; bool bRet = true; if (sSourceKeyName.IsEmpty() || sTargetKeyName.IsEmpty()) { LOG_WS_ERROR(_T("Empty source or target key specified")); return false; } if (((lRetVal = RegOpenKey(hRootHive, sSourceKeyName, &hSourceKey)) != ERROR_SUCCESS)) { CStdString sErr; sErr.Format(_T("Failed to open source key %s, error %d"), sSourceKeyName.c_str(), lRetVal); LOG_WS_ERROR(sErr.c_str()); return false; } CopyKey(hRootHive, hSourceKey, sTargetKeyName); TCHAR szKeyName[MAX_PATH]; for (DWORD i = 0, retcode = ERROR_SUCCESS; retcode == ERROR_SUCCESS; ++i) { DWORD dwBuffSize = MAX_PATH; ZeroMemory(szKeyName, MAX_PATH); retcode = RegEnumKeyEx(hSourceKey, i, szKeyName, &dwBuffSize, NULL, NULL, NULL, NULL); if (retcode == ERROR_SUCCESS) { RecursiveCopyKey(hRootHive, sSourceKeyName + _T("\\") + szKeyName, sTargetKeyName + _T("\\") + szKeyName); } else if (retcode != ERROR_NO_MORE_ITEMS) { CStdString sErr; sErr.Format(_T("Failed to enumerate source key %s, error %d"), sSourceKeyName.c_str(), retcode); LOG_WS_ERROR(sErr.c_str()); bRet = false; } } CloseKey(hSourceKey); return bRet; }