In the following code if I comment out the call to “GetCurrentDirectory” everything works fine, but if I don’t then the code breaks after it, no child windows show up, but the program don’t crash. The compiler doesn’t give any error.
char *iniFilePath;
int lenWritten = GetCurrentDirectory( MAX_PATH, iniFilePath );
if( lenWritten )
{
lstrcat( iniFilePath, iniFileName.c_str() );
char *buffer;
GetPrivateProfileString( iniServerSectionName.c_str(), serverIp.c_str(), "", buffer, MAX_PATH, iniFilePath );// server ip
MessageBox( 0, buffer, 0, 0 );
}
else
{
MessageBox( 0,0,0,0 );
}
iniFilePathis an unintialised pointer whichGetCurrentDirectory()is attempting to write to, causing undefined behaviour.GetCurrentDirectory()does not allocate a buffer for the caller: it must be provided.Change to:
Instead of using
lstrcat(), which has Warning Do not use message on its reference page, construct the path use astd::stringinstead to avoid potential buffer overruns:Note similar issue with
buffer, as pointed out by Wimmel.