zoukankan      html  css  js  c++  java
  • 几个非常有用的函数-获取可执行文件所在的目录及读取配置文件

    // 取得Exe所在的目录(不包含最后的"\")
    CString GetExePath()
    {
    	char sFileName[256] = {0};
    	CString sPath = _T("");
    	
    	GetModuleFileName(AfxGetInstanceHandle(), sFileName, 255);
    	sPath.Format("%s", sFileName);
    	int pos = sPath.ReverseFind('\\');
    	if(pos != -1)
    		sPath = sPath.Left(pos);
    	else
    		sPath = _T("");
    	
    	return sPath;
    }
    
    void ReadKeyValue(LPCSTR lpSection, LPCSTR lpKey, LPCTSTR lpDefault, CString &sDest, LPCTSTR sConfigFile)	// 读取字符串
    {
    	char sValue[500] = {0};
    	GetPrivateProfileString(lpSection, lpKey, lpDefault, sValue, 499, sConfigFile);
    	sDest = sValue;
    }
    
    void ReadKeyValue(LPCSTR lpSection, LPCSTR lpKey, int nDefault, UINT &nDest, LPCTSTR sConfigFile)			// 读取整数
    {
    	nDest = GetPrivateProfileInt(lpSection, lpKey, nDefault, sConfigFile);
    }
    
    void ReadKeyValue(LPCSTR lpSection, LPCSTR lpKey, int nDefault, BOOL &nDest, LPCTSTR sConfigFile)			// 读取BOOL数据
    {
    	UINT nTemp;
    	nTemp = GetPrivateProfileInt(lpSection, lpKey, nDefault, sConfigFile);
    	nDest = nTemp != 0;
    }
    

    若有需要,再添加对这几个函数的声明:

    CString GetExePath();
    void ReadKeyValue(LPCSTR lpSection, LPCSTR lpKey, LPCTSTR lpDefault, CString &sDest, LPCTSTR sConfigFile);
    void ReadKeyValue(LPCSTR lpSection, LPCSTR lpKey, int nDefault, UINT &nDest, LPCTSTR sConfigFile);
    void ReadKeyValue(LPCSTR lpSection, LPCSTR lpKey, int nDefault, BOOL &nDest, LPCTSTR sConfigFile);
    
  • 相关阅读:
    03_ if 练习 _ little2big
    uva 11275 3D Triangles
    uva 12296 Pieces and Discs
    uvalive 3218 Find the Border
    uvalive 2797 Monster Trap
    uvalive 4992 Jungle Outpost
    uva 2218 Triathlon
    uvalive 3890 Most Distant Point from the Sea
    uvalive 4728 Squares
    uva 10256 The Great Divide
  • 原文地址:https://www.cnblogs.com/joeblackzqq/p/1947301.html
Copyright © 2011-2022 走看看