Win32注册表增删改查操作
class CRegMgr
{
public:
CRegMgr(const std::wstring& wAppName = L"AnyDouYin") :m_wAppName(wAppName) {}
public:
const std::vector<std::wstring> m_vKeys = { L"Key", L"Value", };
std::wstring m_wAppName = L"AnyDouYin";
HKEY hKey()
{
return HKEY_CURRENT_USER;
}
std::wstring subKey()
{
return L"SOFTWARE\\" + m_wAppName;
}
std::wstring subKeyList()
{
return subKey() + L"\\List";
}
std::wstring GetNextItemId()
{
std::vector<std::wstring> tsvKey = {};
RegHelper::RegEnumKeys(tsvKey, subKeyList().c_str(), hKey());
for (size_t i = 0; i < tsvKey.size(); i++)
{
if (tsvKey.at(i).compare(std::to_wstring(i)) != 0)
{
return std::to_wstring(i);
}
}
return std::to_wstring(tsvKey.size());
}
BOOL FindSame(const std::vector<std::wstring>& vFind)
{
std::vector<std::wstring> tsvKey = {};
RegHelper::RegEnumKeys(tsvKey, subKeyList().c_str(), hKey());
for (auto& it : tsvKey)
{
std::vector<DWORD> vStringLength(vFind.size(), MAX_PATH);
std::vector<std::wstring> vString(vFind.size(), std::wstring(MAX_PATH, L'\0'));
for (std::size_t n = 0, m = 0; n < vString.size(); n++)
{
RegHelper::RegGetString(vString[n].data(), &vStringLength[n], m_vKeys[n].c_str(), (subKeyList() + std::wstring((L"\\")) + it).c_str(), hKey());
if (vFind[n].compare(vString[n].c_str()) == 0 && (++m) == vString.size())
{
return TRUE;
}
}
}
return FALSE;
}
std::wstring FindConfig(const std::vector<std::wstring>& vFind)
{
std::vector<std::wstring> tsvKey = {};
RegHelper::RegEnumKeys(tsvKey, subKeyList().c_str(), hKey());
for (auto& it : tsvKey)
{
std::vector<DWORD> vStringLength(vFind.size(), MAX_PATH);
std::vector<std::wstring> vString(vFind.size(), std::wstring(MAX_PATH, L'\0'));
for (std::size_t n = 0, m = 0; n < vString.size(); n++)
{
RegHelper::RegGetString(vString[n].data(), &vStringLength[n], m_vKeys[n].c_str(), (subKeyList() + std::wstring((L"\\")) + it).c_str(), hKey());
if (vFind[n].compare(vString[n].c_str()) == 0 && (++m) == vString.size())
{
return it;
}
}
}
return (L"");
}
BOOL AddConfig(const std::vector<std::wstring>& vData)
{
if (FindSame(vData) == FALSE)
{
std::wstring tNextItemId = subKeyList() + std::wstring((L"\\")) + GetNextItemId();
for (std::size_t n = 0; n < vData.size(); n++)
{
RegHelper::RegSetString(vData[n].c_str(), m_vKeys[n].c_str(), tNextItemId.c_str(), hKey());
}
return TRUE;
}
return FALSE;
}
BOOL DelConfig(int nItem, const std::vector<std::wstring>& vData)
{
std::wstring strKey = FindConfig(vData);
if (strKey.empty() == false)
{
return (RegHelper::RegRemoveKey((subKeyList() + std::wstring((L"\\")) + strKey).c_str(), hKey()));
}
return FALSE;
}
void LoadConfig(std::vector<std::vector<std::wstring>>& vvStringList)
{
std::vector<std::wstring> tsvKey = {};
RegHelper::RegEnumKeys(tsvKey, subKeyList().c_str(), hKey());
for (auto& it : tsvKey)
{
std::vector<DWORD> vStringLength(m_vKeys.size(), MAX_PATH);
std::vector<std::wstring> vString(m_vKeys.size(), std::wstring(MAX_PATH, L'\0'));
for (std::size_t n = 0; n < vString.size(); n++)
{
RegHelper::RegGetString(vString[n].data(), &vStringLength[n], m_vKeys[n].c_str(), (subKeyList() + std::wstring((L"\\")) + it).c_str(), hKey());
}
vvStringList.emplace_back(vString);
}
}
};