Registry operations in C ++ Builder

Source: Internet
Author: User

First, introduce the attributes and methods of TRegistry:

The TRegistry Class has four attributes.

Attribute Type Description
CurrentKey Int Contains the value of the current key value, but it is not very easy to understand, so it is not very common
RootKey Int The Root Key of the current key value. BCB defines enumerated constants: HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CURRENT_CONFIG, HKEY_DYN_DATA, and HKEY_USERS. Corresponding primary keys in the registry. When a TRegistry Class instance is generated, the default value of RootKye is HKEY_CURRENT_USER.
CurrentPath AnsiString The text description of the current key value. \ HKEY_CURRENT_USER \ Software \ Borland's CurrentPath = "Software \ Borland", while RootKey = HKEY_CURRENT_USER
LazyWrite Bool True: The application is immediately returned if a key is disabled. If the value is false, it is returned only after the registry is written. The default value is true.

There are many methods for the TRegistry Class. the commonly used methods are described as follows:

Method Description
CloseKey () Close a key value and write data to the key value. You should close it after the key value operation is completed, but you do not have to call this method, because the destructor of TRegistry automatically calls it.
DeleteKey (AnsiString) Delete? The parameter is the key value to be deleted. If the parameter is a null string, the current key value is deleted.
OpenKey (Ansistring, bool) Open a key value. If the key value does not exist, the second parameter determines whether it is automatically created.
LoadKey (AnsiString, AnsiString) Load the key value from the file. The first parameter is the key value, and the second parameter is the file name.
KeyExists (AnsiString) Checks whether a key value exists.
SaveKey (AnsiString, AnsiString) Save a key value to a file. For parameter descriptions, see LoadKey ()
ReadInteger (AnsiString) Read a key value as an integer. If it fails, an exception is thrown. Similar to this function, ReadBool (), ReadString (), ReadFloat (), ReadDateTome (), and ReadBinaryData () are used to read different types of key values.
ValueExists (AnsiString) Checks whether a value exists.
WriteInteger (AnsiString, int) Write a key value as an integer. If it fails, an exception is thrown. Similar to this function, WriteBool (), WriteString (), WriteFloat (), WriteDateTome (), and WriteBinaryData () are used to write different types of key values.

It is easy to use TRegistry. Generally, you can perform the following four steps:

1) create a TRegistry Class
2) open a key value using the OpenKey () method
3) read/write key values with ReadType () and WriteType ()
4) Call CloseKey) to close a key value.

It is worth noting that when using the TRegistry Class, you must add the header file: # include $ # @ 60; Rgistry. hpp $ # @ 62.

Let's take an example to illustrate how to use the TRegistry Class in my "WinNT Automatic Logon.

WinNT users all know that when starting WinNT, press Ctrl + Alt + Del to log on, even if there is no password. although WinNT does not often crash as WIN9X does, it is a bit annoying to start the system every time. can I enable WinNT To Enable Automatic Logon at each startup? In fact, you only need to set the value of AutoAdminLogon, a property of the Registry, to "1". Unfortunately, this operation can only be managed once, after each startup, WinNT automatically changes its value to "0" (damn Bill !). If you need to modify the registry every time, it is better to log on manually every time! After a program is started, it is OK to automatically modify this value.

Create a new project and put four text boxes in the form, named dUser, edPasswd, edRePasswd, and edDomain respectively. The PasswordChar attribute of edPasswd and edRePasswd is changed to "*" (password input ). Add two check boxes to select whether to automatically log on (chkAuto) and automatically run (chkAutorun), one timer (Timer1), two buttons (bbtOK, bbtCancel) and some labels.

Open the code window and enter the following code in the header:

# Include TRegistry®Key = * new TRegistry (); // defines the global variable: Registry class. Int delay = 0; // defines the global variable and the delay time.

Select the OnCreat event of the form and enter the following code:

Regkey. RootKey = HKEY_LOCAL_MACHINE; // set the root key // open the key where automatic logon is located
Regkey. OpenKey ("SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ Winlogon", true );
// In the following three statements, enable the automatically running Windows key value. TRegistry & AutoRun = * new TRegistry (); AutoRun. RootKey = HKEY_LOCAL_MACHINE;
AutoRun. OpenKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true );
If (strlen (CmdLine)> Application-> ExeName. Length () + 3) // if a parameter is required, uninstall
{

AutoRun. DeleteValue ("Windows NT Auto Logon"); // Delete attributes that you have added
Regkey. DeleteValue ("Windows NT Auto Logon"); delete & AutoRun;
Application-> Terminate (); // Terminate Application return;} // write the program to run automatically.
AutoRun. WriteString ("Windows NT Auto Logon", Application-> ExeName );
Delete & AutoRun;
EdDomain-> Text = regkey. ReadString ("DefaultDomainName"); // read the Domain Name
EdUser-> Text = regkey. ReadString ("DefaultUserName"); // read the user name
ChkAuto-> Checked = true;
ChkAutorun-> Checked = regkey. ReadString ("Windows NT Auto Logon") = "1 "? True: false; // whether the program runs automatically
EdPasswd-> Text = regkey. ReadString ("DefaultPasswd"); // read the password
SetControl (! ChkAutorun-> Checked); // if it is an automatic running status, block others, and vice versa.
If (chkAutorun-> Checked)
{

Timer1-> Enabled = true; // start time of Automatic Running edRePasswd-> Text = edPasswd-> Text;

}

}

The setControl () function shields several components and is defined as follows:

First, add void setControl (bool) to the header file form class definition of the form );

The function body is as follows (the form name is fmMain ):

Void TfmMain: setControl (bool t)
{

EdUser-> Enabled = t;
EdRePasswd-> Enabled = t;
EdPasswd-> Enabled = t;
EdDomain-> Enabled = t;
ChkAuto-> Enabled = t;

}

In the chkAutoRun OnClick event, you can choose whether to enable the timer based on whether automatic running is selected.

SetControl (! ChkAutorun-> Checked); // if it is an automatic running status, block others, and vice versa if (! ChkAutorun-> Checked)
{Timer1-> Enabled = false; // if it is not automatically run, the timer fails to run delay = 0;

}
Else

Timer1-> Enabled = true; // otherwise, the timer is valid.

In The OnClick event of the new bbtOK, enter the following code to write the code to the Registry.

If (edRePasswd-> Text = edPasswd-> Text) // if the password is correct
{

Regkey. WriteString ("DefaultDomainName", edDomain-> Text); // write the Domain Name
Regkey. WriteString ("DefaultUserName", edUser-> Text); // write the user name
Regkey. WriteString ("AutoAdminLogon", chkAuto-> Checked? "1": "0"); // whether to log on automatically after writing
Regkey. WriteString ("Windows NT Auto Logon", chkAutorun-> Checked? "1": "0"); // whether the write is automatically run
Regkey. WriteString ("DefaultPasswd", edPasswd-> Text); // WRITE password
If (! ChkAutorun-> Checked) // if not run automatically
{

TRegistry & AutoRun = * new TRegistry ();
AutoRun. RootKey = HKEY_LOCAL_MACHINE; // locate Automatic startup
AutoRun. OpenKey ("SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Run", true );
AutoRun. DeleteValue ("Windows NT Auto Logon"); // Delete attributes that you have added
Delete & AutoRun;

}
Application-> Terminate (); // program termination.

}
Else

{ShowMessage ("Incorrect password! "); EdPasswd-> Text = "";

}

When the program runs automatically, write the following code to the registry after a delay of 3 seconds in the Timer1 OnTimer event:

Delay ++; if (delay> = 3) // delay three seconds to bbtOK-> Click (); // Click the "OK" button to write to the Registry

Then add the code to Terminate the program in bbtCancel: Application-> Terminate (); is our program completed? Is it really finished? NO! At the beginning, we used new to generate a regkey. We should use delete to delete it. Therefore, it is OK to add delete & regkey TO THE OnClose event of the form.

If you choose to run automatically, the program will be able to delay 3 seconds at each start, and then exit after modifying the corresponding items in the Registry to release system resources.


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.