long ReadIntegerValueFromIni( const CString& csAppName_i,
const CString& csKeyName_i,
const CString& csDefaultVal_i,
const CString& csFileName_i,
constint nMinVal_i,
constint nMaxVal_i )
{
long lKeyValue = 0;
int nDefaultVal = _ttoi( csDefaultVal_i );
try
{
constint SUCCESS_RET = 1;
TCHAR tcszBuf[ MAX_PATH ];
TCHAR tcszBufTemp[ MAX_PATH ];
lKeyValue = nDefaultVal;
// Read the value from INI file.
GetPrivateProfileString( csAppName_i, csKeyName_i, csDefaultVal_i, tcszBuf, sizeof( tcszBuf ),
csFileName_i );
// Check whether the value read from ini is a valid numeric number or not.
if( SUCCESS_RET != swscanf( tcszBuf, _T( "%d%s" ), &lKeyValue, &tcszBufTemp ))
{
lKeyValue = nDefaultVal;
// Invalid value found so adjusted to Default Value.
}
// Check whether the value read from ini is within the allowed range or not.
elseif( nMinVal_i > lKeyValue || nMaxVal_i < lKeyValue )
{
lKeyValue = nDefaultVal;
// Input value is not within the allowed range, so adjusted to Default Value
}
else
{
// Do nothing.
}
}
catch( ... )
{
// Unhandled exception occured in ReadIntegerValueFromIni()
lKeyValue = nDefaultVal;
}
return lKeyValue;
}