Working with Windows Registry using Windows API – Part 3 (How to Create a Registry Key)

By | August 18, 2015
  In the previous article, we discussed how to create a backup of your registry. We would like to remind you that the Windows registry is a very critical part of Windows OS and therefore you must know how to backup the whole registry or a part of it, so that you can recover your computer from any undesirable situation in case a missing or modified registry key causes you trouble. We suggest you take a look at How to Backup the Registry before proceeding, because it is very important to learn before working with the registry. Here, we shall discuss how to create a registry key.

How to Create a Registry Key

Here, we shall show you how to use Windows API functions to create a new registry key.

You may recall that the function used for creating new registry keys is RegCreateKeyEx(). Its syntax is :
LONG RegCreateKeyEx (HKEY hKey, LPCTSTR lpSubKey, DWORD Reserved, LPTSTR lpClass, DWORD dwOptions, REGSAM samDesired, LPSECURITY_ATTRIBUTES lpSecurityAttributes, PHKEY phkResult, LPDWORD lpdwDisposition)

Here, the various parameters have the following meanings :

  • hKey is a handle of the type of HKEY.
  • lpSubKey is the name of the new subkey under the open key indicated by the handle.
  • lpClass is a user-defined class type for the key. NULL value is recommended for this parameter.
  • dwOptions flag is usually REG_OPTION_NON_VOLATILE. Another option is REG_OPTION_VOLATILE. The difference is that ‘non-volatile’ registry information is stored in a file and hence it remains in the computer even after the computer restarts. On the other hand, ‘volatile’ registry information is temporarily stored in RAM and is deleted as soon as the computer shuts down.
  • 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.
  • lpSecurityAttributes can be NULL or can point to a security attribute. The other possible values are the same as samDesired in point 5 above.
  • lpdwDisposition points to a DWORD that indicates whether the key already existed or was created (REG_OPENED_EXISTING_KEY or REG_CREATED_NEW_KEY).

For example,

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

Sample Program to create a registry key
#include <windows.h>
#include <iostream>

using namespace std;

HKEY CreateKey (HKEY hRootKey, wchar_t* strKey)
HKEY hKey;
LONG nError = RegOpenKeyEx (hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);

cout << "Creating the specfied registry key... " << strKey << endl;
nError = RegCreateKeyEx (hRootKey, strKey, NULL, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL);

if (nError)
cout << "Error: " << nError << " Could not create the specfied key !!!" << strKey << endl;
cout << "The specfied registry key was created successfully...!!!" << endl;
return hKey;

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 << " The key was created but the registry value could not be set !!! " << (char*) lpValue << endl;

int main()
static DWORD v1, v2;

HKEY hKey = CreateKey (HKEY_LOCAL_MACHINE, L"SOFTWARE\Windows Code Bits");

v1 = 15;
v2 = 3;

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

cout << endl;
cout << "Press Any Key to Exit...";
return 0;


How does this program work ?

a) First of all, the main() function calls a user-defined function named CreateKey() with two arguments, i.e. (i) the root key and (ii) the key that we want to create.b) The CreateKey() function calls RegOpenKeyEx() to check whether the key that we want to create already exists. If this is the case, the function skips the next few statements and returns the handle to that key. If no such key exists, a new key is created with the name specified. The new key is created in HKEY_LOCAL_MACHINE\SOFTWARE\Windows Code Bits.

c) Now, the control returns back to the main() function. Here, two values (namely, ‘Website’ and ‘Blog’) are created for the key that we just created and data is fed into them using a user-defined function named SetVal().
d) The SetVal() function calls RegSetValueEx() to set the data in each value. Both values are of REG_DWORD type and their values are 15 and 3 respectively.

The following video shows the above program creating a new registry key and setting its values :

You can download this program i.e. RegCreate.cpp here
You can download the executable i.e. RegCreate.exe here

Please note that above executable is 64-bit and will not work on 32-bit editions of Windows. To create a 32-bit executable for your 32-bit Windows PC, you need to build the program as 32-bit in Visual Studio or any other IDE that you use.

So, we hope you understood how to create a registry key using the Windows API. The next article will discuss how to open, edit and delete registry keys using Windows API functions.

(Visited 318 times, 1 visits today)

Leave a Reply

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