Windows API獲取系統配置文件的配置參數

在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);
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章