CString behavior

I notice that if I assign a wide string to a CString the resulting CString is automatically converted from wide to char, as in the example below...

CString str;

str = val.bstrVal;

where val is a VARIANT filled in by a query to WMI. The BSTR at val.bstrVal contains wide characters terminated by a NULL and after the assign str is the same text, but characters. Does anyone know where this is "cast" behavior is documented, if it is, and if I can rely on it always happening?

Thanks,
Steve
stevaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NievergeltSenior SW DevCommented:
Hi Steve,

That sounds like your C++ code is not compiler using the _UNICODE define (Project Properties).

Share and Enjoy   Christoph
0
stevaAuthor Commented:
You're right.  It's not.  But does that empower the compiler to automatcially "cast" wide strings to character strings? If I hadn't stumbled onto this I would have thought that some sort of conversion call was going to be necessary first to convert the wide string to a char string before I assigned it to a CString. And if this is what the compiler does indeed do, where is it documented? Can I always count on it?

Again, thanks for any input,
Steve
0
AlexFMCommented:
CString class works according to project configuration. If UNICODE is defined, it keeps UNICODE string. If not, it keeps ANSI string.
If you want to keep wide string in non-Unicode application, use std::wstring.

#include <string>
using namespace std;

wstring s;
str = val.bstrVal;

If you want to convert UNICODE string to ANSI, use ATL conversion macros:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_atl_string_conversion_macros.asp

#include <atlbase.h>

USES_CONVERSION;
CString str;

str = W2A(val.bstrVal);
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

stevaAuthor Commented:
Ok.  So what you seem to be saying is that this automatic cast behavior is a fluke and I shouldn't count on it.

It's not that I want to "keep" UNICODE or "keep" ANSI.  This conversion from wide to char is exactly what I wanted to do, and the simple assignment seems cleaner than hauling in atlbase.h and applying W2A - if it can be counted on.  
0
NievergeltSenior SW DevCommented:
Hi steva

Yes, I would say that sums it up. As long as Microsoft - in their infinite wisdom ;-) - does not change CString UNICODE behaviour, you can count on it.
Well anyway, as long as you do not change your compiler and libraries, you are safe.

Share and Enjoy   Christoph
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stevaAuthor Commented:
Ok guys.  Thanks for the discussion.  I split the points.
0
stevaAuthor Commented:
I just came across the following information in Que's "Practical Visual V++ 6" by Jon bates and Tim Tompkins. They're talking on page  660 about how to convert from a CString to a BSTR and then say "The reverse translation can be performned by using the (char*) and (const char*) casts to turn the BSTR into a null-terminated string."

So an explicit cast certainly works, and I guess when the compiler sees that the CString is the char version, rather than the UNICODE type, it will also do the cast automatically, sort of like when you assign a byte to an integer.

Anyway, I thought I'd toss that in here, in case anyone other than me had an interest.

Steve



0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.