VB Access Windows Registry via API

zhaozj2021-02-17  71

Do you want your program to access the Windows registry? Of course, you can access the huge Windows registry is what you want every programmer, then I will tell you how to access the Windows Registry through the API function. Please take a look at the Visual Basic program below:

'Root bond 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

'Declaration about API functions

Private Declare Function RegreateKey Lib "Advapi32.dll" Alias ​​"regcreateKeya" _

(_ _

Byval HKey as long, _

Byval lpsubkey as string, _

Byref PhkResult As Long_

) AS long 'builds 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 'opens a primary key

Private Declare Function RegdeleTeKey Lib "Advapi32.dll" Alias ​​"RegdeleteKeya "_

(_ _

Byval HKey as long, _

Byval lpsubkey as string_

) AS long 'deletes 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, and LPDATA should be changed from the default byref type to 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 'querying a key value, LPDATA should be changed from the default byref type to BYVAL type

Private Declare Function RegdeleteValue Lib "Advapi32.dll" Alias ​​"RegdeleteValuea" _

(_ _

Byval HKey as long, _

Byval lpvaluename as string _

) AS long 'deletes a key value

'

Sub main ()

DIM NKEYHANDLE AS Long, NValeType 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

Take a look at the results of the program:

There is a New Registry Key primary key in the hkey_current_user root key of the registry. In addition to an empty "default) value, there is a" My Value "value" I am A Winner! "

Let's take a look at this program:

The first is defined constants. The four constants in front are the handle of each root key, which is fixed; the three is the key value type, and the "New" menu item pop-up in the registry editor can see these three A key value type.

The second step is to declare the API function. This step can be done by "API Text Browser": Use "API Text Browser" to load file "WIN32API.txt", find the API function in the above example and add it to the "Selected Item" column. "Copy" button back to the VB program editing environment, paste the content in the clipboard into the VB program editor, which completes the declaration process of the API function. It is worth noting that "LPDATA" in "RegSetValueex" and "RegQueryValueex" two functions is defined as "any" type, but it is not defined to pass the delivery mode, so it is default "byref" delivery. After testing, this is not the correct setting or query key value, but it is OK after it is changed to "Byval".

The third step is to access the registry. "Regopenkey" usage is similar to "RegcreateKey", the former is used to open an existing primary key, and the latter can create this primary key if the primary key can be used to open an existing primary key. . When the primary key is turned on or created, a handle will return to the parameter "phkResult", which will be used in the operation of the key value, which is "nkeyhandle" parameters in "RegqueryValueex", "RegQueryValueex", "Regdeletevalue". There is also a point to explain that there is a key value name in each primary key "(default)", but its key value name is not "(default)", but an empty string. Now, do you know how to access the Windows registration table through the API function?

= - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - = - // __________ / lb / ___ outinn / _ [] _ ​​/ ____ // _________ / | () | / _ / http://outinn.yeah.net/ | ____ / - | __ | - / | Welcome To Visit Outinn! | __ | == | ___ | || | __ | - = - = - = - | _ || _ | = - fancy, Outinn@china.com

转载请注明原文地址:https://www.9cbs.com/read-31332.html

New Post(0)