Re: copy from CString to char array

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

From: Ulrich Eckhardt (doomster_at_knuut.de)
Date: 12/22/04


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



Relevant Pages

  • System.IO.DirectoryNotFoundException: HRESULT: 0x80070003
    ... bstrUrl, String& pbstrServerRelativeUrl, String& pbstrTitle, String& ... Int16& pnCollation, UInt32& pnCollationLCID, Int16& pnCalendarType, ... Int16& pnAdjustHijriDays, Int16& pnAltCalendarType, Boolean& ... Win32 Version: 2.0.50727.42 ...
    (microsoft.public.sharepoint.portalserver)
  • Re: Unusal one
    ... sentences in multiple lines in your eVc 4 editor? ... CString variable in which I want to store text like this ... CString string; ... I cannot see it without scrolling horizontally in my Microsoft eVc ...
    (microsoft.public.pocketpc.developer)
  • Re: Unicode question
    ... You are right about the null termination being a problem, I usually use the following when interfacing with Win32: ... std::string string; ... are where I employ CString, otherwise I use std::string as much as possible. ...
    (microsoft.public.vc.language)
  • CDO exception with Web.Mail
    ... CultureInfo culture, String[] namedParameters) ... Win32 Version: 1.1.4322.573 ... CodeBase: ... To enable just in time debugging, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Problem with Find Dialog Box in Internetnet explorer
    ... text editor, which is probably *not* Platform Builder or eVC, you do a ... the string "find". ...
    (microsoft.public.windowsce.platbuilder)