Reputation: 21
I want to show all registry keys, subkeys, and values in HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run to see what programs run at startup.
I'm using this code from MS.
void QueryKey(HKEY hKey)
{
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; // 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 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 subkeys, until RegEnumKeyEx fails.
if (cSubKeys == 0)
{
printf("No values found\n");
}
if (cSubKeys)
{
printf( "\nNumber of subkeys: %d\n", 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)
{
_tprintf(TEXT("(%d) %s\n"), i+1, achKey);
}
}
}
// Enumerate the key values.
if (cValues)
{
printf( "\nNumber of values: %d\n", cValues);
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 )
{
_tprintf(TEXT("(%d) %s\n"), i+1, achValue);
}
}
}
}
int RegKeyCount = 0;
int main(int argc, char *argv[])
{
HKEY hTestKey;
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Windows \\CurrentVersion\\Run"), 0, KEY_READ, &hTestKey) == ERROR_SUCCESS)
{
QueryKey(hTestKey);
}
}
I am confused in that if I run this code for "SOFTWARE\Microsoft\Windows \CurrentVersion" it will show me all subkeys and values (I can see that Run is a subkey of CurrentVersion), however when I try to get it to show me the subkeys and values for Run it says nothing is found even though entries are there.
I should also say I do not know the names of the values of the subkeys/values, they could be anything.
Is this indeed what RegEnumValue is supposed to do or do I need to use another Registry Function?
Upvotes: 2
Views: 2326
Reputation: 5132
The only problem I found was the spaces in your parameter to RegOpenKeyEx()
, the program runs ok if you take out the embedded spaces so that it reads TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run")
.
Your printf
in the beginning is a bit confusing, maybe you should change "No values found\n"
to "No keys found\n"
?
if (cSubKeys == 0)
printf("No keys found\n");
Also: if you build/run this code as a 32-bit program in a 64-bit OS, be aware that you will get the contents of HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run, not HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run!
Upvotes: 1