Use C ++ Builder to add startup logs for computers
Source: Internet
Author: User
Use C ++ Builder to add startup logs for computers-Linux general technology-Linux programming and kernel information. The following is a detailed description. Computers in public data centers or offices are usually shared by everyone. As managers or "Main" users, they really want to know when the machines have been turned on and used, if you have a startup record, you can keep abreast of these situations. This article describes how to use C ++ Builder 5 to compile such a program, so that each time a computer is started, the program is executed, and information such as the boot time is recorded in the registry, you can view the data in the Registry Editor at any time. Program features: each time the computer is started, it must be executed once (this function is automatically completed by the program without having to manually add it to the Registry, you only need to run the program once), to increase concealment, this program is a non-Form program running in the background.
Programming highlights:
1. Create a New project File in the C ++ Builder 5 Environment: Execute the menu command File/New Application and save the project File as "Logon. bpr ".
2. Run the Project/View Source command to open the Logon. cpp file and
The two statements are deleted to create a program without a form.
3. All the code for the Logon. cpp file is as follows:
# Include
# Include // Add rows
# Pragma hdrstop
USERES ("logon. res ");
WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
{
Try
{// Add part to start
Int num = 0;
TRegistry * Registry = new TRegistry; // create a Registry heap object pointer
Registry-> RootKey = HKEY_USERS; // locate the Registry Root Key
If (! Registry-> OpenKey (". DEFAULT \ Logon \ Records", false ))
{// Failed to open the primary key
Registry-> CreateKey (". DEFAULT \ Logon \ Records"); // create a primary key
If (! (Registry-> OpenKey (". DEFAULT \ Logon \ Records", false )))
Return 0;
Else
{
TRegistry * Reg = new TRegistry; // ①
Reg-> RootKey = HKEY_LOCAL_MACHINE;
Reg-> OpenKey ("Software \ Microsoft \ Windows \ CurrentVersion \ Run", false );
Reg-> WriteString ("Logon", Application-> ExeName); // register this program in the Registry
Reg-> CloseKey ();
Delete Reg;
Application-> MessageBox ("the login program has been registered successfully! "," Result ", MB_ OK); // ②
Registry-> WriteInteger ("Boot Times", num );
Registry-> WriteString ("program registration Time", Date () + Time ());
Return 0;
}
}
Else
{
Num = Registry-> ReadInteger ("Boot times"); // ③
Registry-> WriteInteger ("Boot Times", num + 1); // record the boot times
Registry-> WriteString ("" + AnsiString (num + 1) + "", Date () + Time (); // record the boot Time
}
Registry-> CloseKey ();
Delete Registry;
// Add part to end
Application-> Initialize ();
Application-> Run ();
}
Catch (Exception & exception)
{
Application-> ShowException (& exception );
}
Return 0;
}
Application:
This method is used to transplant the above Code to other C ++ Builder applications, and restrict the use of the program by reading and writing the Registry. For example, the maximum number of times of using the program is 30, add an if statement at code ③.
If (num = 30)
{
Application-> MessageBox ("this program has been used for 30 times and will quit !!", "Warning:", MB_ OK );
Return 0;
}
You can (delete the statement segments from line 1 to line 2) and write the restricted edition application. Of course, you can also consider the time limit, as long as the "organ" is set in the registry.
To generate a separate EXE file, you should activate Project/Options... Menu item, remove the "Use dynamic RTL" option under the "Linker" label, and remove the "Build with runtime Packages" option under the "packages" label. Copy the logon.exe file to a fixed folder (for example, a windows Folder) on the machine on which you want to record the file. Execute the file once to complete program registration.
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.