//reg_key = HKLM\SOFTWARE\KEYNAME
if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE ,
reg_key , 0 ,
KEY_QUERY_VALUE|KEY_SET_VALUE , &hkey) )
{
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; // 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]; //MAX_VALUE_NAME = 5
DWORD cchValue = MAX_VALUE_NAME;
// 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
// Enumerate the key values.
if (cValues)
{
num_vals = cValues;
if(reg_vals == NULL)
{
reg_vals = new char[num_vals * 5];
SecureZeroMemory(reg_vals, ( (num_vals * 5) * sizeof(char) ) );
}
for (i=0, retCode=ERROR_SUCCESS; i < cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retCode = RegEnumValue(hkey, i,
achValue,
&cchValue,
NULL,
NULL,
NULL,
NULL);
if (retCode == ERROR_SUCCESS )
{
//Check for incorrect values
if( strlen(achValue) != (val_size - 1) )
{
//Delete the errenous value
RegDeleteValue(hkey,achValue);
achValue[0] = '\0';
num_vals--;
}
else
{
//Get the registry values into the 'reg_vals' array
strncpy_s((reg_vals + (i * val_size ) ), 5, achValue, strlen(achValue));
reg_vals[(i * val_size )+ strlen(achValue) ] = ',';
}
}
}
reg_vals[( (i-1) * val_size )+ strlen(achValue) ] = '\0'; }
RegCloseKey(hkey);
}
|