Working with Windows Registry using Windows API – Part 4 (How to Open, Edit or Delete a Registry Key)
How to Open, Edit or Delete a Registry Key
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.
nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);
using namespace std;
HKEY OpenKey(HKEY hRootKey, wchar_t* strKey)
LONG nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);
cout << “Error: ” << nError << ” Could not find the specified key ” << strKey << endl;
DWORD GetVal(HKEY hKey, LPCTSTR lpValue)
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;
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;
cout << endl;
cout << “Press Any Key to Exit… “;
How does this program work?
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));
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.
nError = RegDeleteKey (HKEY_LOCAL_MACHINE, L”SOFTWARE\Windows Code Bits”);
Sample program to delete a registry key
using namespace std;
LONG nError = RegDeleteKey (HKEY_LOCAL_MACHINE, L”SOFTWARE\Windows Code Bits”);
cout << ” Error: ” << nError << ” Could not delete the specified registry key !!!” << endl;
cout << ” The specified registry key has been deleted successfully…!!!” << endl;cout << endl;
cout << “Press Any Key to Exit…”;
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 :
You can download the executables of the above programs at the links given below :
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).