c++获取ini配置文件。ini配置文件和exe执行程序同在一个目录下的信息

配置文件Config.ini如下:

pch.h文件

#include <atlstr.h>
#include <string.h>
#include <iostream>
#include <windows.h>
#include <stdio.h>
//#include <malloc.h>
//#define _CRT_SECURE_NO_WARNINGS

class Config {
    
    public:
char *getConfigInfo(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize,CString szIniPath);
		CString getPath();
int getNumber(LPCTSTR lpAppName, LPCTSTR lpKeyName, DWORD nSize, CString szIniPath);

};

test.cpp文件

#include "pch.h"
using namespace std;
extern char* ConvertLPWSTRToLPSTR(LPWSTR lpwszStrIn);
//#include <malloc.h>
//#define _CRT_SECURE_NO_WARNINGS

int main() {
	TCHAR jdbc[MAX_PATH] = { 0 };
	TCHAR data[MAX_PATH] = { 0 };
	TCHAR user[MAX_PATH] = { 0 };
	TCHAR pass[MAX_PATH] = { 0 };
	Config con;
	CString szIniPath =con.getPath();
	wcout << "szIniPath====" << szIniPath.GetString() << endl;
	char *jdbcUrl =con.getConfigInfo(TEXT("Mysql"), TEXT("jdbcUrl"), NULL, jdbc, MAX_PATH, szIniPath);
	char *database = con.getConfigInfo(TEXT("Mysql"), TEXT("database"), NULL, data, MAX_PATH, szIniPath);
int nValue = ::GetPrivateProfileInt(TEXT("Section1"), TEXT("time"), 0, szIniPath);
	wcout << "database====" << database << endl;
	wcout << "jdbcUrl====" << jdbcUrl << endl;
    wcout << "nValue ====" << nValue << endl;
}
CString Config::getPath() {
	// ----------------------------------------  
	// 一个config.ini  
	// ----------------------------------------  
	// 得到exe执行路径.  
	TCHAR tcExePath[MAX_PATH] = { 0 };
	::GetModuleFileName(NULL, tcExePath, MAX_PATH);
	// 设置ini路径到exe同一目录下  
    #ifndef CONFIG_FILE  
    #define CONFIG_FILE     (TEXT("Config.ini"))  
    #endif  
    //_tcsrchr() 反向搜索获得最后一个'\\'的位置,并返回该位置的指针  
	TCHAR *pFind = _tcsrchr(tcExePath, '\\');
	if (pFind == NULL)
	{
		return "0";
	}
	*pFind = '\0';
	CString szIniPath = tcExePath;
	szIniPath += "\\";
	szIniPath += CONFIG_FILE;
	wcout << "szIniPath=" << szIniPath.GetString() << endl;
	return szIniPath;
}
char *Config::getConfigInfo(LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpDefault, LPTSTR lpReturnedString, DWORD nSize, CString szIniPath) {//获得字符串
	//下面执行读取 ----------------------------------  
	if (!::PathFileExists(szIniPath))
	{
		return 0;
	}
	//--------------------------------------------------------  
	//DWORD GetPrivateProfileString(  
	//                              LPCTSTR lpAppName,            // 节名  
	//                              LPCTSTR lpKeyName,            // 键名,读取该键的值  
	//                              LPCTSTR lpDefault,            // 若指定的键不存在,该值作为读取的默认值  
	//                              LPTSTR lpReturnedString,      // 一个指向缓冲区的指针,接收读取的字符串  
	//                              DWORD nSize,                  // 指定lpReturnedString指向的缓冲区的大小  
	//                              LPCTSTR lpFileName            // 读取信息的文件名。若该ini文件与程序在同一个目录下,  
	//                                                                也可使用相对路径,否则需要给出绝度路径  
	
	//--------------------------------------------------------  
	::GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, lpReturnedString, nSize, szIniPath);
	CString CSConfig = lpReturnedString;
	char *configIn = ConvertLPWSTRToLPSTR(CSConfig.GetBuffer());
	//int nValue = ::GetPrivateProfileInt(TEXT("Service"), TEXT("Description"), 0, szIniPath);
	//cout <<"nValue="<< nValue << endl;
	//wcout << "jdbcUrl==" << jdbcUrl << endl;
	return configIn;
};

int Config::getNumber(LPCTSTR lpAppName, LPCTSTR lpKeyName,DWORD nSize, CString szIniPath) {//获取整型
	//下面执行读取 ----------------------------------  
	if (!::PathFileExists(szIniPath))
	{
		return 0;
	}
	
	//UINT GetPrivateProfileInt(  
	//                              LPCTSTR lpAppName,            // 节名  
	//                              LPCTSTR lpKeyName,            // 键名,读取该键的值  
	//                              INT nDefault,                 // 若指定的键名不存在,该值作为读取的默认值  
	//                              LPCTSTR lpFileName            // 同上  
	//  
	//--------------------------------------------------------  
	
	//int nValue = ::GetPrivateProfileInt(TEXT("Service"), TEXT("Description"), 0, szIniPath);
	int nValue = ::GetPrivateProfileInt(lpAppName,lpKeyName,nSize, szIniPath);
	wcout <<"nValue="<< nValue << endl;
	return nValue;
};
//****************************************************************************************** /
char* ConvertLPWSTRToLPSTR(LPWSTR lpwszStrIn)
{
	LPSTR pszOut = NULL;
	try
	{
		if (lpwszStrIn != NULL)
		{
			int nInputStrLen = wcslen(lpwszStrIn);

			// Double NULL Termination  
			int nOutputStrLen = WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;
			pszOut = new char[nOutputStrLen];

			if (pszOut)
			{
				memset(pszOut, 0x00, nOutputStrLen);
				WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);
			}
		}
	}
	catch (std::exception e)
	{
	}
	return pszOut;
}

猜你喜欢

转载自blog.csdn.net/qq_34227896/article/details/83058886