|
const bool CRegistryAccess::CreateKey(const CString &cstrBranch, const CString &cstrKeyName)
// This is to create a variable in the registry (without setting it). Return T on success and F if not.
{
TRY
{
HKEY hKey = (HKEY)NULL;
TCHAR szName[MAX_KEY_LENGTH] = _T("");
::wsprintf(szName, TEXT("%s\\%s"), (LPCTSTR)cstrBranch, (LPCTSTR)cstrKeyName);
DWORD dwDisp = 0;
bool bResult = (::RegCreateKeyEx(
m_hKeyCurrentUser,
szName,
0,
NULL,
REG_OPTION_NON_VOLATILE,
KEY_FULL_ACCESS,
NULL,
&hKey,
&dwDisp) == ERROR_SUCCESS);
if(!bResult)
{
#ifdef _DEBUG
if(m_bHiddenKey)
bResult = (::GetLastError() == 0);
else
::ShowLastError(_T("RegCreateKeyEx"));
#endif
}
// Return T on success and F if not.
return bResult;
}
CATCH_ALL(e)
{
WNDTRACEEXCEPTION_USERDLG("CRegistryAccess", "CreateKey", "", e);
THROW_LAST();
}
END_CATCH_ALL
return false;
}
const bool CRegistryAccess::EnumKey(CONST DWORD dwIndex, HKEY &hKey, CString &cstrKeyName)
// Enum one key on a open key handle.
{
TRY
{
LONG lResult = ERROR_SUCCESS;
TCHAR szName[MAX_KEY_LENGTH] = _T("");
DWORD dwSize = MAX_KEY_LENGTH;
FILETIME ftWrite;
cstrKeyName.Empty();
lResult = ::RegEnumKeyEx(
hKey,
dwIndex,
szName,
&dwSize,
NULL,
NULL,
NULL,
&ftWrite);
if(lResult == ERROR_SUCCESS)
{
// Got the one we wanted.
cstrKeyName.Format(TEXT("%s"), szName);
return true;
}
#ifdef _DEBUG
if(!m_bHiddenKey)
::ShowLastError(_T("RegEnumKeyEx"));
#endif
return false;
}
CATCH_ALL(e)
{
WNDTRACEEXCEPTION_USERDLG("CRegistryAccess", "EnumKey", "", e);
THROW_LAST();
}
END_CATCH_ALL
return false;
}
const void CRegistryAccess::EnumKeys(const CString &cstrBranch, CStringArray &sarrKeys)
// Return keys under this branch as string array.
{
TRY
{
HKEY hKey = (HKEY)NULL;
sarrKeys.RemoveAll();
sarrKeys.SetSize(0, 1);
if(OpenKey(cstrBranch, hKey))
{
CString cstrKeyName(_T(""));
for(DWORD dwIndex = 0; EnumKey(dwIndex, hKey, cstrKeyName); dwIndex++)
// Collect it.
sarrKeys.Add(cstrKeyName);
CloseKey(hKey);
}
}
CATCH_ALL(e)
{
WNDTRACEEXCEPTION_USERDLG("CRegistryAccess", "EnumKeys", "", e);
THROW_LAST();
}
END_CATCH_ALL
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.