void CAamsProtocol::WriteConfig()
{
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;
}
*pFind = '\0';
CString szIniPath = tcExePath;
szIniPath += "\\";
szIniPath += CONFIG_FILE;
//--------------------------------------------------------
//BOOL WritePrivateProfileString(
// LPCTSTR lpAppName, //节的名字,是一个以0结束的字符串
// LPCTSTR lpKeyName, //键的名字,是一个以0结束的字符串。若为NULL,则删除整个节
// LPCTSTR lpString, //键的值,是一个以0结束的字符串。若为NULL,则删除对应的键
// LPCTSTR lpFileName //要写入的文件的文件名。若该ini文件与程序在同一个目录下,
// ) 也可使用相对路径,否则需要给出绝度路径。
//如果Ini不存在,它会自动在szIniPath上创建此INI文件.再执行写入.
CString ip1;
ip1.Format(_T("%d"),(work.nSocketAddress & 0xFF000000) >> 24);
CString ip2;
ip2.Format(_T("%d"),(work.nSocketAddress & 0x00FF0000) >> 16);
CString ip3;
ip3.Format(_T("%d"),(work.nSocketAddress & 0x0000FF00) >> 8);
CString ip4;
ip4.Format(_T("%d"),(work.nSocketAddress & 0x000000FF) );
::WritePrivateProfileString("Login", "ip1", ip1, szIniPath);
::WritePrivateProfileString("Login", "ip2", ip2, szIniPath);
::WritePrivateProfileString("Login", "ip3", ip3, szIniPath);
::WritePrivateProfileString("Login", "ip4", ip4, szIniPath);
CString port;
port.Format(_T("%d"),work.nHostPort);
::WritePrivateProfileString("Login", "port", port, szIniPath);
CString type;
type.Format(_T("%d"),(int)work.type);
::WritePrivateProfileString("Login", "worktype", type, szIniPath);
::WritePrivateProfileString("Login", "site", work.szSite, szIniPath);
::WritePrivateProfileString("Login", "user", work.szUser, szIniPath);
}
void CAamsProtocol::ReadConfig()
{
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;
}
*pFind = '\0';
CString szIniPath = tcExePath;
szIniPath += "\\";
szIniPath += CONFIG_FILE;
if (!::PathFileExists(szIniPath))
{
AfxMessageBox(_T("配置文件不存在!"));
return;
}
int ip1 = ::GetPrivateProfileInt("Login", "ip1", 192, szIniPath);
int ip2 = ::GetPrivateProfileInt("Login", "ip2", 168, szIniPath);
int ip3 = ::GetPrivateProfileInt("Login", "ip3", 1, szIniPath);
int ip4 = ::GetPrivateProfileInt("Login", "ip4", 42, szIniPath);
work.nSocketAddress = ((ip1<<24) & 0xFF000000)+((ip2<<16) & 0x00FF0000)+((ip3<<8) & 0x0000FF00)+(ip4 & 0x000000FF);
work.nHostPort = ::GetPrivateProfileInt("Login", "port", 6000, szIniPath);
work.type = (EWorkType)::GetPrivateProfileInt("Login", "worktype", 0, szIniPath);
::GetPrivateProfileString("Login", "site", "1", work.szSite, MAX_PATH, szIniPath);
::GetPrivateProfileString("Login", "user", "admin", work.szUser, MAX_PATH, szIniPath);
::GetPrivateProfileString("Login", "password", "admin", work.szPassword, MAX_PATH, szIniPath);
}