Use Windows API to create and edit a. ini file

Source: Internet
Author: User

1. There are two types of Apis related to the. ini file:

1) Operating System Configuration File win. ini Function

GetProfileSectionGetProfileStringGetProfileIntWriteProfileSectionWriteProfileString

2) operate the User-Defined configuration file privateprofile. ini Function

GetPrivateProfileSectionNamesGetPrivateProfileSectionGetPrivateProfileStringGetPrivateProfileIntGetPrivateProfileStructWritePrivateProfileSectionWritePrivateProfileStringWritePrivateProfileStruct

2. Sample Code for operating APIs related to user-defined configuration files

# Include <windows. h> # include <tchar. h> # include <strsafe. h> # pragma comment (Lib, "strsafe. lib ") # define ini_file_name _ T (" ini_test.ini ") tchar ginifilefullpath [max_path] = {_ T ('\ 0')}; int main () {DWORD dwret = 0; bool Bret = false; // obtain the full path of the INI file dwret =: getmodulefilename (null, ginifilefullpath, max_path); If (0ul = dwret) {_ ftprintf (stderr, _ T ("error: error occurs in calling getmodulefilename, ") _ T (" error code is % lu. \ n "),: getlasterror (); Return-1;} If (max_path = dwret & error_insufficient_buffer =: getlasterror () {_ ftprintf (stderr, _ T ("error: the buffer is too small to hold the module name. \ n "); Return-1;} _ tprintf (_ T (" the full path for the current module is: \ n \ t % s \ n "), ginifilefullpath); DWORD dwloopidx = dwret-1; while (ginifilefullpath [dwloopidx]! = _ T ('\') {-- dwloopidx; }:: stringcchcopy (ginifilefullpath + (dwloopidx + 1), max_path-(dwloopidx + 1), ini_file_name ); _ tprintf (_ T ("the full path for % s is: \ n \ t % s \ n"), ini_file_name, ginifilefullpath); // specify writeprivateprofilesection // note: if ginifilefullpath indicates. if the INI file does not exist, A. ini file is created first. tchar szinsertedkeyvaluepair [1024] = {_ T ('\ 0')}; # If defined (_ Unicode) | defined (UNICODE) wchar_t * DEST = NULL; wchar_t * src = NULL; size_t COUNT = 0; DEST = Hangzhou; src = l "key11 = defaultvalue11"; Count = wcslen (L "key11 = defavaluvalue11"); wmemcpy (DEST, SRC, count); DEST + = count; src = l "\ 0"; Count = 1; wmemcpy (DEST, SRC, count); DEST + = count; src = l "key12 = defaultvalue12"; Count = wcslen (L "key12 = defaultvalue12"); wmemcpy (DEST, SRC, count); DEST + = count; src = l "\ 0"; Count = 1; wmemcpy (DEST, SRC, count); DEST + = count; src = l "key13 = defaultvalue13 "; count = wcslen (L "key13 = defaultvalue13"); wmemcpy (DEST, SRC, count); DEST + = count; src = l "\ 0"; Count = 1; wmemcpy (DEST, SRC, count); # else char * DEST = NULL; char * src = NULL; size_t COUNT = 0; DEST = szinsertedkeyvaluepair; src = "key11 = defaultvalue11 "; count = strlen ("key11 = defaultvalue11"); memcpy (static_cast <void *> (DEST), static_cast <void *> (SRC), count); DEST + = count; src = "\ 0"; Count = 1; memcpy (static_cast <void *> (DEST), static_cast <void *> (SRC), count); DEST + = count; src = "key12 = defaultvalue12"; Count = strlen ("key12 = defavaluvalue12"); memcpy (static_cast <void *> (DEST), static_cast <void *> (SRC ), count); DEST + = count; src = "\ 0"; Count = 1; memcpy (static_cast <void *> (DEST), static_cast <void *> (SRC ), count); DEST + = count; src = "key13 = defaultvalue13"; Count = strlen ("key13 = defavaluvalue13"); memcpy (static_cast <void *> (DEST ), static_cast <void *> (SRC), count); DEST + = count; src = "\ 0"; Count = 1; memcpy (static_cast <void *> (DEST ), static_cast <void *> (SRC), count); # endif // defined (_ Unicode) | defined (UNICODE) Bret = :: writeprivateprofilesection (_ T ("Section1"), partial, ginifilefullpath); If (false = BRET) {_ ftprintf (stderr, _ T ("error: error occurs in calling writeprivateprofilesection, ") _ T (" error code is % lu. \ n "),: getlasterror (); Return-1 ;} // required writeprivateprofilesection // required writeprivateprofilestring tchar szstr4int [10] = {_ T ('\ 0')}; _ itot (-77, szstr4int, 10); Bret = :: writeprivateprofilestring (_ T ("Section1"), _ T ("key12"), szstr4int, ginifilefullpath); If (false = BRET) {_ ftprintf (stderr, _ T ("error: error occurs in calling writeprivateprofilestring,") _ T ("error code is % lu. \ n "),: getlasterror (); Return-1;} BRET =: writeprivateprofilestring (_ T (" Section1 "), _ T (" key13 "), _ T ("value13"), ginifilefullpath); If (false = BRET) {_ ftprintf (stderr, _ T ("error: error occurs in calling writeprivateprofilestring ,") _ T ("error code is % lu. \ n "),: getlasterror (); Return-1 ;} // required writeprivateprofilestring // required parameter tchar szreturnbuffer [1024] = {_ T ('\ 0')}; dwret =: getprivateprofilesectionnames (szreturnbuffer, 1024, ginifilefullpath ); if (1024-2 = dwret) {_ ftprintf (stderr, _ T ("error: szreturnbuffer is not large enough ") _ T ("contain all the section names associated with") _ T ("the specified initialization file. \ n "); Return-1;} // ------------------------------------------- getprivateprofilesectionnames // ------------------------------------------------ getprivateprofilesection // append key14 = defaultvalue14 dwret = :: getprivateprofilesection (_ T ("Section1"), szreturnbuffer, 1024, ginifilefullpath); If (1024-2 = dwret) {_ ftprintf (stderr, _ T ("error: szreturnbuffer is not large enough to ") _ T (" contain all the key name and value pairs ") _ T (" associated with section1. \ n "); Return-1 ;} # If defined (_ Unicode) | defined (UNICODE) DEST = szreturnbuffer + dwret; src = l "key14 = defaultvalue14"; Count = wcslen (L "key14 = defavaluvalue14 "); wmemcpy (DEST, SRC, count); DEST + = count; src = l "\ 0"; Count = 1; wmemcpy (DEST, SRC, count ); # else DEST = szreturnbuffer + dwret; src = "key14 = defaultvalue14"; Count = strlen ("key14 = defaultvalue14"); memcpy (static_cast <void *> (DEST ), static_cast <void *> (SRC), count); DEST + = count; src = "\ 0"; Count = 1; memcpy (static_cast <void *> (DEST ), static_cast <void *> (SRC), count); # endif // defined (_ Unicode) | defined (UNICODE) Bret = :: substring (_ T ("Section1"), szreturnbuffer, ginifilefullpath); If (false = BRET) {_ ftprintf (stderr, _ T ("error: error occurs in calling writeprivateprofilesection, ") _ T (" error code is % lu. \ n "),: getlasterror (); Return-1;} // getprivateprofilesection // ------------------------------------------------ writeprivateprofilesection // In. append section2 zeromemory (static_cast <void *> (szinsertedkeyvaluepair), sizeof (szinsertedkeyvaluepair) to the end of the INI file; # If defined (_ Unicode) | defined; src = l "key21 = defaultvalue21"; Count = wcslen (L "key21 = defaultvalue21"); wmemcpy (DEST, SRC, count); DEST + = count; src = l "\ 0"; Count = 1; wmemcpy (DEST, SRC, count); DEST + = count; src = l "key22 = defaultvalue22 "; count = wcslen (L "key22 = defaultvalue22"); wmemcpy (DEST, SRC, count); DEST + = count; src = l "\ 0"; Count = 1; wmemcpy (DEST, SRC, count); # else DEST = szinsertedkeyvaluepair; src = "key21 = defaultvalue21"; Count = strlen ("key21 = defaultvalue21 "); memcpy (static_cast <void *> (DEST), static_cast <void *> (SRC), count); DEST + = count; src = "\ 0"; Count = 1; memcpy (static_cast <void *> (DEST), static_cast <void *> (SRC), count); DEST + = count; src = "key22 = defavaluvalue22 "; count = strlen ("key22 = defaultvalue22"); memcpy (static_cast <void *> (DEST), static_cast <void *> (SRC), count); DEST + = count; src = "\ 0"; Count = 1; memcpy (static_cast <void *> (DEST), static_cast <void *> (SRC), count ); # endif // defined (_ Unicode) | defined (UNICODE) Bret =: writeprivateprofilesection (_ T ("section2"), szinsertedkeyvaluepair, ginifilefullpath ); if (false = BRET) {_ ftprintf (stderr, _ T ("error: error occurs in calling writeprivateprofilesection,") _ T ("error code is % lu. \ n "),: getlasterror (); Return-1;} // ---------------------------------------------------- writeprivateprofilesection // getprivateprofileint // Note: msdn, the returned data type of this function prototype is uint, but it does not mean that it can only return a // unsigned integer value. through this function ,. the negative number in the INI file can be obtained correctly. int nintvalue4key12 =: getprivateprofileint (_ T ("Section1"), _ T ("key12"),-1, ginifilefullpath ); _ tprintf (_ T ("\ nnintvalue4key12 = % I \ n"), nintvalue4key12); // descrigetprivateprofileint // descrigetprivateprofilestring // read value for key11 dwret = :: getprivateprofilestring (_ T ("Section1"), _ T ("key11"), _ T ("key11 key cannot be found in the initialization file. "), szreturnbuffer, max_path, ginifilefullpath); If (max_path-1 = dwret) {_ ftprintf (stderr, _ T (" error: Neither lpappname nor lpkeyname is null ") _ T ("and the supplied destination buffer is too small") _ T ("to hold the requested string. \ n "); Return-1;} If (max_path-2 = dwret) {_ ftprintf (stderr, _ T (" error: either lpappname or lpkeyname is null and ") _ T (" the supplied destination buffer is too small to ") _ T (" hold all the strings. \ n "); Return-1;} _ tprintf (_ T (" \ nvalue for key11 is % S. \ n "), szreturnbuffer); // read value for key12 dwret =: getprivateprofilestring (_ T (" Section1 "), _ T (" key12 "), _ T ("key12 key cannot be found in the initialization file. "), szreturnbuffer, max_path, ginifilefullpath); If (max_path-1 = dwret) {_ ftprintf (stderr, _ T (" error: Neither lpappname nor lpkeyname is null ") _ T ("and the supplied destination buffer is too small") _ T ("to hold the requested string. \ n "); Return-1;} If (max_path-2 = dwret) {_ ftprintf (stderr, _ T (" error: either lpappname or lpkeyname is null and ") _ T (" the supplied destination buffer is too small to ") _ T (" hold all the strings. \ n "); Return-1;} _ tprintf (_ T (" value for key12 is % S. \ n "), szreturnbuffer); // read value for key13 dwret =: getprivateprofilestring (_ T (" Section1 "), _ T (" key13 "), _ T ("key13 key cannot be found in the initialization file. "), szreturnbuffer, max_path, ginifilefullpath); If (max_path-1 = dwret) {_ ftprintf (stderr, _ T (" error: Neither lpappname nor lpkeyname is null ") _ T ("and the supplied destination buffer is too small") _ T ("to hold the requested string. \ n "); Return-1;} If (max_path-2 = dwret) {_ ftprintf (stderr, _ T (" error: either lpappname or lpkeyname is null and ") _ T (" the supplied destination buffer is too small to ") _ T (" hold all the strings. \ n "); Return-1;} _ tprintf (_ T (" value for key13 is % S. \ n "), szreturnbuffer); // ----------------------------------------------------- getprivateprofilestring return 0 ;}


Refer:

1. msdn online documentation

2. VC operations on the INI File

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.