Use BCB to operate INI and Registry

Source: Internet
Author: User

 In Windows
In section 95/98, the system registry has six primary keys: HKEY_LOCAL_MACHINE, hkey_classes_root,
Hkey_current_config, HKEY_USERS, HKEY_CURRENT_USER, and hkey_dyn_data. Win2000
The primary key hkey_dyn_data is removed from the registry. There are only five primary keys. The following describes the functions of each primary key:

HKEY_LOCAL_MACHINE stores hardware and software configurations of the local system. It stores most information about the Registry and is the most important primary key in the registry. These settings are for all users using Windows, and are public information, so they are irrelevant to specific users.

Hkey_classes_root records all com servers and applications in the Windows operating system.ProgramAssociated file extensions, file icons, and other information. When the user double
When you click a document, the system can start the corresponding application through the information. Information stored in the hkey_classes_root primary key and
The information stored in the HKEY_LOCAL_MACHINE/software/classes branch is consistent.

Hkey_current_config primary key. If two or more sets of hardware configuration files are set in windows, the user will select which configuration to use when the system starts. The hkey_current_config primary key stores all the information in the current configuration file.

The Default User (. Default) stored in the HKEY_USERS primary key and the information of the currently logged on user and software.

The information saved in the HKEY_CURRENT_USER primary key (the subkey information of the current user) is the same as that saved in the HKEY_USERS/. (Note that there is a point here. Any operation on them will result in modification to the other party.

The hkey_dyn_data primary key contains the dynamic data in the system running, that is, information about an event that has occurred. Therefore, the keyword contains not only the current status of the system hardware, but also the data to be updated and retrieved. These data changes dynamically, and they are always up-to-date.

It is quite easy to access the registry or INI file in C ++ builder. Because VCL provides several predefined classes to help programmers maintain and use the INI file and registry. The reason for putting them together is that their processing in C ++ builder is similar.

The VCL classes are as follows:
Tregistry processes the registry.
Tinifile/tmeminifile processes INI files.
Tregistryinifile can process both the registry and INI files. In addition to reading and writing the system registry, its attributes and methods are similar to Tinifile. Use
Member variables or functions of the tcustominifile class (the basic class of Tinifile, tmeminifile, and tregistryinifile) can be written.
Common access to registry or INI filesCode.

Our testing program mainly implements two functions. One is to store the form coordinates in the INI file when the main form is closed. When this program is opened at the moment, read from this INI File
This coordinate is used to display the form at the position where the form was last closed. Another function is to hide the form when you click the button, if there is a "document" in the current Windows Start Menu, otherwise
It. This change will be displayed after Windows is restarted.

to use Tinifile and Tregistry classes normally in the program, the header file inifiles. HPP and registry. HPP must be included.
first, you can save the form position when the form is closed. Add the following code to the onclose event processing function of the main form:
// record the position of the window when the window is closed
Tinifile * pinifile;
ansistring filename;
filename = changefileext (Application-> exename ,". ini ");
pinifile = new Tinifile (filename);
pinifile-> writeinteger (Caption," TOP ", top );
pinifile-> writeinteger (Caption, "Left", left);
Delete pinifile;
corresponding location, add the following code to the onshow event processing function of the form:
// when the window is displayed, read the top and left values when the last close
Tinifile * pin Ifile;
ansistring filename;
filename = changefileext (Application-> exename ,". ini ");
pinifile = new Tinifile (filename);
Top = pinifile-> readinteger (Caption," TOP ", 200 );
left = pinifile-> readinteger (Caption, "Left", 200);
Delete pinifile;
as shown in the preceding code, the steps for using the Tinifile class are as follows:
(1) create a Tinifile object
(2) use writetype () (type can be integer or bool) function to store data or read data using readtype.
(3) Delete A Tinifile object.

In this example, the changefileext () function is used to generate an INI file with the same name as the executable file. Of course, such a file name is not required. When initializing a Tinifile object
INI file name is passed into the constructor Tinifile () as a parameter. If the file does not exist, a new one is automatically created. In this example, readinteger () is used to read data.
Different types can also be readbool (), readdate (), readdatetime (), readfloat (), or readtime ().
Similar writing functions are also the same. Three parameters are required for each function: the first parameter represents the section of the INI file, the second parameter represents the value to be read and written, and the third parameter represents a default value.
The default value is used or created in the INI file.

Next we will use the Tregistry Class to operate the Registry to implement another function.

As background knowledge, we need to know the path HKEY_CURRENT_USER/software/Microsoft/windows in the registry.
You can add a new value norecentdocsmenu (DWORD) to/CurrentVersion/policies/explorer and set it to 1.
Indicates hiding the document items in the "Start" menu. Otherwise, 0 indicates displaying this menu item.

The steps for using the Tregistry Class are as follows:
(1) create a new object of the Tregistry Class;
(2) set the primary key
(3) Use openkey () to open the target key value
(4) use readtype () or writetype () (the type here needs to be changed according to different situations) to read and write key values.
(5) Use closekey () to disable this key value

The test code is as follows:

Tregistry * Reg = new Tregistry;
try
{
reg-> rootkey = HKEY_CURRENT_USER;
reg-> openkey ("software // Microsoft // windows // CurrentVersion // policies // Explorer", true );
// if the key value norecentdocsmenu does not exist
If (! Reg-> valueexists ("norecentdocsmenu")
{< br> reg-> writeinteger ("norecentdocsmenu", 1);
return;
}< br> // if the key value is 0
If (reg-> readinteger ("norecentdocsmenu ")! = 1)
{< br> reg-> writeinteger ("norecentdocsmenu", 1 );
}< br> else
{< br> reg-> writeinteger ("norecentdocsmenu", 0 );
}< BR >__ finally
{< br> Delete reg;
}

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.