Do you want your program to access the Windows registry? Of course, accessing a huge Windows Registry is what every programmer wants, so I will tell you how to access the Windows registry through API functions. Take a look at the following Visual Basic Program:
'Root key constant
Const HKEY_CLASSES_ROOT =-2147483648 #
Const HKEY_CURRENT_USER =-2147483647 #
Const HKEY_LOCAL_MACHINE =-2147483646 #
Const HKEY_USERS =-2147483645 #
'Key Value Type
Const REG_SZ = 1 & 'string value
Const REG_BINARY = 3 & 'binary Value
Const REG_DWORD = 4 & 'dword Value
'Declare related API functions
Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA "_
(_
ByVal hKey As Long ,_
ByVal lpSubKey As String ,_
ByRef phkResult As Long _
) As Long 'create a new primary key
Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA "_
(_
ByVal hKey As Long ,_
ByVal lpSubKey As String ,_
ByRef phkResult As Long _
) As Long 'open a primary key
Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA "_
(_
ByVal hKey As Long ,_
ByVal lpSubKey As String _
) As Long 'delete a primary key
Private Declare Function RegCloseKey Lib "advapi32.dll "_
(_
ByVal hKey As Long _
) As Long 'close a primary key
Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA "_
(_
ByVal hKey As Long ,_
ByVal lpValueName As String ,_
ByVal Reserved As Long ,_
ByVal dwType As Long ,_
ByVal lpData As Any ,_
ByVal cbData As Long _
) As long 'creates or changes a key value. lpdata should be changed from the default byref type to the byval type.
Private declare function regqueryvalueex lib "advapi32.dll" alias "regqueryvalueexa "_
(_
Byval hkey as long ,_
Byval lpvaluename as string ,_
Byval lpreserved as long ,_
Byref lptype as long ,_
Byval lpdata as any ,_
Byref lpcbdata as long _
) As long: query a key value. lpdata should be changed from the default byref type to the byval type.
Private declare function regdeletevalue lib "advapi32.dll" alias "regdeletevaluea "_
(_
Byval hkey as long ,_
Byval lpvaluename as string _
) As long 'Delete A key value
'Main Process
Sub main ()
Dim nkeyhandle as long, nvaluetype as long, nlength as long
Dim svalue as string
SValue = "I am a winner! "
Call RegCreateKey (HKEY_CURRENT_USER, "New Registry Key", nKeyHandle)
Call RegSetValueEx (nKeyHandle, "My Value", 0, REG_SZ, sValue, 255)
SValue = Space (255)
NLength = 255
Call RegQueryValueEx (nKeyHandle, "My Value", 0, nValueType, sValue, nLength)
MsgBox sValue
Call RegDeleteValue (nKeyHandle, "My Value ")
Call RegDeleteKey (HKEY_CURRENT_USER, "New Registry Key ")
Call RegCloseKey (nKeyHandle)
End Sub
Let's see the program running result:
A New Registry Key primary Key is added to the HKEY_CURRENT_USER root Key in the Registry. In addition to an empty "(default)" value, the value is "I am a winner !" "My Value" Value
Next we will analyze this program:
First, define constants. The first four constants are the handles of each root key, which are fixed. The last three are key value types. In the Registry Editor, right-click the "new" menu item popped up by the primary key, the three key value types are displayed.
The second step is to declare the API function. This step can be completed through the "API text Browser": use the "API Browser" to browse the win32api.txt file, find the API function in the preceding example, and add it to the "selected items" column, click the copy button, return to the VB program editing environment, and paste the content in the clipboard to the VB program editor. This completes the declaration process of the API function. It is worth noting that the "lpData" in the "RegSetValueEx" and "RegQueryValueEx" functions is defined as "Any", but there is no defined transfer method, therefore, it is passed as "ByRef" by default. The author tests that the key value cannot be correctly set or queried, but the transfer method is changed to "ByVal", and then OK.
The third step is to access the registry. The usage of "RegOpenKey" is similar to that of "RegCreateKey". The former is used to open an existing primary key, and the latter is used to open an existing primary key, if this primary key does not exist, you can also create this primary key. After the primary key is opened or created successfully, a handle is returned to the parameter "phkResult". This handle is used in key-value operations, it is the "nKeyHandle" parameter in "RegSetValueEx", "RegQueryValueEx", and "RegDeleteValue. Note that each primary key has a key value name displayed as "(default)", but its key value name is not "(default)", but an empty string.
Now, do you know how to access the Windows registry through API functions?
-= -- =-