Reading And Writing Windows Registry Using WinAPI

Windows Registry

If you are the windows application or driver developer, then might be you need to access the windows registry. In this article, I will describe the way to how to create and access the key in windows registry. Here, I am assuming you are familiar with windows internals and API. If you are not familiar with windows internal and API, see this popular course: Windows Internals

Below find the list of some WinAPI that I am using to create and access the windows registry key:
  • RegOpenKeyEx
  • RegCreateKeyEx
  • RegSetValueEx
  • RegQueryValueEx
  • RegCloseKey

You can find here a complete list of registry functions – MSDN.

Note: To access the windows registry you should have the admin rights.

Before creating the key we need to understand the windows registry hives. The hives are the group of registry keys, subkey, and the registry values.

You can see the registry hives in registry editor left-hand side of the screen. You can open the registry editor to running the command regedit in the search box or Run window.

Here is a list of some common registry hives in Windows:
  • HKEY_CLASSES_ROOT
  • HKEY_CURRENT_USER
  • HKEY_LOCAL_MACHINE
  • HKEY_USERS
  • HKEY_CURRENT_CONFIG

I think now time to see the example code. In this example code, I will create a key and read/write the value.

How to create a key under the hives:

In this code, you just need to pass the registry hives and key name which you want to create. If everything is fine then this function creates the key under the given hives.

 

Write a DWORD value into the created key:

In this function, you need to pass the hives name, key name, value name and DWORD value which you want to store in the key. In this function, I am opening the key and just write the value. If everything fines then the value will store in the registry.

 

If you love online courses, then here is a good c language course for you from the Pluralsight, 10 days trial is Free.

C tutorial

Write a string into the created key:

In this function, you need to pass the hives name, key name, value name, and string which you want to store in the key. Here one thing is to need to remember the size of wide char is 16 bits, so you need to be careful before writing the string into the windows registry.

 

Read a DWORD value from the created key:

Before reading the value from a key, you should open it first. You require hives name, key name and value name to read the DWORD.

 

Read a string from the created key:

Similar to above method. You require hives name, key name and value name to read the string from the key. Before reading the string you need to give the proper length of the string either you will get an error.

 

To understand the above methods let see an example code, In below example, I have created a key “Aticleworld” and two value “date” and “Message”. I will store and read the stored value from the key using the above-described methods.

windows registry key

 

 



One comment

Leave a Reply