在运行以下C ++代码以提取HKEY_CURRENT_USER
的整个注册表子键时,我在netbeans中收到RUN FAILED (exit value 1..)
错误。只打开一个子键,即open()
函数运行一次。
Link到RegOpenKeyEx
功能。
任何帮助都将受到高度赞赏。
#include <windows.h>
#include <iostream>
#include <string>
#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383
#define MAX_SUB_KEYS 10000
using namespace std;
void QueryKey(HKEY);
void open(HKEY,string [],int);
void open(HKEY hkey12,string subkey[],int m)
{
int u=0;
while(u<m)
{
HKEY hkey1;
const char* sub=subkey[u].c_str();
if( RegOpenKeyEx(hkey12,
sub,
0,
KEY_READ,
&hkey1) == ERROR_SUCCESS
)
{
QueryKey(hkey1);
}
else { cout<<"Subkey open failed "<<endl;}
RegCloseKey(hkey1);
u++;
}
}
int main()
{
HKEY hTestKey;
if( RegOpenKeyEx( HKEY_CURRENT_USER,
NULL,
0,
KEY_READ,
&hTestKey) == ERROR_SUCCESS
)
{
QueryKey(hTestKey);
}
RegCloseKey(hTestKey);
return 0;
}
void QueryKey(HKEY hKey) // This function fetches the actual parameters.
{
TCHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name
DWORD cbName; // size of name string
TCHAR achClass[MAX_PATH] = TEXT(""); // buffer for class name
DWORD cchClassName = MAX_PATH; // size of class string
DWORD cSubKeys=0; // number of subkeys
DWORD cbMaxSubKey; // longest subkey size
DWORD cchMaxClass; // longest class string
DWORD cValues=0; // number of values for key
DWORD cchMaxValue; // longest value name
DWORD cbMaxValueData; // longest value data
DWORD cbSecurityDescriptor; // size of security descriptor
FILETIME ftLastWriteTime; // last write time
DWORD i, retCode;
TCHAR achValue[MAX_VALUE_NAME];
DWORD valueData; // BYTE valueData[2048]; // buffer for value data
DWORD cchValue = MAX_VALUE_NAME; //pointer to size of value name
DWORD valueType; // buffer for value type
DWORD size=2049; // pointer to size of value data
// Get the class name and the value count.
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
string subkey[cSubKeys];
string values[cValues];
int typev[cValues];
// Extract the subkeys, until RegEnumKeyEx fails.
if (cSubKeys)
{
for (i=0; i<cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
subkey[i]=achKey;
}
}
}
// Enumerate the key values.
if (cValues)
{
for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retCode = RegEnumValue(hKey, i,
achValue,
&cchValue,
NULL,
&valueType,
(BYTE *)valueData,
&size);
if (retCode == ERROR_SUCCESS )
{
values[i]=achValue;
typev[i]=valueType;
}
else { cout<<"Error"<<endl;
}
}
}
int d=0;
cout<<"------------------------------------------------------------------------"<<endl;
cout<<"Sub Keys of "<<"are displayed below- "<<endl<<endl;
while(d<cSubKeys){
cout<<subkey[d]<<endl;
d++;
}
d=0;
cout<<endl;
if(!values[0].empty()) {
cout<<"Values are displayed below- "<<endl<<endl;
while(d<cValues){
cout<<values[d]<<" "<<typev[d]<<endl;
d++;
}
}
open(hKey,subkey,cSubKeys);
}