. Ini file
The INI file (also called the initialization file) is used to save the applicationProgramA special ASCII file for setting and options. It uses ". ini" as the file extension and is also called a configuration file or profile ). In addition to applications that can have their own private Initialization Files, windows also provides a system initialization file Win. and then configure the current Windows system. You can also record the running options of other applications in the system.
Generally, the initialization file private to the application is relatively small, which can reduce the amount of information the program reads during initialization, thus improving the startup speed of the program. And the system initialization file Win. INI not only records a large amount of information about the system, but also stores initialization data of many other application software. Therefore, it is usually large, and the amount of accessed data is much larger than that of private configuration files. If not necessary, we do not recommend win. INI file operations, but if the information to be accessed involves the Windows system environment or other applications, you must. INI for read/write access, and send the wm_wininichange message to all top-level windows while accessing, notifying other process system initialization files that have been changed.
The information in the configuration file can be read and identified by the system and many different types of applications, this is because the internal access to data adopts a pre-agreed "entry-value pairs" Storage Structure and stores the accessed data in different categories. The following is part of the win. ini file in the system directory:
[Windows] Load = Run = Nullport = none [Desktop] Wallpaperstyle = 2 Pattern = (none) |
Here, the configuration file divides the information into several "sections" with the Section title in square brackets. For example, "[desktop]" is the desktop section, each section contains some related "items" and assigns values to them by equal signs. The general format is as follows:
In the initialization file, the value can only be of two data types: Value and string. Windows provides two sets of API functions for the two data types to read the data in the initialization file, and only supports writing strings when writing the initialization file, numeric and other data types must be converted before being written to the initialization file.
Access to private Initialization Files
Data access to private initialization files is completed by three API functions: getprivateprofileint (), getprivateprofilestring (), and writeprivateprofilestring. The functions are described as follows:
uint getprivateprofileint (lpctstr lpappname, // node name address lptstr lpkeyname, // item name address int ndefault, // The default value returned when the item name is not found lpctstr lpfilename // the address of the initialization file name ); DWORD getprivateprofilestring (lpctstr lpappname, // node name address lptstr lpkeyname, // item name address lptstr lpdefault, // default string lptstr lpreturnedstring, // buffer address for storing strings DWORD nsize, // buffer size lpctstr lpfilename // initialize the file name address ); bool writeprivateprofilestring (lpappname, // node name address lptstr lpkeyname, // item name address lptstr lpstring, // string address to be written lpctstr lpfilename // initialize the file name address ); |
Getprivateprofileint () returns the integer of the lpkeyname in the lpappname section of the initialization file lpfilename. If this item is not found, the default value ndefault is returned. If the project exists but the value is not an integer, 0 is returned. If a project value contains non-numeric characters, only the first non-numeric character is returned. For example, for "value = 21century", only the value 21 is returned. The initialization file name lpfilename can be a full path or only a file name. If no specific path is specified, the Windows system will search for the file in the system directory. The usage of getprivateprofilestring () and writeprivateprofilestring () is similar, but the data type of the processing object is different.
Private initialization files are mainly used to save information related to the current state of the application. When the program exits, the information is retained because it has been written to the initialization file. When the program runs again, you can read the data in the initialization file to learn the information about the application during the last running. The following sectionCodeThat is, the number of running times and the last running date of the program are recorded through access to the private initialization file:
// Obtain the full path of the current application Getmodulefilename (null, buffer, max_path ); Spath = cstring (buffer ); Spath = Spath. Left (Spath. reversefind ('\\')); // Obtain the full path of the initialization file Spath + = "\ sample04.ini "; // Obtain the total number of program running times Uint time = getprivateprofileint ("program", "RunTime", 0, Spath ); // Get the last running date Getprivateprofilestring ("date", "last", "2002-11-1", buffer, 1000, Spath ); // Display the file information obtained from the initialization file Smsg. Format ("This software has run % d times, last run date is % s", time, cstring (buffer )); Afxmessagebox (smsg ); // Accumulate the running times and save them to the initialization file Time ++; Stime. Format ("% d", time ); Writeprivateprofilestring ("program", "RunTime", stime, Spath ); // Obtain the current date and save it to the initialization file Ctime TM = ctime: getcurrenttime (); Sdate. Format ("% d-% d", TM. getyear (), TM. getmonth (), TM. getday ()); Writeprivateprofilestring ("date", "last", sdate, Spath ); |
After the program is executed, the content of the initialization file sample04.ini is:
[Date] Last = 2002-11-12 [Program] Runtime = 1 |
Access to win. ini
Win. INI is a special initialization file that provides initialization services for the system. It will be accessed by the system at system startup and configured according to the saved parameter values. Windows provides three API functions: getprofileint (), getprofilestring (), and writeprofilestring. INI for read/write access, the function usage is very similar to the several functions that access the private initialization file, but you do not have to specify the initialization file name. The following is the prototype Declaration of the three functions:
Uint getprofileint (lpctstr lpappname, // node name address Lptstr lpkeyname, // item name address Int ndefault // The default value returned when the item name is not found ); DWORD getprofilestring (lpctstr lpappname, // node name address Lptstr lpkeyname, // item name address Lptstr lpdefault, // default string address Lptstr lpreturnedstring, // address for storing the String cache DWORD nsize // cache size ); Bool writeprofilestring (lpctstr lpappname, // node name address Lptstr lpkeyname, // item name address Lptstr lpstring // address of the string to be written ); |
You can add the program configuration information to win. ini by slightly modifying the code that previously accessed the private initialization file. The changed code is as follows:
// Obtain the total number of program running times Uint time = getprofileint ("program", "RunTime", 0 ); // Get the last running date Getprofilestring ("date", "last", "2002-11-1", buffer, 1000 ); // Display the file information obtained from the initialization file Smsg. Format ("This software has run % d times, last run date is % s", time, cstring (buffer )); Afxmessagebox (smsg ); // Accumulate the running times and save them to the initialization file Time ++; Stime. Format ("% d", time ); Writeprofilestring ("program", "RunTime", stime ); // Obtain the current date and save it to the initialization file Ctime TM = ctime: getcurrenttime (); Sdate. Format ("% d-% d", TM. getyear (), TM. getmonth (), TM. getday ()); Writeprofilestring ("date", "last", sdate ); |
Because win. the INI file is the system initialization file. Before the program is running, the file does not include custom sections such as "date" and "program" and their items. Therefore, after the program is first executed, convert the writeprofilestring () function to win. at the end of the INI file, create the joints and items, and write data.
Access to the system registry
Initialization Files are widely used in early windows programming. Various system initialization files, such as win. INI, system. INI is even responsible for controlling system software, hardware configuration, and user environment. In the current Windows programming, although the initialization file continues to be used with its simple and convenient programming method, its usage scope is not as wide as before, functions, especially in the provision of configuration information for the system, are greatly weakened, which has almost no important effect. Instead, a binary data file called the "system registry" provides storage and retrieval of configuration information for applications and system components. The file can only be edited by using the Registry Editor provided by the system, or by using an API function specifically used for registry access in the program.
The system registry is a multi-level structure tree with six predefined keys at the root of the tree: hkey_classes_root, HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE, keyi_users, hkey_current_config, and hkey_dyn_data (the hkey_dyn_data key is only for Windows 9x ). Each predefined key also contains many Tree nodes, each of which is a registry keyword and represents a specific configuration item. After the node is expanded, it contains subkeywords until the last node. The hkey_classes_root key stores information such as the document type and attributes, as well as classification information related to the application. The HKEY_CURRENT_USER key records the user's current system configuration. The HKEY_LOCAL_MACHINE key records the computer status information. The keyi_users key organizes all user information of the current system. The hkey_current_config and hkey_dyn_data keys respectively record hardware configuration information and data information related to dynamic registration.
Windows provides nearly 30 API functions for accessing the system registry, these API functions provide functions such as key creation, opening, closing, deletion, and key value setting and deletion for the Registry. Generally, key or key value addition, deletion, and modification are common operations on the registry. Each key in the registry has its own specific role, by accessing these keys and modifying the key value appropriately, you can obtain almost all the software and hardware information related to the system and optimize the system performance. For example, the user information is located in HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ of the system registry (for Windows 2000 Professional ), the key value registeredowner and registeredorganization indicate the user's name and company name respectively. To obtain this information in a program, you only need to access the content of this key value.
To read the content of a key value in the registry, you must first open the corresponding key through the regopenkey () or regopenkeyex () function, and then call regqueryvalue () or regqueryvalueex () retrieve the content of the specified key value. After the operation is complete, call regclosefkey () to close the opened key and end access to the registry. Regopenkeyex () and regqueryvalueex () have powerful functions. They are extensions of regopenkey () and regqueryvalue () functions. The function prototype is:
Long regopenkeyex (hkey, // predefined key to be opened Lptstr lpsubkey, // address of the Child key to be opened DWORD uloptions, // Reserved Regsam samdesired, // Secure Access mask Phkey phkresult // address of the opened key ); Long regqueryvalueex (hkey, // key to be retrieved Lptstr lpvaluename, // address of the key value name to be retrieved Lpdword lpreserved, // Reserved Lpdword lptype, // key-Value Type address Lpbyte lpdata, // the address that stores the cache of the search results Lpdword lpcbdata // address of the cache Length ); |
Finally, a simple program example is provided to obtain user registration information by accessing the registry key value. The sample snippet shows the basic processes for enabling, retrieving, and disabling the key specified for the system registry:
// Open the key Hkey; Lpctstr rgspath = "SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion "; Long ret = regopenkeyex (HKEY_LOCAL_MACHINE, rgspath, 0, key_read, & hkey ); If (Ret! = Error_success) { Regclosekey (hkey ); Return; } // Read key value content DWORD dwinfosize; DWORD type = REG_SZ; Byte userinfo [255]; Ret = regqueryvalueex (hkey, "registeredowner", null, & type, userinfo, & dwinfosize ); If (Ret! = Error_success) { Regclosekey (hkey ); Return; } ...... // Close the key Regclosekey (hkey ); |
Access Permissions of the registry key
Based on the important position and special role of the Registry in the Windows operating system, it is very necessary to set some security aspects. Although you can set user permissions to restrict access to sensitive data in the Registry, the applications used by this user are likely to have to access this data. This requires you to be able to modify the security level of the registry key in the program. For example, when you use regopenkeyex () to open a key, you can use the write_owner access permission to open it, and pass the returned handle pointing to the Registry to the call to regsetkeysecurity () to obtain ownership of the key:
Regopenkeyex (hkey_classes_root, "testvalue", 0, write_owner, & hkey ); Regsetkeysecurity (hkey, owner_security_information, & Security ); |
Security is an object in the security_descriptor structure. Except the write_owner used above, the access permissions to the registry key include Delete, read_control, write_dac, and other standard access permissions. The Registry's proprietary access permissions include key_create_link, keys, key_execute, keys, key_policy, key_query_value, key_setvalue, key_all_access key_write.
Summary
Using the initialization file and registry in an application can complete many special functions. Generally, many applications are used to record and obtain the state configuration of the program in the last running, make the program have the "Memory" function. Some hacker software also enables it to run on startup by writing certain information to the initialization file and registry.