• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 492
  • Last Modified:

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
0
steva
Asked:
steva
  • 4
  • 2
2 Solutions
 
NievergeltCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 
NievergeltCommented:
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
 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now