Use VB to operate the Registry

Source: Internet
Author: User
Tags key string
Use VB to operate the Registry

Author: Unknown Source: China's VB network Release Date: 2004-12-20

Use VB to operate the Registry

If you are familiar with the Windows registry when there is a problem with windows, it will be easier to solve the problem; many commercialized software or specialized software will rewrite the registry for the first installation and running of the software on your machine, to become a programmer, you must master the technology of reading and writing the registry. Using a good registry will increase the color of your application. The following describes the Registry-related programming methods in VB.
Organization structure of the Registry
Before accessing the registry, you must first understand the Registry's organizational structure. The simplest way to understand the Registry's organizational structure is to start the "Registry Editor" provided by windows ", click the "run" command in the "Start" menu and enter Regedit. The "Registry Editor" window is displayed, as shown in 1 ).

Figure 1
● Key and subkey)
The structure of the Registry Editor is similar to that of the resource manager. Each folder icon in the left window represents a key, just as there are subfolders in the folder, and the registry key also has a subkey. To completely represent a sub-key, it is customary to use the path representation of the folder. For example, the "software" subkey under HKEY_LOCAL_MACHINE represents hkey_local _
Machine/software, and the "Microsoft" sub-key under "software" indicates hkey_local _
Machine/software/Microsoft (2 ).

Figure 2
● Value, value name, data, and default value)
When a key (or sub-key) is selected in the left window of the Registry Editor, The key value appears in the right window ), the key value can be divided into two parts: key name and data. Each key must contain at least one default value/
For example, the default value of the. BMP subkey is "acdc_bmp ". In addition to the default key value, this subkey also contains the name "content type" and Data "image/BMP" (3 ).

Figure 3
Provided by VB
Registry functions
After learning about the organization structure of the Registry, we will discuss how to access it. Just as you must specify the folder (directory) where the file is stored when accessing the file, you must specify the key first when accessing the registry. Key in the Registry Editor is a long string, such as "HKEY_LOCAL_MACHINE/software/
Microsoft/Windows/CurrentVersion ". Within Visual Basic 6.0, a standard registration location has been provided to store the program information of the application created in VB: HKEY_CURRENT_USER/software/Vb and VBA program settings "). VB provides two statements and two functions to process the program setting values stored in the Application Registration location:
Function getsetting (appname, section, key [, default]): retrieves the registry setting value.
Statement savesetting appname, section, key, value: Save or create the registry setting value.
Function getallsettings (appname, Section): returns an array containing multiple registry setting values.
Statement deletesetting appname, Section [, key]: Delete the registry setting value.
The preceding parameters are described as follows:
[]: Optional.
Appname: A string expression that contains the name of an application or project. It is a subkey in the standard position.
Section: A string expression that contains the region name. It is a subkey under appname.
Key: string expression, standard position/appname/
Value Name of the section subkey ).
Value: string expression, standard position/appname/
The Section subkey corresponds to the value of the key name ).
Default: expression. If no value is set in registry key settings, the default value is returned. If omitted, the default value is a string ("") with zero length.
Getallsettings returns variant, a two-dimensional array of strings. The two-dimensional array contains all the registry key setting values and their corresponding values in the specified area. If the appname or section does not exist, getallsettings returns the uninitialized VARIANT.
Instance
Create a project in VB6.0 and name it vbreg. VBP. delete all the forms, right-click the project resource manager, select Add module, and name it vbreg. Bas. Double-click Reg. Bas and enter the following code:
Dim avntsettings as Variant
Dim intx as integer
Avntsettings = getallsettings ("VB 6 API declaration loader", "File List ")
For intx = 0 to ubound (avntsettings, 1)
Debug. Print avntsettings (intx, 0), avntsettings (intx, 1)
Next intx
The above section first uses the getallsettings function to retrieve the values of the two registry items in the file list section of the "VB 6 API declaration loader" subkey and display the results in the immediate window. Before running, press <Ctrl> + <G> to make sure that the current window is displayed on the screen. Open the Registry to compare the key values of the standard position/VB 6 API declaration loader/file list with the results.
The following program uses the savesetting statement to create a sub-key named "My project/my sub-key" in the standard position, and then uses the getsetting function to obtain and display a set value. The getsetting function must return a value because the default parameter is input.
Note that the region name cannot be obtained using the getsetting function. Finally, use the deletesetting statement to delete the subkey.
Savesetting "My projects", "My subkeys", "top", 75
Savesetting "My projects", "My subkeys", "left", 50
Debug. Print "TOP", getsetting ("My projects", "My subkeys", "TOP", "25 ")
Debug. Print "left", getsetting ("My projects", "My subkeys", "left", "0 ")
'For ease of observation, debugging can set a breakpoint here and switch to the Registry. Press the <F5> key to refresh and you can see the created sub-key and its key value.
Deletesetting "My Projects" and "My subkeys"
'Switch to the Registry again after running and press the <F5> key to refresh. Check whether the created sub-keys and their key values are deleted.
Note: before running this program, make sure that "API text Browser" is automatically loaded when VB is started ", the "file" menu in the API text Browser contains a list of opened "text files. Otherwise, open "API text Browser" and select "Open Text File" under the "file" menu to open at least one text file or database.
Windows API registry Programming
Although VB itself provides four functions about the registry, these functions can only be stored in "HKEY_CURRENT_USER/
Read, delete, and modify key values under software/Vb and VBA programsettings. This can be used by general applications. What should I do if I want to access other primary keys or subkeys that are not "standard? In this case, you must use Windows API help.
In Windows, each key corresponds to a key handle (equivalent to a long integer, which is usually indicated by hkey in the program ), windows uses hkey to represent keys to make registry access more efficient, because Integer Operation Efficiency is better than string, so we should first understand how to obtain the key handle (that is, hkey ). Key at the top layer, including hkey_classes_root and hkey_current
_ User, HKEY_LOCAL_MACHINE, etc. The hkey values of these keys are fixed. The values are shown in the following table:
-----------------------
Key key handle
-----------------------
Hkey_classes_root & h80000000
Hkey_current_config & h80000005 HKEY_CURRENT_USER & h80000001
Hkey_dyn_data & h80000006
HKEY_LOCAL_MACHINE & h80000002
HKEY_USERS & h80000003
--------------------
If you want to obtain the subkey handle of these keys, you must call the regopenkey API function. regopenkey contains three parameters. The usage is as follows:
Private declare function regopenkey lib "advapi32.dll" alias "regopenkeya" (byval hkey as long, byval lpsubkey as string, phkresult as long) as long
Here, hkey is the key handle, lpsubkey is the sub-key string, and phkresult is the return value of the function. If regopenkey is successfully called, this parameter is returned to the sub-key hkey.
For example, we want to obtain hkey_local_ma
The statement used for the "software/Microsoft" sub-key under the chine is:
Dim RET as long, hkey as long
Ret = regopenkey (hkey_local _
Machine, "Software/Microsoft", hkey)
If ret = 0 then
'Ret = 0 indicates success, and the hkey value is equal to "Software
/Microsoft "subkey's key handle
End if
Note that after you call the Registry API function (for example, the preceding regopenkey), if it succeeds, 0 is returned. Otherwise, a non-0 value is returned, which is different from the VB Function convention.
In addition to specifying the top-level key handle value (for example, hkey_classes ),
_ Root, HKEY_LOCAL_MACHINE, etc.) can also be a subkey handle. In the preceding example, the hkey is equal to the subkey handle of "HKEY_LOCAL_MACHINE/software/Microsoft _
LOCAL_MACHINE/software/Microsoft/
For Windows/CurrentVersion subkey handle, the program is as follows:
Dim RET as long, hkey as long, hkey2 as long
Ret = regopenkey (hkey, "Windows/Current
Version ", hkey2)
'Hkey2 will be equal to "hkey_local_mac
Hine/software/Microsoft/
CurrentVersion subkey handle
In the above program, be sure not/
Add "/" before CurrentVersion "to make it"/Windows
/CurrentVersion ", which is an incorrect representation.
The following describes several other APIs (32-bit APIs ):
● Regsetvalueex (): stores data in the Value Field of the opened registry keyword;
● Regclosekey (): Release the handle of the specified keyword;
● Regqueryvalueex (): searches the registry for values related to your specified key value;
● Regcreatekeyex (): Creates and opens a specified keyword. if it already exists, it is opened;
● Regenumkeyex (): enumeration of the subkeywords of the specified registry keyword (32-bit );
● Regenumvalue (): copy the name and data block of an indexed value each time you call the enumerated value of the specified registry keyword;
● Regdeletekey (): deletes a keyword and Its subkeywords;
● Regdeletevalue (): deletes a value with a name in the specified registry keyword.
Conclusion
By calling these APIs and functions provided by VB, we can easily read, query, create, and delete any keywords in the registry. For detailed usage, see "API Browser ", we will not discuss it here.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.