2
我想獲取特定鍵路徑下的所有註冊表值,但RegEnumValue()始終返回錯誤代碼259,因爲ERROR_NO_MORE_ITEMS和sectionValue具有無意義的值。我手動檢查註冊表,並在指定的鍵下有值。C++ RegEnumValue() - 無法迭代到每個值
例如。
關鍵是MyTestApp
鍵值ManualTestCase = 10
鍵值AutomationTestCase = 50
HKEY hKey; //registry key handle
LONG lResult; //result of registry operations
DWORD dwType, dwSize=0;
//try to open the key that we are currently pointing at with rootPath
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, rootPath, NULL, KEY_ALL_ACCESS, &hKey);
if (lResult == ERROR_SUCCESS)
{
LPTSTR className = NULL;
DWORD classNameSize = MAX_PATH;
DWORD subKey = 0;
DWORD maxSubKey;
DWORD maxClass;
DWORD value;
DWORD maxValue;
DWORD maxValueData;
DWORD securityDescriptor;
FILETIME ftLastWriteTime;
DWORD sectionNameSize;
int j;
//to get total keys for the specified path
lResult = RegQueryInfoKey(hKey, className, &classNameSize, NULL,
&subKey, &maxSubKey, &maxClass, &value, &maxValue,
&maxValueData, &securityDescriptor, &ftLastWriteTime);
if(lResult == ERROR_SUCCESS)
{
for(int i = 0; i < subKey; i++)
{
LPTSTR sectionName = new TCHAR[1096];
sectionNameSize = 1096;
ftLastWriteTime.dwHighDateTime = 0;
ftLastWriteTime.dwLowDateTime = 0;
//enumerate all the registry key names for specified path
lResult = RegEnumKeyEx(hKey, i, sectionName,
§ionNameSize, NULL, NULL,
NULL, &ftLastWriteTime);
CString testStr = sectionName;
if(lResult == ERROR_SUCCESS)
{
j = 0;
do
{
LPTSTR sectionValue;
DWORD sectionValueSize = 4096;
DWORD dwType;
//enumerate all the values for specified key
lResult = RegEnumValue(hKey, j, sectionName,
§ionNameSize, NULL, &dwType,
(LPBYTE)sectionValue, §ionValueSize);
//
if(lResult == ERROR_SUCCESS)
{
//do something to the data
bool whatever = true;
}
else if(lResult == ERROR_MORE_DATA)
{
//
bool yeahSure = true;
}
j++;
}while(lResult != ERROR_NO_MORE_ITEMS);
}
delete[] sectionName;
}
}
}
RegCloseKey(hKey);
謝謝,就是這樣。在調用RegEnumValue(..)之前調用RegOpenKeyEx(HKEY_LOCAL_MACHINE,rootPath +「\\」+ sectionName,NULL,KEY_ALL_ACCESS,&hKey);)之後,可以得到正確的值 – Lufia 2011-12-23 21:29:59
另外我需要創建KHEY變量hKey1並傳入RegEnumValue()引用RegOpenKeyEx(,,,,,&hKey1)。完成遍歷該值後,必須關閉hKey1,否則我無法進入下一個子鍵。 – Lufia 2011-12-23 21:42:56
您也忘記在值枚舉循環中重新初始化sectionNameSize。 – 2011-12-24 14:21:15