松鼠的博客

导航

.ini 配置文件处理

操作函数:
WritePrivateProfileString
GetPrivateProfileString

// include files 
#include <stdio.h> 
#include 
<windows.h> 
 
// a main function 
main()

    
// local variables 
    CHAR inBuf[80]; 
    HKEY hKey1, hKey2; 
    DWORD dwDisposition; 
    LONG lRetCode; 
 
    
// try to create the .INI file key 
    lRetCode = RegCreateKeyEx ( HKEY_LOCAL_MACHINE, 
                              
"SOFTWARE\\Microsoft\\Windows NT 
                               \\CurrentVersion\\IniFileMapping\\appname.ini"
                              0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, 
                              NULL, 
&hKey1, 
                              
&dwDisposition); 
 
    
// if we failed, note it, and leave 
    if (lRetCode != ERROR_SUCCESS){ 
    printf (
"Error in creating appname.ini key\n"); 
    
return (0) ; 
    } 
 
    
// try to set a section value 
    lRetCode = RegSetValueEx ( hKey1, 
                             
"Section1"
                             
0
                             REG_SZ, 
                             
"USR:App Name\\Section1"
                             
20); 
 
    
// if we failed, note it, and leave 
    if (lRetCode != ERROR_SUCCESS) { 
    printf ( 
"Error in setting Section1 value\n"); 
    
return (0) ; 
    } 
 
    
// try to create an App Name key 
    lRetCode = RegCreateKeyEx ( HKEY_CURRENT_USER,
                              
"App Name",
                              
0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE,
                              NULL, 
&hKey2,
                              
&dwDisposition);
 
    
// if we failed, note it, and leave 
    if (lRetCode != ERROR_SUCCESS)
    { 
        printf (
"Error in creating App Name key\n"); 
        
return (0) ; 
    } 
 
    
// force the system to re-read the mapping into shared memory 
    
//    so that future invocations of the application will see it 
    
//    without the user having to reboot the system 
    WritePrivateProfileStringW( NULL, NULL, NULL, L"appname.ini" ); 
 
    
// if we get this far, all has gone well 
    
// let's write some added values 
    WritePrivateProfileString ("Section1""FirstKey","It all worked out okay.""appname.ini");
    WritePrivateProfileString (
"Section1""SecondKey","By golly, it works.""appname.ini"); 
    WritePrivateProfileSection (
"Section1""ThirdKey = Another Test.","appname.ini"); 
 
    
// let's test our work 
    GetPrivateProfileString ("Section1""FirstKey""Bogus Value: Get didn't work", inBuf, 80"appname.ini"); 
    printf (
"%s", inBuf); 
    
// okay, we are outta here 
    return(0); 
}

posted on 2008-09-23 10:29  Xproer-松鼠  阅读(416)  评论(0)    收藏  举报