0
我从CStdioFile派生的MFC类声明如下HOWTO追查可变腐败
// Datafile.h
class CDataFile : public CStdioFile
{
public:
CDataFile(void);
~CDataFile(void);
int OpenFile(LPCWSTR FileName);
}
我的OpenFile函数被调用的文件名变量被损坏后。
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
}
}
但如果我更改文件名以
WCHAR FileName[] = _T("c:\\afile.txt");
打开文件之前变量文件名保持不变。我已经在MFC/Winapi中看到过这种行为,并且总是通过使用字符数组而不是LPCWSTR或CString来解决它。为什么会发生?以及用VS调试器追踪这些问题的最佳方法是什么?腐败出现在MFC文件在这里发生Filecore.cpp
if (!CFile::Open(lpszFileName, (nOpenFlags & ~typeText), pException))
return FALSE;
(http://msdn.microsoft.com/en-us/library/hwbccf8z.aspx)表示它需要LPCTSTR而不是LPCWSTR(普通字符与宽字符串)。也许这是问题? – 2009-11-20 17:20:53
我不这么认为“typedef LPCWSTR LPCTSTR;” – Canacourse 2009-11-20 17:24:31