Windows API registry functions

Source: Internet
Author: User
Windows API registry functions

Application programing interface is a 32-bit application provided by windows.ProgramProgramming interfaces, including a large number of functions, provide a wealth of functions. When compiling an application, we can call the registry function to operate the Registry to implement the functions we need. There are more than 20 functions that can be used in the Registry in Windows APIs. The functions can be divided into the following categories:

Key management class regclosekey () regcreatekey () regcreatekeyex () regdeletekey ()
Regdeletekeyex () regopenkey () regopenkeyex ()
Value Management class regdeletevalue () regqueryvalue () regqueryvalueex () regsetvalue ()
Regsetvalueex ()
Query counting class regqueryinfokey () regenumkey () regenumkeyex () regenumvalue ()
Backup/recovery regloadkey () regreplacekey () regrestorekey () regsavekey ()
Region class regconnectregistry () regnotifychangekeyvalue () regunloadkey ()
Security
(Applicable only to NT) reggetkeysecurity () regsetkeysecurity ()

API registry function details-> key management class:

1. regclosekey (): Close the registry key to release the handle.
Regclosekey (byval hkey as long)
Parameter: hkey -- Root Key (or sub-key) Handle
Return Value: = 0 success = 0 failure

2. regcreatekey (): Open the specified registry key. If the key does not exist, try to create it.
Regcreatekey (byval hkey as long, byval lpsubkey as string, phkresult as long)
Parameter: hkey -- root key handle lpsubkey -- subkey name or path
Phkresult -- if the execution is successful, this parameter returns the sub-key handle.
Return Value: = 0 success = 0 failure

3. regcreatekeyex (): Same as regcreatekey ().

Regcreatekeyex (byval hkey as long, byval lpsubkey as string, byval reserved as long, byval lpclass as string, byval dwoptions as long, byval samdesired as long, lpsecurityattributes as large, phkresult as long, lpdwdisposition as long)

4. regdeletekey (): deletes the registry key, but cannot delete the root key.
Regdeletekey (byval hkey as long, byval lpsubkey as string)
Parameter: hkey -- Key handle lpsubkey -- subkey name or path. If it is "", the hkey is deleted.
Return Value: = 0 success = 0 failure

5. regdeletekeyex (): Same as regdeletekeyex ()

6. regopenkey (): Obtain the sub-key handle of the Registry Root Key.
Regopenkey (byval hkey as long, byval lpsubkey as string, phkresult as long)
Parameter: hkey -- root key handle lpsubkey -- subkey name or path
Phkresult -- if the execution is successful, this parameter returns the sub-key handle.
Return Value: = 0 success = 0 failure

7. regopenkeyex (): Same as regopenkey ()

Regopenkeyex (byval hkey as long, byval lpsubkey as string, byval uloptions as long, byval samdesired as long, phkresult as long)

Registry root key handle

The use of Microsoft Windows operating systems further standardizes each application to manage its own data and configuration information, further standardizes the methods for communicating with system devices and supporting multi-user environments.
We know that before Windows 95, applications saved their settings in the *. ini file. Currently, almost all applications in Windows write their settings, user information, registration information, and program data to the Registry during installation and initialization, it is modified when the application is running.
When we compile an application, how do we write the setup information and related data of the application into the Registry during installation? This requires us to understand how to use a program to operate the registry and write or delete relevant information.
The following describes in detail how to operate the Registry through a program.
First, let's take a look at some basic knowledge of registry programming:

Each root key handle of the Registry is fixed as follows:

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_performance_data = & h80000004
Public const hkey_current_config = & h80000005
Public const hkey_dyn_data = & h80000006

Registry data type
The following table lists the types of registry data used in the program:

REG_BINARY binary data
REG_DWORD 32-bit dual-character
reg_dword_little_endian large counting format 32-bit number, the valid byte of a word is a high-end word
reg_dword_big_endian large notation format 32-bit number. The valid byte of a word is a low-end word
reg_expand_sz null termination string, including references to environment variables such as "& temp &"
reg_link single Code symbol chain
reg_multi_sz definition includes null termination string Array, array ended by two actual null strings
reg_none undefined Value Type
reg_resouse_list Device Driver list
REG_SZ Null String, is the most common format for saving strings

Public const reg_none = 0'
Public const REG_SZ = 1' string
Public const reg_expand_sz = 2' extensible string
Public const REG_BINARY = 3' binary data
Public const REG_DWORD = 4' long integer
Public const reg_dword_big_endian = 5' big _ endian long integer
Public const reg_multi_sz = 7' multiple strings

Use Visual Basic to operate the Registry
In Microsoft Visual Basic, there are 4 registry operation functions, including savesetting, getsetting, getallsettings, and deletesettings.

Savesetting saves or creates an application project in the Windows Registry
Syntax savesetting appname, section, key, setting
Parameter appname: string expression, the name of an application or project.
Section: A string expression that contains the region name. The registry key settings are saved in the region.
Key: A string expression that contains the name of the registry key to be saved.
Setting: string expression that contains the key setting value.
The function example uses the savesetting statement to create a project of the "MyApp" application, and then deletes the project using the deletesetting statement.
1. Add some setting values in the registration area.
Savesetting appname: = "MyApp", Section: = "Startup", key: = "TOP", setting: = 75
Savesetting "MyApp", "Startup", "Left", 50
2. Delete the CIDR Block and all set values.
Deletesetting "MyApp", "Startup"
Getsetting returns the registry key setting value from the application project in the Windows Registry
Syntax getsetting appname, section, key [, default]
Appname: A string expression that contains the name of an application or project.
Section: A string expression that contains the region name. registry key settings are required for this region.
Key: string expression. The name set in the registry is returned.
Default: Optional. 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 ("").
If getsetting does not have a parameter, getsetting returns the default value.
The function example uses the savesetting statement to create a project for the appname application, and then uses the getsetting function to obtain and display one of the settings. The getsetting function must return a value because the default parameter is input. Note that the section name cannot be obtained using the getsetting function. Finally, delete the application item using the deletesetting statement.
1. Define variables for saving the two-dimensional array data returned by the getsetting Function
Dim mysettings as Variant
2. Add a project to the Registry
Savesetting "MyApp", "Startup", "TOP", 75
Savesetting "MyApp", "Startup", "Left", 50
Debug. Print getsetting (appname: = "MyApp", Section: = "Startup", key: = "Left", default: = "25 ")
3. delete a project in the Registry
Deletesetting "MyApp", "Startup"
Getallsettings returns the settings of all registry items and their corresponding values from the Windows registry.
Syntax getallsettings (appname, Section)
The appname parameter is required. String expression, the name of the application or project.
Section is required. String expression that contains the region name and requires registry key settings for the region. Getallsettings returns variant, which is a two-dimensional array of strings. The two-dimensional array contains all registry key settings and their corresponding values in the specified area.
If the appname or section does not exist, getallsettings returns the uninitialized Varian
Function example in this example, the savesetting statement is used to create the project of the appname application in the Windows registration area, and then the getallsettings function is used to obtain and display the setting value. Note that the Application name and section name cannot be obtained using the getallsettings function. Finally, delete the application item using the deletesetting statement.
'Variable used to save the two-dimensional array data returned by the getallsettings Function
'Integer is used for counting.
Dim mysettings as variant, intsettings as integer
'Add the Setting Value in the registration area.
Savesetting appname: = "MyApp", Section: = "Startup", key: = "TOP", setting: = 75
Savesetting "MyApp", "Startup", "Left", 50
'Get the set value of the input item.
Mysettings = getallsettings (appname: = "MyApp", Section: = "Startup ")
For intsettings = lbound (mysettings, 1) to ubound (mysettings, 1)
Debug. Print mysettings (intsettings, 0), mysettings (intsettings, 1)
Next intsettings
Deletesetting "MyApp", "Startup"
Deletesetting deletes the region or registry key settings from the application project
Syntax: deletesetting appname, Section [, key]
The appname parameter is required. String expression, the name of the application or project.
Section is required. String expression that contains the name of the region where the registry key is to be deleted. If only appname and section are available, the specified region and all related registry key settings are deleted.
Key is optional. String expression that contains the registry key settings to be deleted.
If all parameters are provided, delete the specified registry key settings. If you try to use the deletesetting statement set on a nonexistent region or registry key, a runtime error occurs.
Function example the following example uses the savesetting statement to create a project of the MyApp application in the Windows registration area (or the 16-bit Windows platform INI file), and then delete it using the deletesetting statement. Because the key parameter is not specified, the entire segment will be deleted, including the segment name and all its machine codes (KEYS ).
'Add some setting values in the registration area.
Savesetting appname: = "MyApp", Section: = "Startup", key: = "TOP", setting: = 75
Savesetting "MyApp", "Startup", "Left", 50
'Delete the CIDR Block and all set values.
Deletesetting "MyApp", "Startup"

In Visual Basic, in addition to using these four internal statements or functions, you can also call Windows API functions to operate the registry.

Access to the database registry value -- read the value of a specified key name

in this Article , we will use the regqueryvalueex function to read the value of a specified name of a key)
regqueryvalueex VB Function Description and parameter explanation:
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
hkey: Key handle
lpvaluename: Value Name
lpreserved: Reserved Parameter Set the value to 0.
lptype: returned data type
lpdata: returned data
lpcbdata: the length of the incoming lpdata data, if the data is read successfully, the length of the read data is returned.
return value: = 0, indicating success; = 0, indicating failure.
Note:
1. In addition to reading the value of the specified name, this function can also read the default value. To read the default value, set
lpvaluename to "" [Null String.
2. Possible values of lptype, which we mentioned in the second article.
Enum valuetype
reg_none = 0
REG_SZ = 1 --> string
reg_expand_sz = 2 --> extensible string
REG_BINARY = 3 --> binary data
REG_DWORD = 4 --> long integer
reg_dword_big_endian = 5 --> big_endian long integer
reg_multi_sz = 7 --> multiple strings
end Enum

The regqueryvalueex function is used to obtain the data type and length of a value. You only need to set the parameter lpdata to vbnullstring [indicating that no data is read], and then the data type is obtained by the parameter lptype, the length of the data obtained by using lpcbdata. The call example is as follows:
Dim hkey as long, RET as long, lendata as long, typedata as long
Dim name as string
'Read the value of internat.exe in HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ run.
Name = "internat.exe"
Ret = regopenkey (HKEY_LOCAL_MACHINE, "SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", hkey)
If ret = 0 then
Ret = regqueryvalueex (hkey, name, 0, typedata, byval vbnullstring, lendata) 'Do not forget byval
End if
After obtaining the data type and length of a value, we will perform different processing based on different data types. It is described below.

A --> the data type is REG_SZ.
This method is the simplest. You only need to use the following statement after the preceding statement to obtain the correct string. Eg:
Ret = regqueryvalueex (hkey, name, 0, typedata, byval vbnullstring, lendata)
S = string (lendata, CHR (0 ))
Regqueryvalueex hkey, name, 0, typedata, byval S, lendata' do not forget byval
S = left (S, instr (S, CHR (0)-1)

B --> the data type is reg_expand_sz.
The so-called reg_expand_sz string is a string that may contain % WINDIR % or another character string [% WINDIR % indicates the directory where windows is located. We can use the set command in the MS-DOS mode to check this type of string., we may also see strings of special significance similar to WINDIR, such as TMP, path, and winbootdir.], When encountering such a string, we must also call another API function expandenvironmentstrings to expand it [for example, % Windir % is expanded to C: \ WINDOWS]. The following is the description and usage of expandenvironmentstrings:
Declare function expandenvironmentstrings lib "Kernel32" alias "expandenvironmentstringsa" (byval lpsrc as string, byval lpdst as string, byval nsize as long) as long
Parameter type and description
Lpsrc string, the string to be expanded
Lpdst string, extended string
The length of nsize long and lpdst.
Note that the lpdst is initialized in advance to make it consistent with the length.
Function call example:
Dim S2 as string
'Use the regqueryvalueex function to obtain the data type and length of a specific value.
Ret = regqueryvalueex (hkey, name, 0, typedata, byval vbnullstring, lendata)
S = string (lendata, CHR (0 ))
Regqueryvalueex hkey, name, 0, typedata, byval S, lendata
S = left (S, instr (S, CHR (0)-1)'s is the read string
S2 = string (LEN (s) + 256, CHR (0) 's2 is an extended string
Expandenvironmentstrings S, S2, Len (S2)
S2 = left (S2, instr (S2, CHR (0)-1)

C --> the data type is reg_multi_sz.
Reg_multi_sz is a string with the following structure:

String 1 CHR (0) string 2 CHR (0)... string n CHR (0) CHR (0)

The function of the following custom subroutine is to obtain each string in multiple strings.
Sub multistringtostringarray (S as string, S2 () as string)
'S is the multi-string that we read
'S2 is the converted String Array
Dim count as integer, POS as integer, pos2 as integer, idx as integer
Pos = instr (S, CHR (0 ))

While POS> 0 count = count + 1
Pos = instr (Pos + 1, S, CHR (0 ))
Wend
'Obtain the number of strings in multiple strings
Count = count-1

Redim S2 (0 to count-1)
Pos = 1
For idx = 0 to count-1
Pos2 = instr (Pos, S, CHR (0 ))
S2 (idx) = mid (S, POs, pos2-Pos)
Pos = pos2 + 1
Next
End sub
Before calling multistringtostringarray, you must define a string array without any elements. An example of a subroutine call is as follows:
S = "WGL" + CHR (0) + "love" + CHR (0) + "Meishan" + CHR (0) + CHR (0)
Dim S2 () as string
Multistringtostringarray S, S2
After the execution, S2 (0) = "WGL", S2 (1) = "love", S2 (2) = "Meishan"

D --> the data type is REG_DWORD and reg_dword_big_endian.
'Use the regqueryvalueex function to obtain the data type and length of a specific value.
Dim L as long
Ret = regqueryvalueex (hkey, name, 0, typedata, byval vbnullstring, lendata)
Regqueryvalueex hkey, name, 0, typedata, L, lendata

E --> the data type is REG_BINARY.
'The regqueryvalueex function is used to obtain the data type and length of a value.
ret = regqueryvalueex (hkey, name, 0, typedata, byval vbnullstring, lendata)
redim Barr (0 to lendata-1) as byte
regqueryvalueex hkey, name, 0, typedata, Barr (0 ), lendata

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.