Control the Windows registry with CB

Source: Internet
Author: User
Tags relative sybase

The Windows registry contains system configuration, machine hardware configuration, Win32 applications, and other configuration information for users. The registry is divided into six root keys (ROOTKEY) based on system information, each of which has a tree structure consisting of a child key and a key value, each of which represents a specific configuration item.

The key to controlling the registry with C++builer is to understand the Tregistry class. In the C++builder VCL class Library, the Tregistry class is provided, and we can control the registry by generating an instance of the class in the application. In the Tregistry class, the focus should be on the following issues:

1. Generation of Tregistry Classes

We can not use the Direct declaration method to generate Tregistry instance, which is different from VC + + HKEY directly generate the instance. You must generate an instance of the Tregistry class with the new keyword, and then pass the pointer to a well-known variable. The specific actions are as follows:

Tregistry * Curreg=new Tregistry;//curreg is the tregistry type of pointer name

When declared in this manner, the Curreg Rootkey property points to the HKEY_CURRENT_USER root key, which is the default action for HKEY_CURRENT_USER.

2. How to turn primary keys on and off

The Tregistry class provides two member functions to open a primary key: Openkey () and the Openkeyreadonly () function. Where the Openkey function also provides the ability to create a primary key.

Openkey () has two parameters, KeyName and cancreate. Cancreate is a bool parameter that, when it is set to true, creates the primary key if the Ansistring class's variable keyname is not present under the current root key; otherwise, the primary key indicated by the KeyName is entered. If Cancreate is set to false and the current root key does not have a primary key that KeyName indicates, the function returns FALSE.

The function of the openkeyreadonly () function is to open a primary key as read-only and, if the parameter value is NULL, to the key indicated by Rootkey.

There are two ways to represent KeyName parameters: absolute path representation and relative path representation. When an absolute path is represented, the string value must start with "\", and a relative path indicates that the primary key specified by KeyName is relative to the current primary key.

After the registry is used, the Closekey () member function should be called in time to close the registry and call the Delete method to release the memory space requested by new.

3. About the acquisition of the key values of the current primary key

We can use the Getkeynames () member function to get the names of all the subkeys under the current primary key, with Getkeyinfo for more detailed information.

It must be noted that although the description of Getkeynames () is void __fastcall getkeynames (classes::tstrings * Strings), that is, its parameter type is tstring, However, we cannot first declare an instance of a tstring class and then use it as a parameter for Getkeynames (). This is mainly because the Tstrings class contains abstract components. Our solution is to use the derived class tstringlist of the Tstrings class instead of tstrings to declare an instance and use it as a parameter for the Getkeynames () function.

After getting the name of the subkey, we can use the function to further determine the details. For example, we can use GetValueNames () with read () and Write () to get the details of the value of the primary key. For example: We want to read the "Location" string value in "\software\sybase", and you can do the following:

Curreg->openkey ("\\Software\\Sybase", true);

Ansistring valueinfo=curreg->readstring ("Location");

At this point, Valueinfo obtained the location value.

4. A description of the property

One of the main properties of the Tregistry class is lazywrite. The function of this property is to determine whether the changes are reflected in the actual registry immediately after the write operation. The value of this property is initialized to true at the time the registry object is constructed, that is, the changes are not immediately reflected in the actual registry, but are overridden after the Closekey () function is executed, which can improve system performance. However, if we need to immediately reflect the modifications to the registry (which is necessary on many occasions), you should first set the Lazywrite property to False, and then perform the modification operation.

Another property worth mentioning is Rootkey, which sets the root key for the object of the current operation, which defaults to HKEY_CURRENT_USER. If you need to manipulate other root keys, assign the value of the target root key directly to the Rootkey property.

Here's a real snippet from our program that shows all the subkey names under the "\software\myinfo" PRIMARY key in ComboBox1:

  #include
   …………
   TRegistry *curReg=new TRegistry;
   curReg->OpenKey(“Software\\MyInfo",true);
   KeyNames=new TStringList();//注意TstirngList类的声明方法!
   curReg->GetKeyNames(KeyNames); for(int i=0;iCount;i++) ComboBox1->Items->Add(KeyNames->
   Strings[i]);
   curReg->CloseKey();
   delete KeyNames;
   …………

In view of this, the problem of controlling the registry in C++builder environment is not complicated, we can do this work easily by using C + + powerful class library.

Related Article

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.