I have a MFC class derived from CStdioFile declared as follows
// Datafile.h
class CDataFile : public CStdioFile
{
public:
CDataFile(void);
~CDataFile(void);
int OpenFile(LPCWSTR FileName);
}
After my OpenFile function is called the FileName variable is being corrupted.
int CDataFile::OpenFile(LPCWSTR FileName)
{
m_OpenFlags = CFile::modeNoTruncate | CFile::modeReadWrite;
// Before open. FileName = "c:\afile.txt"
if (!Open(FileName, m_OpenFlags, NULL))
{
return GetLastError();
}
//After open. FileName = ""ﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮﻮވĚᗸ÷ᘸ÷㼠碞"
// other stuff
}
}
But if I change the FileName to
WCHAR FileName[] = _T("c:\\afile.txt");
Before opening the File the variable Filename remains untouched. I have seen this behaviour before with the MFC/Winapi and always worked around it be by using character arrays instead of LPCWSTR or CString. Why does this happen? and what is the best way to track down problems such as this with the VS Debugger. The corruption appears to happen here in the MFC file Filecore.cpp
if (!CFile::Open(lpszFileName, (nOpenFlags & ~typeText), pException))
return FALSE;