首先默认的是当前目录,然后是系统目录、Windows目录
差异在于少了指定文件,指定文件在CWinApp::m_pszProfileName中定义。并且少了指定长度。1。CWinApp::GetProfileString
GetProfileString( LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL );
2。DWORD GetProfileString(
LPCTSTR lpAppName, // address of section name
LPCTSTR lpKeyName, // address of key name
LPCTSTR lpDefault, // address of default string
LPTSTR lpReturnedString, // address of destination buffer
DWORD nSize // size of destination buffer
);
差异在于少了指定文件,指定文件在CWinApp::m_pszProfileName中定义。并且少了指定长度。1。CWinApp::GetProfileString
GetProfileString( LPCTSTR lpszSection, LPCTSTR lpszEntry, LPCTSTR lpszDefault = NULL );
2。DWORD GetProfileString(
LPCTSTR lpAppName, // address of section name
LPCTSTR lpKeyName, // address of key name
LPCTSTR lpDefault, // address of default string
LPTSTR lpReturnedString, // address of destination buffer
DWORD nSize // size of destination buffer
);
CWinApp* pApp = AfxGetApp();
CString strValue;
strValue=pApp->GetProfileString(_T("Network"), _T("UDP_IP"), _T("192.168.1.9"));
怎么总是取到默认的值,而取不到配置文件中的值?
strValue=pApp->GetProfileString(_T("Network"), _T("UDP_IP"), _T("192.168.1.9"));
char path[256];
memset(path,0,sizeof(path));
GetModuleFileName(NULL,path,255);
CString strPath = path;
int index = strPath.ReverseFind('\\');
strPath = strPath.Left(index);
memset(path,0,sizeof(path));
strcpy(path,strPath);
free((void*)theApp.m_pszProfileName);
if(path[0] == 0)
theApp.m_pszProfileName = _tcsdup(_T("C:\\my.ini"));
else
{
strcat((char *)path,_T("\\my.ini"));
theApp.m_pszProfileName = _tcsdup((const char *)path);
}
這樣就設置了 ini 的位置
那兩個函數的區別,與普通的 sdk 函數與 mfc 包裝過的函數的區別一致
//add this modify the direct of my.ini
char path[256];
memset(path,0,sizeof(path));
GetModuleFileName(NULL,path,255);
CString strPath = path;
int index = strPath.ReverseFind('\\');
strPath = strPath.Left(index);
memset(path,0,sizeof(path));
strcpy(path,strPath);
free((void*)theApp.m_pszProfileName);
if(path[0] == 0)
theApp.m_pszProfileName = _tcsdup(_T("C:\\my.ini"));
else
{
strcat((char *)path,_T("\\my.ini"));
theApp.m_pszProfileName = _tcsdup((const char *)path);
}
就把該 my.ini 設置到與 exe 同一目錄了
注釋了嗎?不注釋是讀註冊表
free((void*)theApp.m_pszProfileName);
theApp.m_pszProfileName = _tcsdup(_T("C:\\windows\\win.ini"));
CString strValue;
strValue = theApp.GetProfileString(_T("windows"), _T("run"), _T("failed"));
測試成功
请问gameboy999(我心迷茫),直接用API读取要怎么读?
配置文件要放在哪里才能让程序读取到?
讀寫的是註冊表,不會有配置文件,
別忘了給分
error C2660: 'GetProfileStringA' : function does not take 3 parameters
这是怎么回事?
然後把
//SetRegistryKey(_T("Local AppWizard-Generated Applications"));
free((void*)theApp.m_pszProfileName);
theApp.m_pszProfileName = _tcsdup(_T("C:\\windows\\win.ini"));加在 initinstance 中
在 dll 中WORD GetPStr_GU(CString &returnstr)
{
CWinApp * pApp = AfxGetApp();
returnstr = pApp->GetProfileString(("windows"), ("run"), ("failed"));
return returnstr.GetLength();
}另外,::GetProfileString()
::GetProfileString(("Mail"), ("CMCDLLNAME"), ("failed"),
(LPTSTR)returnstr,255);
只能讀取標準的 Win.ini ,只能讀取該文件,可以在不能使用mfc類庫的dll中使用
指定的 ini 文件只要 dll 中可以使用 CWinApp 就可以了
memset(path,0,sizeof(path));
GetModuleFileName(NULL,path,256);
::lstrcat(path, _T("\\getini.ini"));
char buffer[256];
unsigned long length;free((void*)theApp.m_pszProfileName);
theApp.m_pszProfileName = _tcsdup((const char *)path);
length=GetProfileString(_T("Network"),_T("UDP_IP"),_T("192.168.1.255"),buffer,256);
memset(path,0,sizeof(path));
GetModuleFileName(NULL,path,256);
::lstrcat(path, _T("\\getini.ini"));
char buffer[256];
unsigned long length;free((void*)theApp.m_pszProfileName);
theApp.m_pszProfileName = _tcsdup((const char *)path);應該放在 主程序 中,在 dll 中這樣用CWinApp * pApp = AfxGetApp();
returnstr = pApp->GetProfileString(("windows"), ("run"), ("failed"));如果你的 dll 中不可以使用 CWinApp 則,無法讀取指定文件.
;;GetProfileString(_T("Network"),_T("UDP_IP"),_T("192.168.1.255"),buffer,256);
使用 5 個參數,只能讀取 win.ini
memset(path,0,sizeof(path));
GetCurrentDirectory(256,path);
::lstrcat(path, _T("\\getini.ini"));free((void*)theApp.m_pszProfileName);
theApp.m_pszProfileName = _tcsdup((const char *)path);
CWinApp * pApp = AfxGetApp();
CString returnstr =pApp->GetProfileString(("Network"), ("UDP_IP"), ("192.168.1.255"));
回头就给分
free((void*)theApp.m_pszProfileName);
theApp.m_pszProfileName = _tcsdup(_T("C:\\windows\\win.ini"));
CString returnstr=theApp.GetProfileString(("Network"),("UDP_IP"), ("failed"));
int a = returnstr.GetLength() ;win.ini中有如下字段:
[Network]
[UDP_IP]=192.168.1.255
可是读的结果是“failed”。
是不是我的机子中邪了?? :(
[Network]
UDP=1.1.1.1
却读不出来????
WritePrivateProfileString(...) //写,看MSDN
给分!!