Re: CString not working as advertised



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?


-- Chris

(Remove the first "i" to reply.)
Joseph M. Newcomer [MVP]
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
.



Relevant Pages

  • Re: Retrieving text from CEdit box as char *?
    ... Windows CE uses Unicode so strings coming from the OS such as window text ... If you really need a string of char, you need to convert the CString to ... Alternatively, if you don't actually need the string in ASCII format, just ...
    (microsoft.public.windowsce.embedded.vc)
  • Re: CString to const char*
    ... For some reason it will not convert to const char* automatically, ... there more than one implementation of CString available? ... CString moved from the global namespace into the WTL namespace. ... If said string contains a Euro sign and your local codepage doesn't ...
    (microsoft.public.vc.language)
  • Re: peer code review/advice needed for noob programmer
    ... There are lots of easy ways to transition from char to CString; ... Yes, except I am having to program to Microsoft FS's API, and the string is ... if any reason to allocate a buffer here. ...
    (microsoft.public.vc.mfc)
  • Re: CSocket/CAsyncSocket sending and receiving
    ... Stop experimenting with CSocket; it is losing. ... CString constructor will do a MultiByteToWideChar to convert the string. ... sequence of Receives will receive N bytes. ...
    (microsoft.public.vc.mfc)
  • Re: CString Access Viloation
    ... void __stdcall INIT(f...other vars..., CString c_MasterPath) ... Declare Sub INIT Lib "MYDLL.dll" (...other vars..., ... Why the char* works, I don't know, but it does. ... string in as a CString, I passed it in as a char*, and then assign that ...
    (microsoft.public.vc.mfc)

Quantcast