Click here to Skip to main content
15,867,141 members
Articles / Desktop Programming / MFC
Article

Registry List Control

Rate me:
Please Sign up or sign in to vote.
4.76/5 (27 votes)
10 Jan 20034 min read 136.1K   2.2K   52   24
A List Control to Monitor Windows Registry

Sample Image - RegistryListCtrl.jpg
Figure 1

Introduction

Every programmer knows about the Registry. Registry is a database to store data related to programs. Some very important information about installed hardware and software, are stored by Windows in registry. Windows has a utility to browse registry and make changes to it called regedit.exe, this is registry editor program. Figure 2 shows it.

Windows Registry Editor
Figure 2

I wrote some utility classes to demonstrate all of Registry capabilities. Using these classes are very easy. Windows Registry Editor (regedit.exe) has these capabilities:

  • A tree to show hierarchy of Registry (Keys and Sub-Keys)
  • A list to show Key's Value (Value name, Type of value and Value Data)

My classes support part two of registry editor capabilities (A list control to show Registry Values).

How to use

First of all add these files to your project

  • HexEditBase.h, HexEditBase.cpp (for editing Binary values). I should thank Ch. Kuendig for his HexEditor that I used to complete my task.
  • RegEditBinaryDialog.h, RegEditBinaryDialog.cpp
  • RegEditDWORDDialog.h, RegEditDWORDDialog.cpp
  • RegEditStringDialog.h, RegEditStringDialog.cpp
  • RegistryListCtrl.h, RegistryListCtrl.cpp (main class)

Also add resources needed by these classes (Menu and Bitmap).

Dialog Based Programs:

Add a List Control to your dialog. Use Class Wizard to add a member variable with variable type CListCtrl. Go to your dialog header file and rename your variable type from CListCtrl to CRegistryListCtrl. Remember to add RegistryListCtrl.h header file at top of your dialog definitioin. Now compile it. If no error occurs, everything goes right.

CRegistryListCtrl in action

When your program is running (like figure 1), and a key is open, CRegistryListCtrl shows you all of value names stored in that key. The list shows items with full detail of them (eg. type of value and also value data). Every value name has at least one of these types:

  • String type known as REG_SZ for storing string values
  • DWORD type known as REG_DWORD for storing integer values
  • Binary type known as REG_BINARY for storing binary values. Generally speaking, structures or other complex data types!

CRegistryListCtrl can change values of all that three types by it's dialogs. Figure 3, 4 and 5 show you these dialogs.

Edit String value
Figure 3 - Edit a string value

Edit DWORD value
Figure 4 - Edit a DWORD value

Edit Binary value
Figure 5 - Edit a Binary value (HEX format)

For complete compatibility with Regedit.exe, I add some another features like adding menu support. These menus should be appear when a user pressed right click of mouse. These two menus are shown in figures 6 and 7. Menu 1 can add new items (and also new keys) and menu 2 shows a way to modify existing values.

Adding new values by this menu
Figure 6 - Adding new items

Modify existing values
Figure 7 - Modify existing values

CRegistryListCtrl has written to monitor changes in values on the fly. In other hand, if any changes occured by another programs or by regedit.exe, CRegistryListCtrl shows it immediately! I use a thread to monitor specified key and report changes to CRegistryListCtrl.

Working with CRegistryListCtrl

CRegistryListCtrl has only one public member function: OpenRegKey():

void OpenRegKey(HKEY hMasterKey, CString SubKey)

hMasterKey is Handle to a currently open key or any of the following predefined reserved handle values:

  • HKEY_CLASSES_ROOT
  • HKEY_CURRENT_CONFIG
  • HKEY_CURRENT_USER
  • HKEY_LOCAL_MACHINE
  • HKEY_USERS

and SubKey is your desired key.

Example

CRegistryListCtrl m_RegList;
m_RegList.OpenRegKey(HKEY_LOCAL_MACHINE, 
  "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");

All of other things will handle by CRegistryListCtrl automatically.

How does CRegistryListCtrl Monitor Registry?

CRegistryListCtrl has a static member function to monitor specified key of Registry. I used this static member function with a thread after calling OpenRegKey() or when you refresh the list by pressing F5. This member function, named NotifyChangeRegKey() notifies CRegistryListCtrl when any changes occured to Registry.

Body of this static member function is as below:

//static member function used with thread to monitor Registry changes
UINT CRegistryListCtrl::NotifyChangeRegKey(void* pParam)
{
  //if thread is running, do not run it again.
  if (RunThread==FALSE)
    RunThread=TRUE;
  else
    return 0;     //exit the thread

  DWORD  dwFilter = REG_NOTIFY_CHANGE_LAST_SET; 

  CRegistryListCtrl* pThread=(CRegistryListCtrl*) pParam;

  HANDLE hEvent;
  HKEY   hKey;
  LONG   lErrorCode;
  
  // Open a key.
  lErrorCode = RegOpenKeyEx(pThread->m_hKey, pThread->m_SubKey, 
    0, KEY_NOTIFY, &hKey);
  if (lErrorCode != ERROR_SUCCESS)
    return 0;
    
  // Create an event.
  hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
  if (hEvent == NULL)
    return 0;
    
  // Watch the registry key for a change of value.
  lErrorCode = RegNotifyChangeKeyValue(hKey, TRUE, 
    dwFilter, hEvent, TRUE);
  if (lErrorCode != ERROR_SUCCESS)
    return 0;
    
  //Wait for an event to occur. In other hand
  //any changes occured to Registry!
  DWORD Result;
  Result=WaitForSingleObject(hEvent, INFINITE);
  
  // Close the key.
  lErrorCode = RegCloseKey(hKey);
  if (lErrorCode != ERROR_SUCCESS)
    return 0;
  
  // Close the handle.
  if (!CloseHandle(hEvent))
    return 0;
  
  //Thread Finished!
  RunThread=FALSE;
  if (Result == WAIT_FAILED)
    return 0;
  else
  {
    //A change occured in Registry, List must be refreshed!
    pThread->Refresh();
    return 1;
  }
}

After opening any key, the tread fired to monitor Registry changes. I used the following statement to fire up the thread:

AfxBeginThread(NotifyChangeRegKey, this);

The first parameter is name of static member function, and second is a pointer to CRegistryListCtrl class. I used this technique for accessing member function and member variables of class within thread!

Future work

My future work is making a control to show hierarchy of Registry in a Tree. I will name it CRegistryTreeCtrl. In addition to showing a tree of Keys and Subkeys, this tree monitor Registry for any changes in it's keys or subkeys and apply this changes on the fly! By using CRegistryListCtrl and CRegistryTreeCtrl together, you can create regedit by yourself in 3 minutes. Enjoy it!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acquisition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competition, my articles are:


You can see list of my articles, by clicking here


Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:46
professionalManoj Kumar Choubey26-Feb-12 19:46 
GeneralTake more and more memory resource!! Pin
knife_wei8-Dec-09 1:11
knife_wei8-Dec-09 1:11 
You did a good job, but there is a bug in your program.
If I press the open button many times, this process will create more and more threads suspended in the WaitforSingleObject() and can't move on. From taskmanager, you can see the process take more and more memory after press the open button many times. Can you fix this bug? Thanks a lot.
Generalif i wait 5 minutes at RegNotifykeychangevalue function after i change value i dont get notification to hEvent Pin
Member 37611485-Aug-09 4:17
Member 37611485-Aug-09 4:17 
Generalinformation about installed hardware Pin
Tailana16-May-05 21:04
Tailana16-May-05 21:04 
GeneralRe: information about installed hardware Pin
Abbas_Riazi16-May-05 21:14
professionalAbbas_Riazi16-May-05 21:14 
GeneralRe: information about installed hardware Pin
Tailana16-May-05 22:16
Tailana16-May-05 22:16 
GeneralRe: information about installed hardware Pin
Abbas_Riazi16-May-05 23:38
professionalAbbas_Riazi16-May-05 23:38 
GeneralExcellent Job Pin
Shotgun24-Jan-05 13:11
Shotgun24-Jan-05 13:11 
GeneralRe: Excellent Job Pin
Abbas_Riazi24-Jan-05 20:21
professionalAbbas_Riazi24-Jan-05 20:21 
GeneralPosition of caret incorrect Pin
cjhsu36510-Jan-05 4:26
cjhsu36510-Jan-05 4:26 
GeneralRegistry Viewer Pin
Mishra Vikas23-Jan-04 2:24
Mishra Vikas23-Jan-04 2:24 
GeneralMemory leaks Pin
Paulo Rogério12-Jan-04 2:24
Paulo Rogério12-Jan-04 2:24 
GeneralGreat Job Pin
Simone Giannecchini2-Jan-04 0:40
Simone Giannecchini2-Jan-04 0:40 
GeneralRe: Great Job Pin
Abbas_Riazi2-Jan-04 3:13
professionalAbbas_Riazi2-Jan-04 3:13 
QuestionHow to monitor for changes? Pin
conrad Braam20-Aug-03 2:03
conrad Braam20-Aug-03 2:03 
AnswerRe: How to monitor for changes? Pin
Abbas_Riazi20-Aug-03 3:08
professionalAbbas_Riazi20-Aug-03 3:08 
GeneralNot bad. Good job. Pin
AhMan15-Jan-03 3:00
AhMan15-Jan-03 3:00 
GeneralPlease fix this Pin
Stephane Rodriguez.11-Jan-03 4:49
Stephane Rodriguez.11-Jan-03 4:49 
GeneralRe: Please fix this Pin
Abbas_Riazi11-Jan-03 17:41
professionalAbbas_Riazi11-Jan-03 17:41 
GeneralRe: Please fix this Pin
Anonymous12-Jan-03 5:58
Anonymous12-Jan-03 5:58 
GeneralRe: Please fix this Pin
real_ashwin10-Sep-03 1:41
real_ashwin10-Sep-03 1:41 
GeneralRe: Please fix this Pin
David Crow30-Oct-03 6:32
David Crow30-Oct-03 6:32 
GeneralRe: Please fix this Pin
BADMOFU10-Mar-05 10:18
BADMOFU10-Mar-05 10:18 
GeneralRe: Please fix this Pin
David Crow10-Mar-05 10:29
David Crow10-Mar-05 10:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.