October 22, 2016
Hot Topics:

Accessing the Windows Registry with the API

  • November 19, 2002
  • By John Percival
  • Send Email »
  • More Articles »

You can edit the Registry using a program called Regedit.exe. It is found in the Windows directory. It allows you to browse through the Registry, and make changes to it. This provides a very easy way to check that your app is doing what it should be doing in the correct places.

The Registry database model introduced last week was very much simplified. It suggested that the AppName parameter was specifying a 'drive' in the Registry. However, this area that VB writes to is in fact just another key. The real 'drives' are the entries that you see when you first start up Regedit. The two that you are most likely to use are HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE. In fact HKEY_CLASSES_ROOT is equal to HKEY_LOCAL_MACHINE\SOFTWARE\CLASSES, and HKEY_CURRENT_USER is the same as one of the entries in HKEY_USERS. VB in fact uses HKEY_CURRENT_USER\SOFTWARE\VB AND VBA PROGRAM SETTINGS to store the data.

The HKEY_CLASSES_ROOT key stores everything about documents, document readers, and how they relate to each other. It stores the information about extensions, and which applications are associated with the extension. It also stores information about DLLs and OCXs. See the article about the Registry and DLLs for more information. HKEY_LOCAL_MACHINE is where you will store most of your application's settings. The standard is:

If you have user specific settings, you can store it in similar sub keys in HKEY_CURRENT_USER. You should not really need any of the other keys, although in HKEY_DYN_DATA you can find some information about windows, similar to the things found in the System Monitor (sysmon.exe) utility. You cannot write to this key.

One thing that you must be careful of, if your users could be running NT, is that you have the necessary privileges to read and write to keys in the Registry.

So, as a proficient Windows programmer, you must know how to access and change all parts, not just the parts that VB provides you with. To do this, we must use the API. First, let's have some declarations:


Copy the following code into the declarations section of a module.

Public Const HKEY_CLASSES_ROOT = &H80000000
Public Const HKEY_CURRENT_USER = &H80000001
Public Const HKEY_LOCAL_MACHINE = &H80000002
Public Const HKEY_USERS = &H80000003
Public Const HKEY_CURRENT_CONFIG = &H80000005
Public Const HKEY_DYN_DATA = &H80000006
Public Const REG_SZ = 1 ' Unicode nul terminated string
Public Const REG_BINARY = 3 ' Free form binary
Public Const REG_DWORD = 4 ' 32-bit number
Public Const ERROR_SUCCESS = 0&

Public Declare Function RegCloseKey Lib "advapi32.dll" _
(ByVal hKey As Long) As Long

Public Declare Function RegCreateKey Lib "advapi32.dll" _
Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey _
As String, phkResult As Long) As Long

Public Declare Function RegDeleteKey Lib "advapi32.dll" _
Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey _
As String) As Long

Public Declare Function RegDeleteValue Lib "advapi32.dll" _
Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal _
lpValueName As String) As Long

Public Declare Function RegOpenKey Lib "advapi32.dll" _
Alias "RegOpenKeyA" (ByVal hKey As Long, ByVal lpSubKey _
As String, phkResult As Long) As Long

Public Declare Function RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName _
As String, ByVal lpReserved As Long, lpType As Long, lpData _
As Any, lpcbData As Long) As Long

Public Declare Function RegSetValueEx Lib "advapi32.dll" _
Alias "RegSetValueExA" (ByVal hKey As Long, ByVal _
lpValueName As String, ByVal Reserved As Long, ByVal _
dwType As Long, lpData As Any, ByVal cbData As Long) As Long

The main idea for reading and writing to the Registry is to find and select a key, read/write a value, and then close the key. This is all based around handles to keys. Handles to keys (a similar idea to window handles and device context handles) are numbers, which Windows uses to reference a file or part of memory; in this case, it is a key in the Registry.

One of the limitations of using the built in functions of VB is that you can only write strings to the Registry. However, with the API, you can write Long type numbers and Byte arrays, as well as several other types of string. We will now take you through how to read and write to anywhere in the Registry.

Page 2 of 6

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Thanks for your registration, follow us on our social networks to keep up-to-date
Rocket Fuel