fix(config): stop reads from creating empty registry subkeys
iniFile/binFile::GetCachedKey used RegCreateKeyExA for every access, so a plain GetStr/GetInt on a missing section materialised an empty subkey. CScreenManager's constructor calls LoadQualityProfiles() unconditionally, which reads profile0.. profile5 x4 keys, so every client start left six zero-valued subkeys behind: 126 of them on the dev box, all with zero values. The server's HKCU\SoftwareYAMA\quality key is the same artefact. Split the helper by intent: writes go through RegCreateKeyExA, reads through RegOpenKeyExA. Both still ask for KEY_READ|KEY_WRITE, because the handle cache is shared and a read-only cached handle would make a later SetStr on the same section fail. Also fixes a second bug in SaveQualityProfiles: when a level matched the default it simply continued, so a level the operator reset to default kept the old custom values in the registry and the reset never took effect. Add SectionExists() (open-only, never creates) and write the defaults back when the section already exists.
This commit is contained in:
1 parent
454d25d37f
commit
8b92f32c47
2 files changed
+55
-17
No files matched your search
@@ -2380,6 +2380,8 @@ void CScreenManager::SwitchToNextWindow()
|
||||
|
||||
void CScreenManager::LoadQualityProfiles()
|
||||
{
|
||||
// 读路径不会创建子键(iniFile 的读取走 RegOpenKeyEx),所以没有下发过质量等级
|
||||
// 的机器不会凭空出现空的 profile0~profile5。别为了“性能”改成先写后读。
|
||||
iniFile cfg(CLIENT_PATH);
|
||||
for (int i = 0; i < QUALITY_COUNT; i++) {
|
||||
char section[32];
|
||||
@@ -2399,13 +2401,19 @@ void CScreenManager::SaveQualityProfiles()
|
||||
for (int i = 0; i < QUALITY_COUNT; i++) {
|
||||
const QualityProfile& def = GetQualityProfile(i);
|
||||
const QualityProfile& cur = m_QualityProfiles[i];
|
||||
// 只有与默认值不同时才保存
|
||||
if (cur.maxFPS == def.maxFPS && cur.maxWidth == def.maxWidth &&
|
||||
cur.algorithm == def.algorithm && cur.bitRate == def.bitRate) {
|
||||
continue;
|
||||
}
|
||||
char section[32];
|
||||
sprintf(section, "profile%d", i);
|
||||
|
||||
// 与默认值一致的档位不必落盘——主控每次开远程桌面都会整表下发,
|
||||
// 全量写会把 6 个节全部建出来,用户没改过任何档位也留一堆默认值。
|
||||
if (cur.maxFPS == def.maxFPS && cur.maxWidth == def.maxWidth &&
|
||||
cur.algorithm == def.algorithm && cur.bitRate == def.bitRate) {
|
||||
// 但**已存在**的节必须把默认值写回去:否则主控把某档改回默认后,
|
||||
// 客户端还留着上次存的自定义值,重置永远不生效。SectionExists
|
||||
// 只查不建,不会因为这条分支又凭空造出空键。
|
||||
if (!cfg.SectionExists(section))
|
||||
continue;
|
||||
}
|
||||
cfg.SetInt(section, "maxFPS", cur.maxFPS);
|
||||
cfg.SetInt(section, "maxWidth", cur.maxWidth);
|
||||
cfg.SetInt(section, "algorithm", cur.algorithm);
|
||||
|
||||
+42
-12
@@ -323,8 +323,14 @@ private:
|
||||
// 注册表键句柄缓存,避免频繁 RegOpenKeyEx/RegCloseKey
|
||||
mutable std::map<std::string, HKEY> m_keyCache;
|
||||
|
||||
// 获取缓存的键句柄,如果不存在则打开并缓存
|
||||
HKEY GetCachedKey(const std::string& mainKey) const
|
||||
// 获取缓存的键句柄
|
||||
// create=true :不存在就建(只有写入才该这么做)
|
||||
// create=false :只打开、绝不创建。纯读取把一个不存在的节凭空写成空键,
|
||||
// 会在注册表里留下一堆没有任何值的子键(典型受害者:客户端
|
||||
// LoadQualityProfiles 无条件读 profile0~profile5)。
|
||||
// 两条路径都申请 KEY_READ|KEY_WRITE:缓存里可能先存下读取时打开的句柄,
|
||||
// 若只给 KEY_READ,后续对同一节的 SetStr 会因权限不足失败。
|
||||
HKEY GetCachedKey(const std::string& mainKey, bool create) const
|
||||
{
|
||||
std::string fullPath = m_SubKeyPath + "\\" + mainKey;
|
||||
|
||||
@@ -334,8 +340,12 @@ private:
|
||||
}
|
||||
|
||||
HKEY hKey = NULL;
|
||||
if (RegCreateKeyExA(m_hRootKey, fullPath.c_str(), 0, NULL, 0,
|
||||
KEY_READ | KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
|
||||
LONG rc = create
|
||||
? RegCreateKeyExA(m_hRootKey, fullPath.c_str(), 0, NULL, 0,
|
||||
KEY_READ | KEY_WRITE, NULL, &hKey, NULL)
|
||||
: RegOpenKeyExA(m_hRootKey, fullPath.c_str(), 0,
|
||||
KEY_READ | KEY_WRITE, &hKey);
|
||||
if (rc == ERROR_SUCCESS) {
|
||||
m_keyCache[fullPath] = hKey;
|
||||
return hKey;
|
||||
}
|
||||
@@ -379,7 +389,7 @@ public:
|
||||
// 写入字符串(使用缓存的键句柄)
|
||||
bool SetStr(const std::string& MainKey, const std::string& SubKey, const std::string& Data) override
|
||||
{
|
||||
HKEY hKey = GetCachedKey(MainKey);
|
||||
HKEY hKey = GetCachedKey(MainKey, true);
|
||||
if (!hKey)
|
||||
return false;
|
||||
|
||||
@@ -392,7 +402,7 @@ public:
|
||||
// (PEM 正文、base64 证书等)截断成空,长值一律走 ReadRegistryValueSz。
|
||||
std::string GetStr(const std::string& MainKey, const std::string& SubKey, const std::string& def = "") override
|
||||
{
|
||||
HKEY hKey = GetCachedKey(MainKey);
|
||||
HKEY hKey = GetCachedKey(MainKey, false);
|
||||
if (!hKey)
|
||||
return def;
|
||||
|
||||
@@ -427,6 +437,21 @@ public:
|
||||
m_keyCache.clear();
|
||||
}
|
||||
|
||||
// 只判断子键是否存在,绝不创建。给「配置回退到默认值、需要清掉残留」的场景用:
|
||||
// 直接调 SetInt 写默认值会把不存在的节建出来,正好又变成空键。
|
||||
bool SectionExists(const std::string& MainKey) const
|
||||
{
|
||||
std::string fullPath = m_SubKeyPath + "\\" + MainKey;
|
||||
if (m_keyCache.find(fullPath) != m_keyCache.end())
|
||||
return true;
|
||||
|
||||
HKEY hKey = NULL;
|
||||
if (RegOpenKeyExA(m_hRootKey, fullPath.c_str(), 0, KEY_READ, &hKey) != ERROR_SUCCESS)
|
||||
return false;
|
||||
RegCloseKey(hKey);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 枚举 m_SubKeyPath 下的所有子键名称
|
||||
// suffix: 只返回以该后缀结尾的键名,默认为空表示返回所有键
|
||||
std::vector<std::string> EnumSubKeys(const std::string& suffix = "") const
|
||||
@@ -478,7 +503,7 @@ public:
|
||||
{
|
||||
std::vector<std::string> result;
|
||||
|
||||
HKEY hKey = GetCachedKey(MainKey);
|
||||
HKEY hKey = GetCachedKey(MainKey, false);
|
||||
if (!hKey)
|
||||
return result;
|
||||
|
||||
@@ -521,7 +546,8 @@ private:
|
||||
mutable std::map<std::string, HKEY> m_keyCache;
|
||||
|
||||
// 获取缓存的键句柄
|
||||
HKEY GetCachedKey(const std::string& mainKey) const
|
||||
// create 语义同 iniFile::GetCachedKey:读取路径不得创建子键。
|
||||
HKEY GetCachedKey(const std::string& mainKey, bool create) const
|
||||
{
|
||||
std::string fullPath = m_SubKeyPath + "\\" + mainKey;
|
||||
|
||||
@@ -531,8 +557,12 @@ private:
|
||||
}
|
||||
|
||||
HKEY hKey = NULL;
|
||||
if (RegCreateKeyExA(m_hRootKey, fullPath.c_str(), 0, NULL, 0,
|
||||
KEY_READ | KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
|
||||
LONG rc = create
|
||||
? RegCreateKeyExA(m_hRootKey, fullPath.c_str(), 0, NULL, 0,
|
||||
KEY_READ | KEY_WRITE, NULL, &hKey, NULL)
|
||||
: RegOpenKeyExA(m_hRootKey, fullPath.c_str(), 0,
|
||||
KEY_READ | KEY_WRITE, &hKey);
|
||||
if (rc == ERROR_SUCCESS) {
|
||||
m_keyCache[fullPath] = hKey;
|
||||
return hKey;
|
||||
}
|
||||
@@ -614,7 +644,7 @@ public:
|
||||
private:
|
||||
bool SetBinary(const std::string& MainKey, const std::string& SubKey, const BYTE* data, DWORD size)
|
||||
{
|
||||
HKEY hKey = GetCachedKey(MainKey);
|
||||
HKEY hKey = GetCachedKey(MainKey, true);
|
||||
if (!hKey)
|
||||
return false;
|
||||
|
||||
@@ -623,7 +653,7 @@ private:
|
||||
|
||||
bool GetBinary(const std::string& MainKey, const std::string& SubKey, std::vector<BYTE>& outData) const
|
||||
{
|
||||
HKEY hKey = GetCachedKey(MainKey);
|
||||
HKEY hKey = GetCachedKey(MainKey, false);
|
||||
if (!hKey)
|
||||
return false;
|
||||
|
||||
|
||||
Reference in new issue
Block a user