Re: CString not working as advertised
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 19 Apr 2006 16:03:05 -0400
On Wed, 19 Apr 2006 15:06:53 -0400, R. Christian Call <rccall@xxxxxxxxxxxxxx> wrote:
I recently got hold of a copy of VS 2005, and I've been playing around****
with writing a VC++ MFC application that needs to convert some CString
values to C character strings, in order to pass them to a C function.
I'm having trouble doing that, and I can't seem to find good info about
the problem.
The examples given in the MFC library fail miserably most fo the time--
even for simple stuff that I'd expect to work without a hitch. For
instance, I can't do this:
CString kindOfFruit = "bananas";
Note that default for VS 2005 is Unicode, so a quoted string like above is meaningless.
You could either write
CStringA kindOfFruit = "bananans";
or
CStringW kindOfFruit = L"bananas";
or
CString kindOfFruite = _T("bananas");
but what you wrote should not compile, and the error message below is what you should get.
****
****
... because I get "error C2440: 'initializing' : cannot convert from
'const char [8] to 'ATL::CString<BaseType,StringTraits>' etc. etc. etc.
For passing CString to a routine that expects "char *", the
documentation suggests casting to LPCTSTR. But when I try that, I get
error C2664 saying that the compiler can't convert LPCTSTR to 'char *'
because "Types pointed to are unrelated; conversion requires
reinterpret_cast, C-style cast or function-style cast."
That's because you are using a Unicode string. If you want a char *, either use CStringA
or use the ATL functions such as T2A to convert a CString to an 8-bit string. So indeed
it is unable to convert a Unicode string to a "char *" because they are incompatible,
which is what the error message tells you.
****
****
No matter what I do, I can't seem to create anything that I can cast to
a 'char *'.
Sure you can. But since you have a Unicode app, you have to go through one more level of
translation.
****
****
I figure there's got to be something really obvious here that I've
overlooked, but I'm not seeing it.
Unicode
***
Can anyone help me out?Joseph M. Newcomer [MVP]
-- Chris
(Remove the first "i" to reply.)
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
--
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
.
- Follow-Ups:
- Re: CString not working as advertised
- From: David Wilkinson
- Re: CString not working as advertised
- References:
- CString not working as advertised
- From: R . Christian Call
- CString not working as advertised
- Prev by Date: Re: help about Csocket
- Next by Date: Re: CFileDialog and file extension
- Previous by thread: Re: CString not working as advertised
- Next by thread: Re: CString not working as advertised
- Index(es):
Relevant Pages
|