vc++高级班之注册表篇[2]---注册表中键的相关操作
----------------------------------------------------------------------------------------
①、键信息的获取:用到的API函数:RegQueryInfoKey
void CRegTestDlg::OnBnClickedBtn()
{
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
DWORD dwSubKeys = 0, maxSubKeyLen = 0, dwValueCount = 0, maxValueNameLen = 0, maxValueDataLen = 0;
lRet = RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwSubKeys, &maxSubKeyLen, NULL, &dwValueCount, &maxValueNameLen, &maxValueDataLen, NULL, NULL);
if (lRet == ERROR_SUCCESS) {
//
}
}
}
===================================================
②、子键的枚举操作:用到的API函数:RegEnumKeyEx
在进行子键的枚举操作时,要借助刚才给大家讲解的 RegQueryInfoKey 函数!
void CRegTestDlg::OnBnClickedBtn()
{
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
DWORD dwSubKeys = 0, maxSubKeyLen = 0, dwValueCount = 0, maxValueNameLen = 0, maxValueDataLen = 0;
lRet = RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwSubKeys, &maxSubKeyLen, NULL, &dwValueCount, &maxValueNameLen, &maxValueDataLen, NULL, NULL);
if (lRet == ERROR_SUCCESS) {
//Enum for the sub keys
DWORD dwNameLen = maxSubKeyLen+1;
TCHAR *pszName = new TCHAR[maxSubKeyLen+1];
for (DWORD dwIndex = 0; dwIndex < dwSubKeys; ++dwIndex) {
ZeroMemory(pszName, dwNameLen);
dwNameLen = maxSubKeyLen+1; //※※※
RegEnumKeyEx(hKey, dwIndex, pszName, &dwNameLen, NULL, NULL, NULL, NULL);
//Other operations
}
delete [] pszName;
}
RegCloseKey(hKey);
}
}
===================================================
③、键的备份与恢复:
备份:RegSaveKey:第二个参数 lpFile 为保存信息的文件名称,这个文件必须是不存在的
void CRegTestDlg::OnBnClickedBtn()
{
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
EnableTargetPriv(SE_BACKUP_NAME);
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
lRet = RegSaveKey(hKey, _T("C:\\123"), NULL);
RegCloseKey(hKey);
}
}
恢复:RegRestoreKey:
void CRegTestDlg::OnBnClickedBtn()
{
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
EnableTargetPriv(SE_RESTORE_NAME);
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
LPCTSTR lpszFile = _T("C:\\123");
lRet = RegRestoreKey(hKey, lpszFile, REG_FORCE_RESTORE);
RegCloseKey(hKey);
}
}
#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@
BOOL EnableTargetPriv(LPTSTR lpszPrivilege)
{
HANDLE hToken = NULL;
TOKEN_PRIVILEGES tkp = {0};
if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) {
return FALSE;
}
if ( !LookupPrivilegeValue( NULL, lpszPrivilege, &tkp.Privileges[0].Luid ) ) {
CloseHandle( hToken );
return FALSE;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if ( !AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), NULL, NULL ) ) {
CloseHandle( hToken );
return FALSE;
}
return TRUE;
}
===================================================
※※※ 小作业:
1、实践本节课给大家讲解的各个 API 函数的使用;
2、时间允许的话,大家可以以树形控件为例,枚举指定注册表键下的子键,用树形控件进行显示!
------------------------------------- End -------------------------------------------
----------------------------------------------------------------------------------------
①、键信息的获取:用到的API函数:RegQueryInfoKey
void CRegTestDlg::OnBnClickedBtn()
{
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
DWORD dwSubKeys = 0, maxSubKeyLen = 0, dwValueCount = 0, maxValueNameLen = 0, maxValueDataLen = 0;
lRet = RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwSubKeys, &maxSubKeyLen, NULL, &dwValueCount, &maxValueNameLen, &maxValueDataLen, NULL, NULL);
if (lRet == ERROR_SUCCESS) {
//
}
}
}
===================================================
②、子键的枚举操作:用到的API函数:RegEnumKeyEx
在进行子键的枚举操作时,要借助刚才给大家讲解的 RegQueryInfoKey 函数!
void CRegTestDlg::OnBnClickedBtn()
{
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
DWORD dwSubKeys = 0, maxSubKeyLen = 0, dwValueCount = 0, maxValueNameLen = 0, maxValueDataLen = 0;
lRet = RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwSubKeys, &maxSubKeyLen, NULL, &dwValueCount, &maxValueNameLen, &maxValueDataLen, NULL, NULL);
if (lRet == ERROR_SUCCESS) {
//Enum for the sub keys
DWORD dwNameLen = maxSubKeyLen+1;
TCHAR *pszName = new TCHAR[maxSubKeyLen+1];
for (DWORD dwIndex = 0; dwIndex < dwSubKeys; ++dwIndex) {
ZeroMemory(pszName, dwNameLen);
dwNameLen = maxSubKeyLen+1; //※※※
RegEnumKeyEx(hKey, dwIndex, pszName, &dwNameLen, NULL, NULL, NULL, NULL);
//Other operations
}
delete [] pszName;
}
RegCloseKey(hKey);
}
}
===================================================
③、键的备份与恢复:
备份:RegSaveKey:第二个参数 lpFile 为保存信息的文件名称,这个文件必须是不存在的
void CRegTestDlg::OnBnClickedBtn()
{
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
EnableTargetPriv(SE_BACKUP_NAME);
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
lRet = RegSaveKey(hKey, _T("C:\\123"), NULL);
RegCloseKey(hKey);
}
}
恢复:RegRestoreKey:
void CRegTestDlg::OnBnClickedBtn()
{
HKEY hKey = NULL;
TCHAR *lpszSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
EnableTargetPriv(SE_RESTORE_NAME);
LONG lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpszSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (lRet == ERROR_SUCCESS) {
LPCTSTR lpszFile = _T("C:\\123");
lRet = RegRestoreKey(hKey, lpszFile, REG_FORCE_RESTORE);
RegCloseKey(hKey);
}
}
#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@#@
BOOL EnableTargetPriv(LPTSTR lpszPrivilege)
{
HANDLE hToken = NULL;
TOKEN_PRIVILEGES tkp = {0};
if ( !OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken ) ) {
return FALSE;
}
if ( !LookupPrivilegeValue( NULL, lpszPrivilege, &tkp.Privileges[0].Luid ) ) {
CloseHandle( hToken );
return FALSE;
}
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if ( !AdjustTokenPrivileges( hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), NULL, NULL ) ) {
CloseHandle( hToken );
return FALSE;
}
return TRUE;
}
===================================================
※※※ 小作业:
1、实践本节课给大家讲解的各个 API 函数的使用;
2、时间允许的话,大家可以以树形控件为例,枚举指定注册表键下的子键,用树形控件进行显示!
------------------------------------- End -------------------------------------------