松鼠的博客

导航

统计

.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   Xproer-松鼠  阅读(410)  评论(0编辑  收藏  举报

编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示