// Open key. Create new key if it does not exist.
HKEY hKey = nullptr;
- DWORD action = 0;
- LONG retValReg = RegCreateKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(),
- 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr,
- &hKey, &action);
+ if (m_hKeys.find(strPath) == m_hKeys.end())
+ {
+ DWORD action = 0;
+ LONG retValReg = RegCreateKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(),
+ 0, nullptr, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nullptr,
+ &hKey, &action);
- if (retValReg != ERROR_SUCCESS)
- return COption::OPT_ERR;
+ if (retValReg != ERROR_SUCCESS)
+ return COption::OPT_ERR;
+
+ m_hKeys[strPath] = hKey;
+ }
+ else
+ {
+ hKey = m_hKeys[strPath];
+ }
// Check previous value
// This just checks if the value exists, LoadValueFromReg() below actually
DWORD type = 0;
BYTE dataBuf[MAX_PATH_FULL] = {0};
DWORD size = MAX_PATH_FULL;
- retValReg = RegQueryValueEx(hKey, strValueName.c_str(),
+ LONG retValReg = RegQueryValueEx(hKey, strValueName.c_str(),
0, &type, dataBuf, &size);
// Actually save value into our in-memory options table
retVal = Set(name, value);
}
}
- RegCloseKey(hKey);
+
+ if (m_bCloseHandle)
+ {
+ RegCloseKey(hKey);
+ m_hKeys.erase(strPath);
+ }
return retVal;
}
if (retVal == COption::OPT_OK)
{
- LONG retValReg = RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(),
- 0, KEY_READ, &hKey);
+ LONG retValReg;
+ if (m_hKeys.find(strPath) == m_hKeys.end())
+ {
+ retValReg = RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(),
+ 0, KEY_ALL_ACCESS, &hKey);
+ if (retValReg == ERROR_SUCCESS)
+ m_hKeys[strPath] = hKey;
+ }
+ else
+ {
+ hKey = m_hKeys[strPath];
+ retValReg = ERROR_SUCCESS;
+ }
if (retValReg == ERROR_SUCCESS)
{
retVal = LoadValueFromReg(hKey, name, value);
- RegCloseKey(hKey);
+ if (m_bCloseHandle)
+ {
+ RegCloseKey(hKey);
+ m_hKeys.erase(strPath);
+ }
}
else
retVal = COption::OPT_ERR;
if (retVal == COption::OPT_OK)
{
- LONG retValReg = RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(),
- 0, KEY_WRITE, &hKey);
+ LONG retValReg;
+ if (m_hKeys.find(strPath) == m_hKeys.end())
+ {
+ retValReg = RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(),
+ 0, KEY_ALL_ACCESS, &hKey);
+ if (retValReg == ERROR_SUCCESS)
+ m_hKeys[strPath] = hKey;
+ }
+ else
+ {
+ retValReg = ERROR_SUCCESS;
+ hKey = m_hKeys[strPath];
+ }
if (retValReg == ERROR_SUCCESS)
{
retVal = SaveValueToReg(hKey, strValueName, value);
- RegCloseKey(hKey);
+ if (m_bCloseHandle)
+ {
+ RegCloseKey(hKey);
+ m_hKeys.erase(strPath);
+ }
}
else
retVal = COption::OPT_ERR;
String strRegPath(m_registryRoot);
String strPath;
String strValueName;
+ LONG retValReg;
SplitName(name, strPath, strValueName);
strRegPath += strPath;
- LONG retValReg = RegOpenKey(HKEY_CURRENT_USER, strRegPath.c_str(), &hKey);
+ if (m_hKeys.find(strPath) == m_hKeys.end())
+ {
+ retValReg = RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath.c_str(),
+ 0, KEY_ALL_ACCESS, &hKey);
+ if (retValReg == ERROR_SUCCESS)
+ m_hKeys[strPath] = hKey;
+ }
+ else
+ {
+ hKey = m_hKeys[strPath];
+ retValReg = ERROR_SUCCESS;
+ }
if (retValReg == ERROR_SUCCESS)
{
retValReg = RegDeleteValue(hKey, strValueName.c_str());
{
retVal = COption::OPT_ERR;
}
- RegCloseKey(hKey);
+ if (m_bCloseHandle)
+ {
+ RegCloseKey(hKey);
+ m_hKeys.erase(strPath);
+ }
}
else
retVal = COption::OPT_ERR;
}
return retVal;
}
+
+void CRegOptionsMgr::CloseHandles()
+{
+ for (auto& pair : m_hKeys)
+ RegCloseKey(pair.second);
+ m_hKeys.clear();
+}
if (cmdInfo.m_bNoPrefs)
m_pOptions->SetSerializing(false); // Turn off serializing to registry.
- Options::Init(m_pOptions.get()); // Implementation in OptionsInit.cpp
+ {
+ WMPROFILE(L"Init");
+ Options::Init(m_pOptions.get()); // Implementation in OptionsInit.cpp
+ }
ApplyCommandLineConfigOptions(cmdInfo);
if (cmdInfo.m_sErrorMessages.size() > 0)
{
if (m_bNonInteractive && IsReallyIdle())
m_pMainWnd->PostMessage(WM_CLOSE, 0, 0);
+ static_cast<CRegOptionsMgr *>(GetOptionsMgr())->CloseHandles();
+
return FALSE;
}
pCmdUI->SetText(text.c_str());
pCmdUI->Enable(GetMergingMode());
}
+
+UINT CMergeApp::GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nDefault)
+{
+ COptionsMgr *pOptions = GetOptionsMgr();
+ String name = strutils::format(_T("%s/%s"), lpszSection, lpszEntry);
+ if (!pOptions->Get(name).IsInt())
+ pOptions->InitOption(name, nDefault);
+ return pOptions->GetInt(name);
+}
+
+BOOL CMergeApp::WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry, int nValue)
+{
+ COptionsMgr *pOptions = GetOptionsMgr();
+ String name = strutils::format(_T("%s/%s"), lpszSection, lpszEntry);
+ if (!pOptions->Get(name).IsInt())
+ pOptions->InitOption(name, nValue);
+ return pOptions->SaveOption(name, nValue) == COption::OPT_OK;
+}
+
+CString CMergeApp::GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault)
+{
+ COptionsMgr *pOptions = GetOptionsMgr();
+ String name = strutils::format(_T("%s/%s"), lpszSection, lpszEntry);
+ if (!pOptions->Get(name).IsString())
+ pOptions->InitOption(name, lpszDefault ? lpszDefault : _T(""));
+ return pOptions->GetString(name).c_str();
+}
+
+BOOL CMergeApp::WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszValue)
+{
+ COptionsMgr *pOptions = GetOptionsMgr();
+ if (lpszEntry != nullptr)
+ {
+ String name = strutils::format(_T("%s/%s"), lpszSection, lpszEntry);
+ if (!pOptions->Get(name).IsString())
+ pOptions->InitOption(name, lpszValue ? lpszValue : _T(""));
+ return pOptions->SaveOption(name, lpszValue ? lpszValue : _T("")) == COption::OPT_OK;
+ }
+ return TRUE;
+}