Re: LPCWSTR Variable Corrupted

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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
****

Giovanni


Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: POSIX equivalent to readlink utility ?
    ... filename is newer than a certain internally defined period. ... I was speaking to the algorithm posed by the ... vary, the timestamp is not ... So your script may be useful, but not for the reason of timestamps. ...
    (comp.unix.shell)
  • Re: LPCWSTR Variable Corrupted
    ... FileName is an optional parameter "int OpenFile(LPCTSTR FileName = ... class CDataFile: public CStdioFile ... Open(pszFileName, openFlags, NULL)) ... should be just fine as an input read-only string parameter.) ...
    (microsoft.public.vc.mfc)
  • Re: File Rename Utility
    ... also all files containing "token" in the 8.3 filename. ... For that reason, you should *never* trust the output ... it checks for a tilde in the filename. ...
    (microsoft.public.win2000.general)
  • Re: HANDLE hFile
    ... and that fileName is not a variable name; if it is a variable name, ... since it only makes sense when string or character literals are used. ... There is always a reason, and you didn't give the reason. ... It works on debug mode. ...
    (microsoft.public.vc.mfc)
  • Re: Dir statement throws a type mismatch
    ... For that reason and because it failed after ... rebooting, I then would conclude the issue is not with it being already ... Clifford Bass ... I doublechecked the spelling of the path and filename, ...
    (microsoft.public.access.modulesdaovba)