大家好,本人正尝试用C++编程来删除注册表的某个键及其所有子键,相关程序在MSDN上找到了,却遇到如下问题很奇怪:
RegDelnode(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Test123"));就失败
RegDelnode(HKEY_CURRENT_USER,
TEXT("SOFTWARE\\Test123"));就成功进一步试验也得出,凡是HKEY_LOCAL_MACHINE里的键值就不能删,而HKEY_CURRENT_USER里的键值就可以删除,我百思不得其解,明明删除键值的函数里的两个参数的第一个就可以在HKEY_CURRENT_USER、HKEY_LOCAL_MACHINE等等里面选择的,怎么会选择性失明?我具体写的时候是用MFC写的,代码基本没变,附CSDN上的代码如下,大家帮忙分析一下:#include <windows.h>
#include <stdio.h>
#include <strsafe.h>
using namespace std;
//*************************************************************
//
// RegDelnodeRecurse()
//
// Purpose: Deletes a registry key and all it's subkeys / values.
//
// Parameters: hKeyRoot - Root key
// lpSubKey - SubKey to delete
//
// Return: TRUE if successful.
// FALSE if an error occurs.
//
//*************************************************************BOOL RegDelnodeRecurse (HKEY hKeyRoot, LPTSTR lpSubKey)
{
LPTSTR lpEnd;
LONG lResult;
DWORD dwSize;
TCHAR szName[MAX_PATH];
HKEY hKey;
FILETIME ftWrite; // First, see if we can delete the key without having
// to recurse. lResult = RegDeleteKey(hKeyRoot, lpSubKey); if (lResult == ERROR_SUCCESS)
return TRUE; lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ, &hKey); if (lResult != ERROR_SUCCESS)
{
if (lResult == ERROR_FILE_NOT_FOUND) {
printf("Key not found.\n");
return TRUE;
}
else {
printf("Error opening key.\n");
return FALSE;
}
} // Check for an ending slash and add one if it is missing. lpEnd = lpSubKey + lstrlen(lpSubKey); if (*(lpEnd - 1) != TEXT('\\'))
{
*lpEnd = TEXT('\\');
lpEnd++;
*lpEnd = TEXT('\0');
} // Enumerate the keys dwSize = MAX_PATH;
lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
NULL, NULL, &ftWrite); if (lResult == ERROR_SUCCESS)
{
do { StringCchCopy (lpEnd, MAX_PATH*2, szName); if (!RegDelnodeRecurse(hKeyRoot, lpSubKey)) {
break;
} dwSize = MAX_PATH; lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
NULL, NULL, &ftWrite); } while (lResult == ERROR_SUCCESS);
} lpEnd--;
*lpEnd = TEXT('\0'); RegCloseKey (hKey); // Try again to delete the key. lResult = RegDeleteKey(hKeyRoot, lpSubKey); if (lResult == ERROR_SUCCESS)
return TRUE; return FALSE;
}//*************************************************************
//
// RegDelnode()
//
// Purpose: Deletes a registry key and all it's subkeys / values.
//
// Parameters: hKeyRoot - Root key
// lpSubKey - SubKey to delete
//
// Return: TRUE if successful.
// FALSE if an error occurs.
//
//*************************************************************BOOL RegDelnode (HKEY hKeyRoot, LPTSTR lpSubKey)
{
TCHAR szDelKey[2 * MAX_PATH]; StringCchCopy (szDelKey, MAX_PATH*2, lpSubKey);
return RegDelnodeRecurse(hKeyRoot, szDelKey);}void main()
{
BOOL bSuccess; bSuccess = RegDelnode(HKEY_CURRENT_USER,
TEXT("Software\\TestDir")); if(bSuccess)
printf("Success!\n");
else printf("Failure.\n");
}
RegDelnode(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Test123"));就失败
RegDelnode(HKEY_CURRENT_USER,
TEXT("SOFTWARE\\Test123"));就成功进一步试验也得出,凡是HKEY_LOCAL_MACHINE里的键值就不能删,而HKEY_CURRENT_USER里的键值就可以删除,我百思不得其解,明明删除键值的函数里的两个参数的第一个就可以在HKEY_CURRENT_USER、HKEY_LOCAL_MACHINE等等里面选择的,怎么会选择性失明?我具体写的时候是用MFC写的,代码基本没变,附CSDN上的代码如下,大家帮忙分析一下:#include <windows.h>
#include <stdio.h>
#include <strsafe.h>
using namespace std;
//*************************************************************
//
// RegDelnodeRecurse()
//
// Purpose: Deletes a registry key and all it's subkeys / values.
//
// Parameters: hKeyRoot - Root key
// lpSubKey - SubKey to delete
//
// Return: TRUE if successful.
// FALSE if an error occurs.
//
//*************************************************************BOOL RegDelnodeRecurse (HKEY hKeyRoot, LPTSTR lpSubKey)
{
LPTSTR lpEnd;
LONG lResult;
DWORD dwSize;
TCHAR szName[MAX_PATH];
HKEY hKey;
FILETIME ftWrite; // First, see if we can delete the key without having
// to recurse. lResult = RegDeleteKey(hKeyRoot, lpSubKey); if (lResult == ERROR_SUCCESS)
return TRUE; lResult = RegOpenKeyEx (hKeyRoot, lpSubKey, 0, KEY_READ, &hKey); if (lResult != ERROR_SUCCESS)
{
if (lResult == ERROR_FILE_NOT_FOUND) {
printf("Key not found.\n");
return TRUE;
}
else {
printf("Error opening key.\n");
return FALSE;
}
} // Check for an ending slash and add one if it is missing. lpEnd = lpSubKey + lstrlen(lpSubKey); if (*(lpEnd - 1) != TEXT('\\'))
{
*lpEnd = TEXT('\\');
lpEnd++;
*lpEnd = TEXT('\0');
} // Enumerate the keys dwSize = MAX_PATH;
lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
NULL, NULL, &ftWrite); if (lResult == ERROR_SUCCESS)
{
do { StringCchCopy (lpEnd, MAX_PATH*2, szName); if (!RegDelnodeRecurse(hKeyRoot, lpSubKey)) {
break;
} dwSize = MAX_PATH; lResult = RegEnumKeyEx(hKey, 0, szName, &dwSize, NULL,
NULL, NULL, &ftWrite); } while (lResult == ERROR_SUCCESS);
} lpEnd--;
*lpEnd = TEXT('\0'); RegCloseKey (hKey); // Try again to delete the key. lResult = RegDeleteKey(hKeyRoot, lpSubKey); if (lResult == ERROR_SUCCESS)
return TRUE; return FALSE;
}//*************************************************************
//
// RegDelnode()
//
// Purpose: Deletes a registry key and all it's subkeys / values.
//
// Parameters: hKeyRoot - Root key
// lpSubKey - SubKey to delete
//
// Return: TRUE if successful.
// FALSE if an error occurs.
//
//*************************************************************BOOL RegDelnode (HKEY hKeyRoot, LPTSTR lpSubKey)
{
TCHAR szDelKey[2 * MAX_PATH]; StringCchCopy (szDelKey, MAX_PATH*2, lpSubKey);
return RegDelnodeRecurse(hKeyRoot, szDelKey);}void main()
{
BOOL bSuccess; bSuccess = RegDelnode(HKEY_CURRENT_USER,
TEXT("Software\\TestDir")); if(bSuccess)
printf("Success!\n");
else printf("Failure.\n");
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货