Windows API获取系统配置文件的配置参数

来源:互联网 发布:python np.split 编辑:程序博客网 时间:2024/05/17 01:07

在Windows平台下获取系统配置文件(如:System.ini)的配置参数。

系统配置文件System.ini的内容如下:

[SYSTEM]
ServiceIP = 10.128.11.99:60000
CommuType = ShareMemory


代码如下:

包含头文件 Winbase.h (include Windows.h)

//GetCurrentPath()函数获取可执行模块的全路径,并将路径中的"\\"变为‘\’,之后去掉了路径中的可执行文件的名字static void GetCurrentPath(char *PathName){char *p,*q;GetModuleFileName(NULL,PathName,256);q = PathName;do {p = q+1;q = strstr(p,"\\");} while (q);if (p) *p = 0;p = strstr(PathName,":");if (p){if (p - PathName > 1){q = PathName;p--;do {*q++ = *p++;} while(*p != 0);*q = 0;}}}//GetSystemConfig()获取System字段下,键为strKeyName对应的值,如果没有获取到,则以默认值strDefault填充。void GetSystemConfig( string strKeyName,string strDefault,char *szReciBuff,int nLen ){char szFileName[256];GetCurrentPath(szFileName);strcat(szFileName,"System.ini");GetPrivateProfileString("SYSTEM",strKeyName.c_str(),strDefault.c_str(),szReciBuff,nLen,szFileName);}//GetSystemIPConfig()获取系统文件中ServiceIP键的值
void GetSystemIPConfig( char *szReciBuff,int nLen ){string strDefaultV="127.0.0.1:60000";GetSystemConfig("ServiceIP",strDefaultV,szReciBuff,nLen);}
//GetSystemCommTypeConfig()获取系统文件中ShareMemory键的值void GetSystemCommTypeConfig( char *szReciBuff,int nLen ){string strDefaultV="ShareMemory";GetSystemConfig("CommuType",strDefaultV,szReciBuff,nLen);}//测试代码:int main(){char szCommuType[256]="";GetSystemCommTypeConfig(szCommuType,256);GetSystemIPConfig(szCommuType,256);}