Now many software in the program to save the data needed in the registry, so that when the user installed more and more software, resulting in a larger registry, easy to make system errors. Of course, Microsoft also proposed to save the data in the registry, but when we need to save a little bit of data can be stored in the Win.ini, so that can be easily maintained, the implementation method is relatively simple. Below I take Borland C + + Builder as an example to say how to implement.
The principle is actually very simple, just call the API writeprofilestring and Getprofileint functions can be. The prototypes of these two functions are: BOOL writeprofilestring (LPCTSTR lpappname,lpctstr lpkeyname,lpctstr lpstring);
UINT getprofileint (LPCTSTR lpappname,lpctstr lpkeyname,int ndefault);
Where Lpappname refers to the name in the middle of Win.ini, that is, a string enclosed by [] lpkeyname refers to the name of each item in the paragraph, lpstring refers to the value of the item, that is, the number after "=", Ndefault is the value returned when Getprofileint does not find Lpappname and Lpkeyname, the default value, which returns a Boolean value (True or false), and the latter is returned as an unsigned reshaping value. When lpkeyname is empty (null) in the WriteProfileString function, the entire contents of the segment are cleared, and the contents of the item are cleared when lpstring is empty, that is, the line is cleared away.
Here is an example to illustrate the use of these two functions. Create a new application with two edit and three button on the Form1, where edit text is empty and three button caption are "Add", "View", "purge", respectively. Double-click the Add button to add the following code:
WriteProfileString ("Example program", "Project", Edit1→text.c_str ());
Double-click the "View" button to add the following code:
unsigned int Temp;
Temp=getprofileint ("Example program", "Project", 100);
Edit2→text=inttostr (Temp);
Double-click the "Clear" button to add the following code:
WriteProfileString ("Example program", Null,null);
Then press the F9 key to run the program.
Down to check the correctness of the program. Enter a number in Edit1, such as "3265" and press the "Add" button, and then run "sysedit" to see "WIN." INI file, you can see that you have added the following:
[Example Program]
Project =3265
where "[]" and "=" are automatically added by the function. Press the "View" button, "3265" appears in Edit2, and the "clear" button is pressed to clear the added section. After viewing the program has achieved the intended purpose.
Friends who love programming can apply the above methods to their own programs to save data information. When you are sure that you want to save the information in the registry, you can define an object of the Tregistry class in C + + Builder to do the related operation.