📄 registry.cpp
字号:
pValData->dwLength = dwSize;
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
free (pbyteData);
}
}
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* load a number */
bool
RegLoadNumber (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, DWORD *pdwNumber)
{
ASSERT (pdwNumber);
HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ) : hKey;
if (hSubKey)
{
DWORD dwType, dwSize;
if (RegQueryValueEx (hSubKey, pszValName, 0, &dwType, NULL, &dwSize) == ERROR_SUCCESS)
{
if (dwType == REG_DWORD)
{
ASSERT (dwSize == sizeof (DWORD));
if (RegQueryValueEx (hSubKey, pszValName, 0, NULL, (LPBYTE) pdwNumber, &dwSize) == ERROR_SUCCESS)
{
ASSERT (dwSize == sizeof (DWORD));
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
}
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* load binary data */
bool
RegLoadBinary (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, LPBYTE pbyteData, DWORD dwSize)
{
ASSERT (pbyteData);
HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ) : hKey;
if (hSubKey)
{
DWORD dwType, dwRealSize;
if (RegQueryValueEx (hSubKey, pszValName, 0, &dwType, NULL, &dwRealSize) == ERROR_SUCCESS)
{
if (dwType == REG_BINARY &&dwSize >= dwRealSize)
{
if (RegQueryValueEx (hSubKey, pszValName, 0, NULL, pbyteData, &dwRealSize) == ERROR_SUCCESS)
{
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
}
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* load new binary data */
bool
RegLoadNewBinary (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, LPBYTE *pbyteData, DWORD *pdwSize)
{
ASSERT (pbyteData);
HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ) : hKey;
if (hSubKey)
{
DWORD dwType, dwRealSize;
if (RegQueryValueEx (hSubKey, pszValName, 0, &dwType, NULL, &dwRealSize) == ERROR_SUCCESS)
{
if (dwType == REG_BINARY)
{
LPBYTE pbyteNewData = (LPBYTE) malloc (dwRealSize);
if (pbyteNewData)
{
if (RegQueryValueEx (hSubKey, pszValName, 0, NULL, pbyteNewData, &dwRealSize) == ERROR_SUCCESS)
{
*pbyteData = pbyteNewData;
*pdwSize = dwRealSize;
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
free (pbyteNewData);
}
}
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* load a string */
bool
RegLoadString (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, LPTSTR pszString, DWORD dwLength)
{
ASSERT (pszString);
HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ) : hKey;
if (hSubKey)
{
DWORD dwType, dwRealLength;
if (RegQueryValueEx (hSubKey, pszValName, 0, &dwType, NULL, &dwRealLength) == ERROR_SUCCESS)
{
if ((dwType == REG_SZ || dwType == REG_EXPAND_SZ || dwType == REG_LINK
|| dwType == REG_MULTI_SZ) &&dwLength >= dwRealLength)
{
if (RegQueryValueEx (hSubKey, pszValName, 0, NULL, (LPBYTE) pszString, &dwRealLength) == ERROR_SUCCESS)
{
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
}
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* load a new string */
bool
RegLoadNewString (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, LPTSTR *pszString, DWORD *pdwLength)
{
ASSERT (pszString);
HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ) : hKey;
if (hSubKey)
{
DWORD dwType, dwRealLength;
if (RegQueryValueEx (hSubKey, pszValName, 0, &dwType, NULL, &dwRealLength) == ERROR_SUCCESS)
{
if (dwType == REG_SZ || dwType == REG_EXPAND_SZ || dwType == REG_LINK
|| dwType == REG_MULTI_SZ)
{
LPTSTR pszNewString = (LPTSTR) malloc (dwRealLength);
if (pszNewString)
{
if (RegQueryValueEx (hSubKey, pszValName, 0, NULL, (LPBYTE) pszNewString, &dwRealLength) == ERROR_SUCCESS)
{
*pszString = pszNewString;
if (pdwLength)
{
*pdwLength = dwRealLength;
}
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
free (pszNewString);
}
}
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* load an array of strings */
bool
RegLoadStringArr (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, LPTSTR pszStrings[], DWORD dwCount)
{
RegVal Value;
if (RegLoadVal (hKey, pszSubKey, pszValName, &Value))
{
if (RegValGetStringArr (&Value, pszStrings, dwCount))
{
RegValFree (&Value);
return true;
}
RegValFree (&Value);
}
return false;
}
/* load a new array of strings */
bool
RegLoadNewStringArr (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, LPTSTR **pszStrings, DWORD *pdwCount)
{
RegVal Value;
if (RegLoadVal (hKey, pszSubKey, pszValName, &Value))
{
if (RegValGetNewStringArr (&Value, pszStrings, pdwCount))
{
RegValFree (&Value);
return true;
}
RegValFree (&Value);
}
return false;
}
#ifdef REG_WITH_MFC
/* load a string */
bool
RegLoadString (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, CString &sString)
{
HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ) : hKey;
if (hSubKey)
{
DWORD dwType, dwRealLength;
if (RegQueryValueEx (hSubKey, pszValName, 0, &dwType, NULL, &dwRealLength) == ERROR_SUCCESS)
{
if (dwType == REG_SZ || dwType == REG_EXPAND_SZ || dwType == REG_LINK
|| dwType == REG_MULTI_SZ)
{
LPTSTR pszString = sString.GetBuffer (dwRealLength);
if (RegQueryValueEx (hSubKey, pszValName, 0, NULL, (LPBYTE) pszString, &dwRealLength) == ERROR_SUCCESS)
{
sString.ReleaseBuffer (dwRealLength);
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
sString.ReleaseBuffer (0);
}
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* load an array of strings */
bool
RegLoadStringArr (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, CStringArray &arrString)
{
RegVal Value;
if (RegLoadVal (hKey, pszSubKey, pszValName, &Value))
{
if (RegValGetStringArr (&Value, arrString))
{
RegValFree (&Value);
return true;
}
RegValFree (&Value);
}
return false;
}
#endif /* REG_WITH_MFC */
/* store data of any type */
bool
RegSaveVal (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, const RegVal *pValData)
{
ASSERT (pValData);
HKEY hSubKey = pszSubKey ? RegCreate (hKey, pszSubKey, KEY_WRITE) : hKey;
if (hSubKey)
{
LONG lResult;
if (pValData->dwType == REG_DWORD)
{
lResult = RegSetValueEx (hSubKey, pszValName, 0, pValData->dwType, (LPBYTE) &pValData->dwNumber, sizeof (DWORD));
}
else if (pValData->dwType == REG_SZ || pValData->dwType == REG_EXPAND_SZ
|| pValData->dwType == REG_LINK || pValData->dwType == REG_MULTI_SZ
|| pValData->dwType == REG_BINARY)
{
lResult = RegSetValueEx (hSubKey, pszValName, 0, pValData->dwType, pValData->pbyteData, pValData->dwSize);
}
else
{
lResult = ERROR_BAD_FORMAT;
}
if (lResult == ERROR_SUCCESS)
{
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* store a number */
bool
RegSaveNumber (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, DWORD dwNumber)
{
HKEY hSubKey = pszSubKey ? RegCreate (hKey, pszSubKey, KEY_WRITE) : hKey;
if (hSubKey)
{
if (RegSetValueEx (hSubKey, pszValName, 0, REG_DWORD, (LPBYTE) &dwNumber, sizeof (DWORD)) == ERROR_SUCCESS)
{
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* store binary data */
bool
RegSaveBinary (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, const LPBYTE pbyteData, DWORD dwSize)
{
HKEY hSubKey = pszSubKey ? RegCreate (hKey, pszSubKey, KEY_WRITE) : hKey;
if (hSubKey)
{
if (RegSetValueEx (hSubKey, pszValName, 0, REG_BINARY, pbyteData, dwSize) == ERROR_SUCCESS)
{
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* store a string */
bool
RegSaveString (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, LPCTSTR pszString)
{
HKEY hSubKey = pszSubKey ? RegCreate (hKey, pszSubKey, KEY_WRITE) : hKey;
if (hSubKey)
{
if (RegSetValueEx (hSubKey, pszValName, 0, REG_SZ, (LPBYTE) pszString, _tcslen (pszString) + 1) == ERROR_SUCCESS)
{
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* store an array of strings */
bool
RegSaveStringArr (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, const LPCTSTR pszStrings[], DWORD dwCount)
{
RegVal Value;
if (RegValSetStringArr (&Value, pszStrings, dwCount))
{
if (RegSaveVal (hKey, pszSubKey, pszValName, &Value))
{
RegValFree (&Value);
return true;
}
RegValFree (&Value);
}
return false;
}
#ifdef REG_WITH_MFC
/* store an array of strings */
bool
RegSaveStringArr (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName, const CStringArray &arrString)
{
RegVal Value;
if (RegValSetStringArr (&Value, arrString))
{
if (RegSaveVal (hKey, pszSubKey, pszValName, &Value))
{
RegValFree (&Value);
return true;
}
RegValFree (&Value);
}
return false;
}
#endif /* REG_WITH_MFC */
/* delete all subkeys in the given key */
bool
RegDeleteSubKeys (HKEY hKey)
{
DWORD dwSubKeyCnt, dwMaxSubKey;
if (RegQueryInfoKey (hKey, NULL, NULL, 0, &dwSubKeyCnt, &dwMaxSubKey,
NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
if (dwSubKeyCnt)
{
LPTSTR pszKeyName = (LPTSTR) malloc (dwMaxSubKey += 1);
if (pszKeyName)
{
do
{
if (RegEnumKey (hKey, --dwSubKeyCnt, pszKeyName, dwMaxSubKey) == ERROR_SUCCESS)
{
HKEY hSubKey = RegOpen (hKey, pszKeyName, KEY_READ | KEY_WRITE);
if (hSubKey)
{
if (RegDeleteSubKeys (hSubKey))
{
RegClose (hSubKey);
if (RegDeleteKey (hKey, pszKeyName) != ERROR_SUCCESS)
{
free (pszKeyName);
return false;
}
}
else
{
RegClose (hSubKey);
free (pszKeyName);
return false;
}
}
else
{
free (pszKeyName);
return false;
}
}
else
{
free (pszKeyName);
return false;
}
}
while (dwSubKeyCnt);
free (pszKeyName);
}
else
{
return false;
}
}
return true;
}
return false;
}
/* delete the given value or key in the registry with all of its subkeys */
bool
RegDeleteKey (HKEY hKey, LPCTSTR pszSubKey, LPCTSTR pszValName)
{
HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ | KEY_WRITE) : hKey;
if (hSubKey)
{
if (pszValName)
{
if (RegDeleteValue (hSubKey, pszValName) == ERROR_SUCCESS)
{
if (hSubKey != hKey)
RegClose (hSubKey);
return true;
}
}
else
{
if (RegDeleteSubKeys (hSubKey))
{
if (hSubKey != hKey)
RegClose (hSubKey);
return RegDeleteKey (hKey, pszSubKey) == ERROR_SUCCESS;
}
}
if (hSubKey != hKey)
RegClose (hSubKey);
}
return false;
}
/* check wether the given key has other subkeys and/or values */
bool
RegHasEntries (HKEY hKey, LPCTSTR pszSubKey, DWORD *pdwSubKeyCount, DWORD *pdwValueCount)
{
HKEY hSubKey = pszSubKey ? RegOpen (hKey, pszSubKey, KEY_READ) : hKey;
if (hSubKey)
{
if (RegQueryInfoKey (hSubKey, NULL, NULL, 0, pdwSubKeyCount, NULL, NULL, pdwValueCount, NULL, NULL, NULL, NULL) == ERROR_SUCCESS)
{
if (hSubKey != hKey)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -