Re: copy from CString to char array
From: Ulrich Eckhardt (doomster_at_knuut.de)
Date: 12/22/04
- Next message: Tim Osborne: "How do I detect that the SIP is closing?"
- Previous message: Manuel Meitinger: "Re: copy from CString to char array"
- In reply to: Anatolik KAVERIN: "copy from CString to char array"
- Next in thread: r_z_aret_at_pen_fact.com: "Re: copy from CString to char array"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 22 Dec 2004 15:51:16 +0100
Anatolik KAVERIN wrote:
> i've got some troubles, i'm kinda newbie to eVC:
Nothing specific to eVC, see below...
> dlg.m_ofn.lpstrInitialDir = (CString)"My Documents";
This doesn't work and never has. The expression
(CString)"foo"
is equivalent to
CString("foo")
and creates a temporary. Subsequently, operator LPCTSTR is called on that
temporary and the result is stored in lpstrInitialDir. With the trailing
semicolon, the temporary ceases to exist, and its storage is returned to
the heap. The problem is that lpstrInitialDir still has a pointer to that
storage...
Now, how about removing the CString like this:
lpstrInitialDir = "My Documents";
This method usually works under win32, but it is still wrong. The point is
that lpstrInitialDir is a TCHAR based string, while "My Documents" is a
char based string literal. Under Windows CE and under win32 in general when
using the so-called Unicode mode, TCHAR is not a char string but a wchar_t
string, so you'd need to use L"My docs" instead.
However, this is still one step from perfection. In order to transparently
switch, tchar.h provides a macro. Using that, your code becomes:
lpstrInitialDir = _T("My Documents");
and compiles unchanged under CE and every other win32 environment.
Uli
- Next message: Tim Osborne: "How do I detect that the SIP is closing?"
- Previous message: Manuel Meitinger: "Re: copy from CString to char array"
- In reply to: Anatolik KAVERIN: "copy from CString to char array"
- Next in thread: r_z_aret_at_pen_fact.com: "Re: copy from CString to char array"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|