Enumerating the registry key/subkey, names and values using C program example

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add netapi32.lib (netapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'netapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Enumerating the Windows registry keys/subkeys, names and values

To show: Using the various registry functions in C program example for Windows system

 

 

// QueryKey() - Enumerates the subkeys of key and its associated values.

// hKey - Key whose subkeys and values are to be enumerated.

// #define _WIN32_WINNT 0x0502 // Windows Server 2003 family

// For Win Xp, change accordingly...

#define _WIN32_WINNT 0x0501

// #define _WIN32_WINNT 0x0500 // Windows 2000

// #define _WIN32_WINNT 0x0400 // Windows NT 4.0

// #define _WIN32_WINDOWS 0x0500 // Windows ME

// #define _WIN32_WINDOWS 0x0410 // Windows 98

// #define _WIN32_WINDOWS 0x0400 // Windows 95

 

#include <windows.h>

#include <stdio.h>

#define MAX_KEY_LENGTH 255

#define MAX_VALUE_NAME 16383

 

void QueryKey(HKEY hKey)

{

CHAR achKey[MAX_KEY_LENGTH]; // buffer for subkey name

DWORD cbName; // size of name string

CHAR achClass[MAX_PATH] = ""; // 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;

CHAR achValue[MAX_VALUE_NAME];

DWORD cchValue = MAX_VALUE_NAME;

// Get the class name and the value count.

retCode = RegQueryInfoKey(

hKey, // key handle

(LPWSTR)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("Subkey Names:\n");

for(i=0; i<cSubKeys; i++)

{

cbName = MAX_KEY_LENGTH;

retCode = RegEnumKeyEx(

hKey, // Handle to an open/predefined key

i, // Index of the subkey to retrieve.

(LPWSTR)achKey, // buffer that receives the name of the subkey

&cbName, // size of the buffer specified by the achKey

NULL, // Reserved; must be NULL

NULL, // buffer that receives the class string

// of the enumerated subkey

NULL, // size of the buffer specified by the previous parameter

&ftLastWriteTime // variable that receives the time at which

// the enumerated subkey was last written

);

 

if(retCode == ERROR_SUCCESS)

{

printf("(%d) %S\n", i+1, achKey);

}

}

printf("Number of subkeys: %d\n\n", cSubKeys);

}

else

printf("RegEnumKeyEx(), there is no subkey.\n");

 

// Enumerate the key values if any.

if(cValues)

{

for(i=0, retCode=ERROR_SUCCESS; i<cValues; i++)

{

cchValue = MAX_VALUE_NAME;

achValue[0] = '\0';

retCode = RegEnumValue(

hKey, // Handle to an open key

i, // Index of value

(LPWSTR)achValue, // Value name

&cchValue, // Buffer for value name

NULL, // Reserved

NULL, // Value type

NULL, // Value data

NULL); // Buffer for value data

 

if(retCode == ERROR_SUCCESS)

{

printf("(%d) Value Name: %S.\n", i+1, achValue);

}

}

printf("Number of values: %d\n", cValues);

}

else

printf("No value under this key/subkey...\n");

}

 

int main(int argc, char *argv[])

{

// Change the key and subkey accordingly...

// The predefined registry keys:

// HKEY_CLASSES_ROOT

// HKEY_CURRENT_CONFIG

// HKEY_CURRENT_USER

// HKEY_LOCAL_MACHINE

// HKEY_PERFORMANCE_DATA

// HKEY_USERS

 

//*************** open key (and subkey) ******************

HKEY theKey = HKEY_LOCAL_MACHINE; // Key, change accordingly...

HKEY hKey; // Key and subkey

LONG lRet = RegOpenKeyEx(

theKey, // Key

L"SYSTEM\\ControlSet001\\Control", // Subkey, change accordingly

0, // Reserved

KEY_ALL_ACCESS, // desired access rights to the key

&hKey // variable that receives a handle to the opened key

);

if(lRet == ERROR_SUCCESS)

printf("RegOpenKeyEx() is OK.\n");

else

printf("RegOpenKeyEx() is not OK.\n");

QueryKey(hKey);

RegCloseKey(hKey);

return 0;

}

 

Output example:

 

RegOpenKeyEx() is OK.

Subkey Names:

(1) AGP

(2) Arbiters

(3) BackupRestore

(4) Biosinfo

(5) BootVerificationProgram

(6) Class

(7) CoDeviceInstallers

(8) COM Name Arbiter

(9) ComputerName

(10) ContentIndex

(11) ContentIndexCommon

(12) CrashControl

(13) CriticalDeviceDatabase

(14) DeviceClasses

(15) FileSystem

(16) GraphicsDrivers

(17) GroupOrderList

(18) HAL

(19) IDConfigDB

(20) Keyboard Layout

(21) Keyboard Layouts

(22) Lsa

(23) MediaCategories

(24) MediaInterfaces

(25) MediaProperties

(26) MediaResources

(27) Network

(28) NetworkProvider

(29) Nls

(30) NTMS

(31) PnP

(32) Print

(33) PriorityControl

(34) ProductOptions

(35) SafeBoot

(36) ScsiPort

(37) SecurePipeServers

(38) SecurityProviders

(39) Server Applications

(40) ServiceGroupOrder

(41) ServiceProvider

(42) Session Manager

(43) Setup

(44) StillImage

(45) SystemResources

(46) Terminal Server

(47) TimeZoneInformation

(48) Update

(49) UsbFlags

(50) Video

(51) VirtualDeviceDrivers

(52) Watchdog

(53) Windows

(54) WMI

(55) WOW

(56) hivelist

(57) ServiceCurrent

Number of subkeys: 57

(1) Value Name: CurrentUser.

(2) Value Name: WaitToKillServiceTimeout.

(3) Value Name: SystemStartOptions.

(4) Value Name: SystemBootDevice.

Number of values: 4

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index