Home   Index   About
Ultimate Pack


Custom Search
Using the Registry

The following example demonstrates the use of the RegQueryInfoKey, RegEnumKey, and RegEnumValue functions. The hKey parameter passed to each function is the handle of an open key. This key must be opened before the function call and closed afterward.

// QueryKey - enumerates the subkeys of a given key and the associated

// values and then copies the information about the keys and values

// into a pair of edit controls and list boxes.

// hDlg - dialog box that contains the edit controls and list boxes

// hKey - key whose subkeys and values are to be enumerated

VOID QueryKey(HWND hDlg, HANDLE hKey)

{

CHAR achKey[MAX_PATH];

CHAR achClass[MAX_PATH] = ""; /* buffer for class name */

DWORD cchClassName = MAX_PATH; /* length of class string */

DWORD cSubKeys; /* 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, j;

DWORD retCode, retValue;

CHAR achValue[MAX_VALUE_NAME];

DWORD cchValue = MAX_VALUE_NAME;

CHAR achBuff[80];

// Get the class name and the value count.

RegQueryInfoKey(hKey, /* key handle */

achClass, /* buffer for class name */

&cchClassName, /* length 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 */

SetDlgItemText(hDlg, IDE_CLASS, achClass);

SetDlgItemInt(hDlg, IDE_CVALUES, cValues, FALSE);

SendMessage(GetDlgItem(hDlg, IDL_LISTBOX),

LB_ADDSTRING, 0, (LONG) "..");

// Enumerate the child keys, looping until RegEnumKey fails. Then

// get the name of each child key and copy it into the list box.

SetCursor(LoadCursor(NULL, IDC_WAIT));

for (i = 0, retCode = ERROR_SUCCESS;

retCode == ERROR_SUCCESS; i++) {

retCode = RegEnumKey(hKey, i, achKey, MAX_PATH);

if (retCode == (DWORD)ERROR_SUCCESS)

SendMessage(GetDlgItem(hDlg, IDL_LISTBOX),

LB_ADDSTRING, 0, (LONG) achKey);

}

SetCursor(LoadCursor (NULL, IDC_ARROW));

// Enumerate the key values.

SetCursor(LoadCursor(NULL, IDC_WAIT));

if (cValues)

for (j = 0, retValue = ERROR_SUCCESS;

j < cValues; j++) {

cchValue = MAX_VALUE_NAME;

achValue[0] = '\0';

retValue = RegEnumValue(hKey, j, achValue,

&cchValue,

NULL,

NULL, /* &dwType, */

NULL, /* &bData, */

NULL); /* &bcData */

if (retValue != (DWORD) ERROR_SUCCESS &&

retValue != ERROR_INSUFFICIENT_BUFFER) {

wsprintf (achBuff,

"Line:%d 0 based index = %d, retValue = %d, "

"ValueLen = %d",

__LINE__, j, retValue, cchValue);

MessageBox (hDlg, achBuff, "Debug", MB_OK);

}

achBuff[0] = '\0';

// Add each value to a list box.

if (!lstrlen(achValue))

lstrcpy(achValue, "<NO NAME>");

wsprintf(achBuff, "%d) %s ", j, achValue);

SendMessage(GetDlgItem(hDlg,IDL_LISTBOX2),

LB_ADDSTRING, 0, (LONG) achBuff);

}

SetCursor(LoadCursor(NULL, IDC_ARROW));

}


Last news from Greatis Software

Nostalgia .Net     Nostalgia .Net     .Net is powerful, but not all-powerful, so sometimes we need to use Win32 API for our .Net applications. It's simple enough with Platform Invoke if you have Win32 skill, but we do not always have time to dig the ancient documentation, declare the special types that are compatible with Win32, find the values of the Win32's constants and so on. Nostalgia .Net offers several simple-to-use classes, and components that will allow you to forget about the headache of Win32 and just use the power of Win32 in your application the same way as you use the native. Net classes.  More »

Recommended software for developers

Ultimate Pack for Delphi and C++ Builder     Ultimate Pack     Component pack for Delphi and C++ Builder that contains runtime form designer, runtime object inspector, print suite and much more for the very special price.  More »

Form Designer .Net     Form Designer .Net     Unique runtime form design solution that allows to edit any form in .Net WinForms application at runtime with full source codes for only 300 euro!  More »

Print Suite .Net     Print Suite .Net     Print Suite .Net is a set of components for easy printing texts, images and grids from your WinForms applications. Full C# source codes are available  More »

Gradient Controls .Net     Gradient Controls .Net     Gradient Controls .Net offers controls with gradient background feature. Labels, panels and so on... Full C# source codes are available  More »

iGrid     Greatis iGrid     iGrid plots drawing grid right over your desktop, so you can use it everywhere, with any drawing application without any special plugins for different graphic editors.  More »


All the contacts and projects

Dmitry Vasiliev (just.dmitry)

Related Links

Software for Visual Studio .NET developers
Software for Delphi and C++ Builder developers
Software for Visual Basic 6 developers
Delphi Tips&Tricks
MegaDetailed.NET

More Online Helps

Win32 Programmer's Reference
Win32 Multimedia Programmer's Reference
OLE Programmer's Reference
Microsoft Windows Pen API Programmer's Reference
Microsoft Windows Sockets 2 Reference
Microsoft Windows Telephony API (TAPI) Programmer's Reference
Unix Manual Pages

Free Tech Secrets ;) Copyright © 2008-2012 Free Tech Secrets ;) greatis just4fun network just4fun