Finding the registry key/subkey name and their data values



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 or use #pragma comment(lib, "netapi32.lib")

To do: Finding registry key/subkey name and their data values

To show: The various registry C functions used in Windows system programming



// #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


// The header files

#include <windows.h>

#include <stdio.h>


// Finding the policy and preference keys. Change accordingly...

#define POLICY_KEY TEXT("Software\\Policies\\Microsoft\\Windows\\Explorer")

#define PREFERENCE_KEY TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer")


DWORD ReadValue(LPTSTR lpValueName, DWORD dwDefault)


HKEY hKey;

LONG lResult;

DWORD dwValue, dwType, dwSize = sizeof(dwValue);


// First, check for a policy.

lResult = RegOpenKeyEx(HKEY_CURRENT_USER, POLICY_KEY, 0, KEY_READ, &hKey);

if(lResult == ERROR_SUCCESS)


lResult = RegQueryValueEx(hKey, lpValueName, 0, &dwType, (LPBYTE)&dwValue, &dwSize);



// Exit if a policy value was found.

if(lResult == ERROR_SUCCESS)


printf("Policy: value found!\n");


// return the data value

return dwValue;



printf("Policy: value not found!\n");


// Second, check for a preference.


if(lResult == ERROR_SUCCESS)


lResult = RegQueryValueEx(hKey, lpValueName, 0, &dwType, (LPBYTE)&dwValue, &dwSize);

RegCloseKey (hKey);


// Exit if a preference was found.

if(lResult == ERROR_SUCCESS)


printf("Preference: value found!\n");

// Return the data value

return dwValue;



printf("Preference: value not found!\n");


// Neither a policy nor a preference was found; return the default value.

return dwDefault;



int main(void)


// key string to find...

LPTSTR lpValueName = L"Browse For Folder Height";

DWORD dwDefault = 0x00000000;


DWORD ret = ReadValue(lpValueName, dwDefault);

printf("The value data for the \'%S\' value name is 0X%.8X(%d).\n", lpValueName, ret, ret);

return 0;



Output example:


Policy: value not found!

Preference: value found!

The value data for the 'Browse For Folder Height' value name is 0X00000120(288).

Press any key to continue . . .




Verify the name and the data. Open registry editor. Go to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer. You can see the 'Browse For Folder Height' name and the 0X00000120(288) data on the right pane. Your machine may show different data value.



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