Working with Windows Registry using Windows API – Part 4 (How to Open, Edit or Delete a Registry Key)

  In the previous ‘Working with Registry’ article, we discussed How to Create a Registry Key. As you may recall, you need to first check whether a key with the same name already exists at the specified location. If there already exists a key with the same name, you can’t create a new key with that name at that location; if no such key exists, the new key will be created. Here, we shall discuss how to open, edit or delete a registry key.

How to Open, Edit or Delete a Registry Key

Here we shall discuss how to open, edit or delete a registry key using Windows API functions. As you might have guessed, the first two tasks i.e. opening a registry key and editing it – are basically the same. In both cases, you need to open an existing key and read its values. The only difference is that you need to modify those values in the second case. In the third case i.e. registry key deletion, you don’t need to open the key. You just call the key deletion function and the job is done.
 

Now, we shall first discuss how to open a registry key.

To open a registry key and read its contents, you need to call RegOpenKeyEx(). Its syntax is :

LONG RegOpenKeyEx (HKEY hKey, LPCTSTR lpSubKey, DWORD ulOptions, REGSAM samDesired, PHKEY phkResult)

Here, the various parameters have the following meanings :

1. hKey defines a currently open key or one of the predefined reserved key handles.

2. lpSubKey is the subkey name you want to open. The subkey name can be a path, such as Microsoft\Windows NT\CurrentVersion. If NULL is used as the subkey name, a new, duplicate key for hKey is opened.

3. ulOptions is reserved and must be zero.

4. samDesired is is the access mask describing the security for the new key. Possible values are KEY_ALL_ACCESS, KEY_WRITE, KEY_QUERY_VALUE, KEY_ENUMERATE_SUBKEYS.

5. phkResult points to points to a variable of type HKEY that is to receive the handle to the newly opened key.

For Example,

nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);

 
Sample program to Open a Registry Key
 
#include <windows.h>
#include <iostream>

using namespace std;


HKEY OpenKey(HKEY hRootKey, wchar_t* strKey)
{
HKEY hKey;
LONG nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);
if (nError)
cout << “Error: ” << nError << ” Could not find the specified key  ” << strKey << endl;
return hKey;
}

DWORD GetVal(HKEY hKey, LPCTSTR lpValue)
{
DWORD data;
DWORD size = sizeof(data);
DWORD type = REG_DWORD;
LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);if (nError == ERROR_FILE_NOT_FOUND)
data = 0;
else if (nError)
cout << “Error: “<< nError << ” Could not get the registry value ” << (char*)lpValue << endl;
return data;
}

int main()
{
static DWORD v1, v2;
HKEY hKey = OpenKey(HKEY_LOCAL_MACHINE, L”SOFTWARE\Windows Code Bits”);
v1 = GetVal(hKey, L”Website”);
v2 = GetVal(hKey, L”Blog”);
cout << ” The values of the opened key are displayed below : ” << endl;
cout << endl;
cout << ” Website : ” << v1 << endl;
cout << ” Blog : ” << v2 << endl;
/* You can add the code to edit registry key values here */

RegCloseKey(hKey);
cout << endl;
cout << “Press Any Key to Exit… “;
getchar();
return 0;
}

How does this program work?

a) First of all, the main() function calls a user-defined function named OpenKey() with two arguments, i.e. (i) the root key and (ii) the key that we want to create.
b) The OpenKey() function calls RegOpenKeyEx() to check whether the key that we want to open exists. If it does, the function returns the handle to that key. If the key doesn’t exist, an error message is displayed. Steps c and d are relevant only if the key exists and is successfully opened.
c) Now, the control returns back to the main() function. Here, we check the data stored in the two values (namely, ‘Website’ and ‘Blog’) of the key. The data contained in these values is then copied to the variables v1 and v2 respectively.
d) Now these values, alongwith the data they contain, are displayed in the console.

How to Edit a Registry Key

As stated above, the programs for opening and editing a registry key are almost identical. To use the above program to edit a registry key, all you have to do is to add the following code to it :

Add the following function i.e. SetVal() above main() :

void SetVal(HKEY hKey, LPCTSTR lpValue, DWORD data)
{
LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
if (nError)
cout << “Error: ” << nError << ” Could not set registry value: ” << (char*)lpValue << endl;
}


Add the following statements in the main() at the location specified in the above program:

v1 += 10;
v2 += 12;


SetVal(hKey, L”Website”, v1);
SetVal(hKey, L”Blog”, v2);


You have seen the program to open/edit a registry key in the above program. Now, you shall learn how to delete a registry key.

To delete a registry key, you just need to call RegDeleteKey() with appropriate arguments. The syntax of RegDeleteKey () is described below :

LONG RegDeleteKey (HKEY hKey, LPCTSTR lpSubKey)

Here the various parameters have the following meanings :

1. hKey is an open key handle.

2. lpSubKey is a subkey name.


For example,

nError = RegDeleteKey (HKEY_LOCAL_MACHINE, L”SOFTWARE\Windows Code Bits”);

Sample program to delete a registry key

#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
LONG nError = RegDeleteKey (HKEY_LOCAL_MACHINE, L”SOFTWARE\Windows Code Bits”);
if (nError)
cout << ” Error: ” << nError << ” Could not delete the specified registry key !!!” << endl;
else
cout << ” The specified registry key has been deleted successfully…!!!” << endl;cout << endl;
cout << “Press Any Key to Exit…”;
getchar();
return 0;
}
 
How does this program work?

a) This program calls the Windows API function RegDeleteKey() with two arguments : (i) the root key and (ii) the key that we want to create.

b) The specified registry key is deleted and a message is displayed indicating the same.

The following video displays how all these programs work :


You can download the above mentioned programs at the links below :

RegOpen.cpp
RegDelete.cpp

You can download the executables of the above programs at the links given below :

RegOpen.exe
RegDelete.exe

Please note that these executables are 64-bit and will not work on 32-bit editions of Windows. To make these programs work on a 32-bit computer, you need build these programs as 32-bit using Visual Studio or any other IDE you use.

We hope you understood how to open, edit or delete a registry key. Let us know whether this article helped you (in the comments section).

(Visited 255 times, 1 visits today)

2 comments

Leave a Reply

Your email address will not be published. Required fields are marked *