Re: LPCWSTR Variable Corrupted
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Mon, 23 Nov 2009 17:20:20 -0500
See below...
On Mon, 23 Nov 2009 18:39:50 +0100, "Giovanni Dicanio"
<giovanniDOTdicanio@xxxxxxxxxxxxxxxxx> wrote:
"Canacourse" <canacourse@xxxxxxxxx> ha scritto nel messaggio****
news:90a8a681-9933-4f86-9693-34cf1f2f87e4@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
No casting involved. That problem of course happens if a FileName
parameter is passed but for test/tracking purposes I was setting the
name of the file in the CDataFile::OpenFile function itself.
FileName = _T("c:\\SomeFile.Txt");
FileName is an optional parameter "int OpenFile(LPCTSTR FileName =
0)" and if not passed the OpenFile Function will create a temp file.
I tried something like this:
<code>
class CDataFile : public CStdioFile
{
public:
CDataFile(void)
{
}
~CDataFile(void)
{
}
DWORD OpenFile(LPCTSTR pszFileName)
{
UINT openFlags = CFile::modeNoTruncate | CFile::modeReadWrite;
if (! Open( pszFileName, openFlags, NULL) )
{
return GetLastError();
}
return 0;
}
};
</code>
with a simple test like this:
<code>
CDataFile df;
df.OpenFile( _T("c:\\test.txt") );
</code>
I set a breakpoint on OpenFile call, and I see no corruption of
'pszFileName' string.
You may want to show us the actual code to try to get a better help.
(Joe suggested to pass a 'const CString &', but in general an 'LPCTSTR'
should be just fine as an input read-only string parameter.)
The reason I suggested const CString & is to break the OP of the habit of creating a lot
of raw character-array and pointer-to-character-array code. I find it an unfortunate
misfeature that some people keep insisting on using these base types when there is no
reason to do so. You only use LPTSTR/LPSTR under very restricted and exotic conditions,
such as when you do a GetBuffer so you can do a low-level transfer into the buffer (e.g.,
via ReadFile, or CAsyncSocket::Receive), and I see no reason a LPCTSTR or LPCWSTR would
ever need to exist in this code.
The const CString & is the fastest way to pass a CString because it doesn't involve a
potential copy.
joe
****
Joseph M. Newcomer [MVP]
Giovanni
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- LPCWSTR Variable Corrupted
- From: Canacourse
- Re: LPCWSTR Variable Corrupted
- From: Mihai N.
- Re: LPCWSTR Variable Corrupted
- From: Canacourse
- Re: LPCWSTR Variable Corrupted
- From: Giovanni Dicanio
- Re: LPCWSTR Variable Corrupted
- From: Giovanni Dicanio
- Re: LPCWSTR Variable Corrupted
- From: Canacourse
- Re: LPCWSTR Variable Corrupted
- From: Giovanni Dicanio
- Re: LPCWSTR Variable Corrupted
- From: Canacourse
- Re: LPCWSTR Variable Corrupted
- From: Giovanni Dicanio
- LPCWSTR Variable Corrupted
- Prev by Date: Re: Scrollbar thumbtrack doesn't scroll all the way
- Next by Date: Re: Scrollbar thumbtrack doesn't scroll all the way
- Previous by thread: Re: LPCWSTR Variable Corrupted
- Next by thread: Re: LPCWSTR Variable Corrupted
- Index(es):
Relevant Pages
|