Click to See Complete Forum and Search --> : Converting CString to wchar_t*
greene
May 11th, 1999, 04:28 AM
How do I convert from a CString to a wide-character array.
wchar_t* str_name;
CString str("Hello");
How would I go about putting "Hello"
into str_name?
Thanks.
This works, but you are coppying a string to an array of unsigned ints, basically.
wchar_t CAray[81];
CString String = "HELLO";
memcpy(CAray, String, 5);
It's hard to tell if this works in the debugger, you have to be in hex mode in the debug window, and look @ the hex values, after executing, and stopping just after the memcpy, the hex values in string match the hex values in CAray.
Joe Farkas
farkas_3@msn.com
farkas-j@grayson.com
Joe O'Leary
May 11th, 1999, 08:49 AM
If you are just talking about putting a hard-coded value in there, it's easy.
wchar_t* wstr_name = L"Hello";
However I assume you want to move a CString variable's value into a wide string. One way to do this is to use the Unicode conversion macros that come with MFC and ATL. For example:
USES_CONVERSION;
CString str(_T("Hello"))
wchar_t* wstr_name = T2W(str.GetBuffer());
str.Release();
Joe O'
msaleemn
February 28th, 2009, 02:15 AM
Hi,,
I need this info.
I am interacting with MSMQ using VC++.
I am able to send messages. But i am not able to send really really large messages, really close to 4 MB but not more than that.
I guess the issue here is not with MSMQ, but to convert CString to wchar_t*.
I am using this code,
int length = data.GetLength(); // data is CString
USES_CONVERSION;
wchar_t *pwcharMessageBody = T2W(data.GetBuffer()); //stack overflow,This line fails for large string
data.ReleaseBuffer();
This is working for most cases. But not working for very large string size.
Any help will be great.
Thanks,
Saleem
0xC0000005
February 28th, 2009, 08:35 AM
A few observations:
I have never used MSMQ but just a quick look appears to indicate that it uses BSTR rather than wchar_t which are technically not the same. While BSTR uses the OLECHAR type which ultimately is wchar_t, its organization in memory is slightly different. There is actually a header preceding the start of the BSTR which stores its length - something that you won't have in wchar_t. Many functions that use BSTR rely on the length header - but not always as a zero-terminated BSTR. The bottom line is that if the function calls for a BSTR then you should use BSTR. Besides, even a multibyte character CString can easily create a BSTR via CString::AllocSysString().
Another suggestion in addition to what has been offered by others is that if you are using a version of VisualStudio with the templated versions of CString then maybe just creating a temporary explicit CStringW is the most simple solution:
CString s = _T("This is my message")
CStringW w = s; // w = L"This is my message"
I haven't actually tested that code but it should work.
Years ago I did some checking of the CString source code in response to another question here and found a practical limit to the length of a CString that doesn't appear to have been documented. I don't recall the specifics, but it seems that CString (at least the old MFC version) uses the stack to store the contents temporarily during copies or other operations. Someone was having problems with extremely large CString lengths (probably much larger than your four megabyte length). I'll try to find that post and reference it here.
Finally, your statement that you are able to send messages - but not really large messages - should be re-examined. It appears that you are not having a problem at all with the conversion - just the size. My suggestion is that you post the MSMQ code that uses your string. I assume that it's some type of implementation of CLSID_MSMQMessage. Show us your string code as you have done above but show us how you use the wchar_t* in the context of a MSMQ method. There are so many ways of debugging code like that (expections, return values, ASSERTs) if only we knew how you were using the string.
Shadowrun
February 28th, 2009, 06:44 PM
As there are a few posts each week involving strings operations (cstring, chars, pointers, unicode...) maybe anyone should post a link which shows maaaany examples about that. ;)
edit: After 1 week.... http://www.codeproject.com/KB/cpp/data_conversions.aspx ^^
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.