Get all values names in key

Hi! I've been searching for a day now and I would like to know how I can get all the values in a key from the registry.

Here is some code:
1
2
3
4
5
6
7
8
9
10
11
    subkey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey,0,KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS)
    {
        dwType = REG_SZ;
        if( RegQueryValueEx(hKey,"AVG_TRAY",0,&dwType,(BYTE*)buffer,&dwBufferSize) == ERROR_SUCCESS )
        {
            cout << "key value is'" << buffer << "'\n";
        }
        else
            cout << "can not query for key value\n";
    }

That gives me the data found in the value named AVG_TRAY from hkey(HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run).

The problem is that before this code I want to get all values into a string array that will be used here to get the data into another array.

So, here is what I would need:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
int GetPaths()
{
    char buffer[255] = {0};
    DWORD dwBufferSize = sizeof(buffer);
    const char* subkey;
    string values[100]; // Max 100 values.
    string Paths[100]; // Max 100 paths.
    DWORD dwType = 0;
    HKEY hKey = 0;
    int i=0;
    int j=0;

    subkey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,subkey,0,KEY_ALL_ACCESS,&hKey) == ERROR_SUCCESS)
    {
        dwType = REG_SZ;
        /** Here a loop that would call a function 
         * that would retrieve the name of the first
         * value into values[i] (i would be 0 initially)
         * and then increment 1 to i and loop again.
         * The value would be the next one and its
         * name would be save in values[i] (i would be
         * 1 this time). It would keep until all the
         * values in hKey would have been read.
         */
         while( i =< hKey.numberofvalues )
         {
             GetValueName(from HKey, valuenumber i, into buffer);
             values[i] = buffer;
             i++;
         }
         /** Now a loop to get the data of thoose values
          * (values[j]) into buffer and then into Paths[j].
          */
         while( j =< i )
         {
             if(RegQueryValueEx(hKey,values[j],0,&dwType,(BYTE*)buffer,&dwBufferSize) == ERROR_SUCCESS)
             {
                 Paths[j] = buffer;
                 j++;
             }
             else
                cout << "Could not query value from " << hKey << endl;
         }
    }
    else
        cout << "Could not open key " << subkey << endl;

    cout << "Got paths from registry successfully." << endl;
    RegCloseKey(hKey);
}


I hope you understand what I want.

The data I want from the values are paths to applications, so that is why data goes into the string Paths.

Thanks for your help.
Last edited on
Anyone? I really need this :(
This is working code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include "stdafx.h"
#include <windows.h>

#define MAX_KEY_LENGTH 255
#define MAX_VALUE_NAME 16383

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)
		{
		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. 

	BYTE* buffer = new BYTE[cbMaxValueData];
	ZeroMemory(buffer, cbMaxValueData);

	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 ) 
				{ 
				
				DWORD lpData = cbMaxValueData;
				buffer[0] = '\0';
				LONG dwRes = RegQueryValueEx(hKey, achValue, 0, NULL, buffer, &lpData);
				_tprintf(TEXT("(%d) %s : %s\n"), i+1, achValue, buffer); 
				} 
			}
		}
	delete [] buffer;
	}


int _tmain(int argc, _TCHAR* argv[])
{
HKEY hKey;
LONG dwRegOPenKey = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run\\"), 0, KEY_READ, &hKey);
if(dwRegOPenKey == ERROR_SUCCESS){
	printf("RegOpenKeyEx succeeded, error code %d\n", GetLastError());
	QueryKey(hKey);

	} else {
		printf("RegOpenKeyEx failed, error code %d\n", dwRegOPenKey);
	}
	RegCloseKey(hKey);
return 0;
}
Awesome, dude, thanks :D
Topic archived. No new replies allowed.