i m using VC++8.0 MFC. i m facing the problem atack corrupt around the variable stCmosPortInput.
i am reading some information from ini file.information is as following format.
//Setting_1 = Floppy Drive Type,0x71,0x10,0x03,0x40;
for each comma serated information i am filling structure.
code for reading ini fileis as follows.
Expand|Select|Wrap|Line Numbers
- FILE *pDefinitionFile;
- CString szTempMsg = _T("");
- CString szErrMsg = _T("");
- //IOREGSETINPUT stIORegSetInput = {0};
- CMOSPORTINPUT stCmosPortInput = {0};
- int nIndex = 0; // index of searched string
- int nCount = 0; //no. of entries in structure.
- int nNoOfBiosSettings = 0;
- CHAR szDefinitionFile[MAX_PATH] = {0};
- CHAR szTempFileBuffer[256] = {0};
- CString szFileData = _T("");
- CString szBiosSettings = _T("");
- CString szBiosSettingName = _T("");
- CString szIOPort = _T("");
- CString szOffSet = _T("");
- CString szMaskValue = _T("");
- CString szExpectedValue = _T("");
- CString szTempSettings = _T("");
- m_listBiosSettings.RemoveAll();
- PerformMessageLogging("Getting values from INI file");
- ZeroMemory(szDefinitionFile,sizeof(szDefinitionFile));
- GetCurrentDirectory(MAX_PATH,szDefinitionFile);
- szDefinitionFile[lstrlen(szDefinitionFile)-3] = 'e';
- szDefinitionFile[lstrlen(szDefinitionFile)-2] = 'n';
- szDefinitionFile[lstrlen(szDefinitionFile)-1] = 'v';
- szDefinitionFile[lstrlen(szDefinitionFile)] = '\\';
- szDefinitionFile[lstrlen(szDefinitionFile)] = '\0';
- strcat_s(szDefinitionFile,INI_FILE_NAME);
- errno_t fileErr;
- fileErr = fopen_s(&pDefinitionFile, szDefinitionFile, "r");
- if(fileErr != 0)
- {
- szTempMsg.LoadString(IDS_ERR_INIFILE);
- szErrMsg.Format(szTempMsg,szDefinitionFile);
- ErrorAndTraceHandler(SOFTWARE_ERR,
- FALSE,
- IDS_FUN_READINIFILE,
- OCR_FILE_ERROR,
- IDS_API_FOPEN,
- IDS_ERR_INIFILE,
- fileErr);
- }
- while(TRUE)
- {
- if(fgets(szTempFileBuffer,sizeof szTempFileBuffer),pDefinitionFile)!= NULL)
- {
- szFileData = szTempFileBuffer;
- if(szFileData.Left(1) == "#")
- {
- continue;
- }
- //e.g. if BIOS setting given in ini file is as following
- //Setting_1 = Floppy Drive Type,0x71,0x10,0x03,0x40;
- PerformMessageLogging(szFileData);
- nNoOfBiosSettings++;
- CString csTemp = _T("");
- nIndex = szFileData.ReverseFind('=');
- szBiosSettings = szFileData.Mid(nIndex+1);
- nIndex = szBiosSettings.Find(";");
- csTemp = szBiosSettings.Left(nIndex);
- m_csArrIniFileData.Add(csTemp);
- PerformMessageLogging(csTemp);
- nIndex = 0;
- szBiosSettingName = _T("");
- nIndex = szBiosSettings.Find(',',0);
- szBiosSettingName = szBiosSettings.Left(nIndex);
- stCmosPortInput.szSettingName = szBiosSettingName;
- PerformMessageLogging(szBiosSettingName);
- // separate string after occurence of first ','
- szTempSettings = szBiosSettings.Mid(nIndex+1);
- // Read I/O Port before second comma
- CString szTemp = _T("");
- LPTSTR BiosTMP = NULL;
- nIndex = 0;
- nIndex = szTempSettings.Find(',',0);
- szIOPort = szTempSettings.Left(nIndex);
- BiosTMP = szIOPort.GetBuffer(szIOPort.GetLength()+1);
- szIOPort.ReleaseBuffer();
- //Store hexadecimal value into decimal format
- //sscanf_s(BiosTMP, "%i", &(stCmosPortInput.ucReadPortAddr));
- sscanf_s(BiosTMP, "%x", &(stCmosPortInput.ucReadPortAddr));
- csTemp.Format("I/O PortAddress in decimal format = %u",stCmosPortInput.ucReadPortAddr);
- PerformMessageLogging(csTemp);
- // Separate string after occurence of second ','
- szTemp = szTempSettings.Mid(nIndex+1);
- //Read Offset of setting before third comma
- nIndex = 0;
- BiosTMP = NULL;
- nIndex = szTemp.Find(',',0);
- szOffSet = szTemp.Left(nIndex);
- BiosTMP = szOffSet.GetBuffer(szOffSet.GetLength()+1);
- szOffSet.ReleaseBuffer();
- //sscanf_s(BiosTMP, "%i", &(stCmosPortInput.ucOffset));
- sscanf_s(BiosTMP, "%x", &(stCmosPortInput.ucOffset));
- csTemp = _T("");
- csTemp.Format("Offset = %u",stCmosPortInput.ucOffset);
- PerformMessageLogging(csTemp);
- // Separate string after occurence of third ','
- szTempSettings = _T("");
- szTempSettings = szTemp.Mid(nIndex+1);
- // Read Mask Value before fourth comma
- nIndex = 0;
- BiosTMP = NULL;
- nIndex = szTempSettings.Find(',',0);
- szMaskValue = szTempSettings.Left(nIndex);
- BiosTMP = szMaskValue.GetBuffer(szMaskValue.GetLength()+1);
- szMaskValue.ReleaseBuffer();
- //sscanf_s(BiosTMP, "%i", &(stCmosPortInput.ucMaskValue));
- sscanf_s(BiosTMP, "%x", &(stCmosPortInput.ucMaskValue));
- csTemp =_T("");
- csTemp.Format("Mask Value = %u",stCmosPortInput.ucMaskValue);
- PerformMessageLogging(csTemp);
- // Separate string after occurence of Fourth ','
- szTemp = _T("");
- szTemp = szTempSettings.Mid(nIndex+1);
- // Read Expected Value before ;
- nIndex = 0;
- BiosTMP = NULL;
- nIndex = szTemp.ReverseFind(';');
- szExpectedValue = szTemp.Mid(0,nIndex);
- BiosTMP = szExpectedValue.GetBuffer(szExpectedValue.GetLength()+1);
- szExpectedValue.ReleaseBuffer();
- //sscanf_s(BiosTMP, "%i", &(stCmosPortInput.ucExpectedValue));
- sscanf_s(BiosTMP, "%x", &(stCmosPortInput.ucExpectedValue));
- csTemp =_T("");
- csTemp.Format("Expected Value = %u",stCmosPortInput.ucExpectedValue);
- PerformMessageLogging(csTemp);
- //PerformMessageLogging("end of setting");
- m_listBiosSettings.AddTail(stCmosPortInput);
- //AddDataToList(&stCmosPortInput);
- //nCount++;
- }
- else
- {
- m_nSettingCount = nNoOfBiosSettings;
- break;
- }
- }
- fclose(pDefinitionFile);
- PerformMessageLogging("Values extracted from INI file");
- if(m_nSettingCount == 0)
- {
- PerformMessageLogging("BIOS settings not found in INI file");
- //szErrMsg.Format("%s",INI_FILE_NAME);
- ErrorAndTraceHandler(COMMON_ERR,
- FALSE,
- IDS_FUN_READINIFILE,
- OCR_FILE_ERROR,
- 0,
- IDS_ERR_NOSETTING,
- 0);
- }