.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);
}
#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);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了