一个写ini文件的问题!!求教!!!!

flyliyang 2004-11-15 10:14:22
void Dlg::OnBnClickedButton1()
{
char IniFileName[255];//ini文件的名字
char WinNum[255];
CFile cfile;
CFileException einfo;

::WritePrivateProfileString("winnum",
"count",
WinNum, //写入一个数字
IniFileName);

strcpy(str2,"name");
strcat(str2,WinNum);
strcat(str2,"=");
//////////////////////////////////////////////////////////////////////////
// cfile.Open(IniFileName,CFile::modeReadWrite,&einfo);//打开信息文件 //
// cfile.SeekToEnd();//定位到文件的尾部 //
// StrLong=strlen(str2); //
// cfile.Write(str2,StrLong);//将字符串插入文件的尾部 //
// cfile.Close;//将文件关闭 //
//////////////////////////////////////////////////////////////////////////
//如果不执行cfile的代码下面的API函数可用
//?????????????为什么在执行完cfile的代码后,下面的写ini文件的API函数不管用了???

::WritePrivateProfileString("window",
"name23",
"DKillerly",
IniFileName);
}

为什么用了框起来的那些代码后,再用WritePrivateProfileString这个API函数就不行了?
死活数据写不到ini文件中.小弟被困扰好几天了.急求.....快急死了...
...全文
178 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yanw0212 2004-11-15
  • 打赏
  • 举报
回复
bool GetINIData()
{
char ServerName[200];
char DBName[200];
char AdministratorName[200];
char AdministratorPWD[200];
char LoginName[200];
char LoginPWD[200];
char JKManagerIP[200];
char WYManagerIP[200];
char ServiceIP[200];
char DBBackupPath[200];
char DBBackupInterval[200];

memset(ServerName,0,200);
memset(DBName,0,200);
memset(AdministratorName,0,200);
memset(AdministratorPWD,0,200);
memset(LoginName,0,200);
memset(LoginPWD,0,200);
memset(JKManagerIP,0,200);
memset(WYManagerIP,0,200);
memset(ServiceIP,0,200);
memset(DBBackupPath,0,200);
memset(DBBackupInterval,0,200);

g_TimeOut = 0 ;

NoQued = 0;
g_PortNum = 0;

NoQued1 = 0;
g_PortNum1 = 0;

NoQued2 = 0;
g_PortNum2 = 0;

TCHAR *path = new char[MAX_PATH+1];
GetModuleFileName(::GetModuleHandle(NULL),path,MAX_PATH);
CString INIFilePath = path;
INIFilePath.TrimRight("\\AccessControlService.exe");
g_ExeFileDirectory = INIFilePath + "\\AlarmManager.exe" ;
CString INIFilePathAndName = INIFilePath + "\\Service.INI" ;

if(!PathFileExists(INIFilePathAndName) )
{
::MessageBox(NULL,"配置文件不存在,AccessControlService服务不能运行","AccessControlService提示信息",MB_OK | 0x00200000L | MB_ICONASTERISK);
return false;
}

CFile f(INIFilePathAndName,CFile::modeRead );

char buf[2000];
memset(buf,0,2000);
UINT nBytesRead = f.Read(buf,2000);

DWORD filelen = f.GetLength();
char dataout[2000];
memset(dataout,0,2000);

C3DES m_des;
m_des.initkeys(inikey1,inikey2,inikey3);
int ii = m_des.DataReverseEncrypt( (UCHAR *)buf, (UCHAR *)dataout,filelen);
if(ii == -1)
{
MessageBox(NULL,"配置文件数据解密错误,AccessControlService服务不能运行!","AccessControlService提示信息",MB_OK | 0x00200000L | MB_ICONASTERISK);
return false;
}

CString DecryptFileAndName = INIFilePath + "\\.bak";
HANDLE hFile = CreateFile( DecryptFileAndName,
GENERIC_WRITE,
FILE_SHARE_READ,
NULL, CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

if (hFile == INVALID_HANDLE_VALUE)
AfxMessageBox(_T("Couldn't create the file!"));
else
{
CFile myFile((int)hFile);
myFile.Write((char*)dataout, lstrlen((char*)dataout));
}

// 读取数据库参数
GetPrivateProfileString( "DataBase","ServerName","",ServerName,sizeof(ServerName),DecryptFileAndName);
GetPrivateProfileString( "DataBase","DBName","",DBName,sizeof(DBName),DecryptFileAndName);
GetPrivateProfileString( "DataBase","AdministratorName","",AdministratorName,sizeof(AdministratorName),DecryptFileAndName);
GetPrivateProfileString( "DataBase","AdministratorPWD","AdministratorPWD error!",AdministratorPWD,sizeof(AdministratorPWD),DecryptFileAndName);
GetPrivateProfileString( "DataBase","LoginName","",LoginName,sizeof(LoginName),DecryptFileAndName);
GetPrivateProfileString( "DataBase","LoginPWD","",LoginPWD,sizeof(LoginPWD),DecryptFileAndName);
g_TimeOut = GetPrivateProfileInt("DataBase","TimeOut", 0, DecryptFileAndName);

// 读取数据库备份参数
GetPrivateProfileString( "DBBackup","DBBackupPath","",DBBackupPath,sizeof(DBBackupPath),DecryptFileAndName);
GetPrivateProfileString( "DBBackup","DBBackupInterval","",DBBackupInterval,sizeof(DBBackupInterval),DecryptFileAndName);

// 读取门禁服务器参数
GetPrivateProfileString( "AccessControlService","JKManagerIP","",JKManagerIP,sizeof(JKManagerIP),DecryptFileAndName);
GetPrivateProfileString( "AccessControlService","WYManagerIP","",WYManagerIP,sizeof(WYManagerIP),DecryptFileAndName);
GetPrivateProfileString( "AccessControlService","ServiceIP","",ServiceIP,sizeof(ServiceIP),DecryptFileAndName);
NoQued = GetPrivateProfileInt("AccessControlService","NoQued", 0, DecryptFileAndName);
NoQued1 = GetPrivateProfileInt("AccessControlService","NoQued1", 0, DecryptFileAndName);
NoQued2 = GetPrivateProfileInt("AccessControlService","NoQued2", 0, DecryptFileAndName);
g_PortNum = GetPrivateProfileInt("AccessControlService","PortNum", 0, DecryptFileAndName);
g_PortNum1 = GetPrivateProfileInt("AccessControlService","PortNum1", 0, DecryptFileAndName);
g_PortNum2 = GetPrivateProfileInt("AccessControlService","PortNum2", 0, DecryptFileAndName);

CloseHandle(hFile) ;
DeleteFile(DecryptFileAndName);

// 转换数据库参数
servername = ServerName ;
dbname = DBName ;
administratorname = AdministratorName;
administratropwd = AdministratorPWD ;
loginname = LoginName ;
loginpwd = LoginPWD ;
g_strTimeOut.Format("%d",g_TimeOut);

// 转换数据库备份参数
g_DBBackupPath = DBBackupPath ;
g_DBBackupInterval = DBBackupInterval ;

// 转换门禁服务器参数
g_WatchControlCenter = JKManagerIP;
g_WYManager = WYManagerIP ;
g_ServiceIP = ServiceIP;
g_strPortNum.Format("%d",g_PortNum);
g_strPortNum1.Format("%d",g_PortNum1);
g_strPortNum2.Format("%d",g_PortNum2);

return true;
}
aben456 2004-11-15
  • 打赏
  • 举报
回复
GetLastError()?

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧