Discussion:
CString to wchar_t* conversions?
(too old to reply)
Daniel
2004-02-17 18:11:05 UTC
Permalink
H

Can anyone tell me how to convert fro

CString to wchar_t
an
wchar_t* to CStrin

thank

-Danie
jason wolfe
2004-02-17 18:21:07 UTC
Permalink
I'm somewhat of a newbie myself, but wchart* is a pointer to a datatype, where CString is a class, and therefor it's (CString's) instantiation would yield and object. Therefore, you would need to create some additional code to write data (presumably your wchart data) to and from the correct CString object member variable

hope this helps.
Jagadeesh
2004-02-17 18:40:38 UTC
Permalink
CString str = "Hello Daniel";LPWSTR lpszW = new WCHAR[255];LPTSTR lpStr =
str.GetBuffer( str.GetLength() );int nLen = MultiByteToWideChar(CP_ACP,
0,lpStr, -1, NULL, NULL);MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW,
nLen);
After having done with lpszW,
use delete[] lpszW;

Cheers
Jagadeesh
Hi
Can anyone tell me how to convert from
CString to wchar_t*
and
wchar_t* to CString
thanks
-Daniel
Cory C. Albrecht
2004-02-17 20:04:58 UTC
Permalink
Post by Jagadeesh
CString str = "Hello Daniel";LPWSTR lpszW = new WCHAR[255];LPTSTR lpStr =
str.GetBuffer( str.GetLength() );int nLen = MultiByteToWideChar(CP_ACP,
0,lpStr, -1, NULL, NULL);MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW,
nLen);
After having done with lpszW,
use delete[] lpszW;
CString str = _T("Some string here");
WCHAR wch[1024] = L"";

if defined(_UNICODE)
swprintf(wch, L"%ls", (LPCTSTR)str);
#else
swprintf(wch, L"%hs", (LPCTSTR)str);
#endif

--
Cory Albrecht
http://s94306345.onlinehome.us/
Jagadeesh
2004-02-17 18:52:38 UTC
Permalink
CString str = "Hello Daniel";
LPWSTR lpszW = new WCHAR[255];
LPTSTR lpStr =str.GetBuffer( str.GetLength() );
int nLen = MultiByteToWideChar(CP_ACP,0,lpStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW,nLen);

After having done with lpszW,
use delete[] lpszW;

Cheers
Jagadeesh
Hi
Can anyone tell me how to convert from
CString to wchar_t*
and
wchar_t* to CString
thanks
-Daniel
Dave Harris
2004-02-19 15:27:46 UTC
Permalink
Post by Jagadeesh
CString str = "Hello Daniel";
LPWSTR lpszW = new WCHAR[255];
LPTSTR lpStr =str.GetBuffer( str.GetLength() );
int nLen = MultiByteToWideChar(CP_ACP,0,lpStr, -1, NULL, NULL);
MultiByteToWideChar(CP_ACP, 0, lpStr, -1, lpszW,nLen);
After having done with lpszW, use delete[] lpszW;
There is no need to use GetBuffer(). If you do use it, you should call
ReleaseBuffer(). If you are going to call MultiByteToWideString() twice, you
should use the length it returns to size the array. The code you've given
could be a security risk if str changes.

Either:
CString str = "Hello Daniel";
int maxLenW = str.GetLength() * 2;
WCHAR *strW = new WCHAR[ maxLenW ];
MultiByteToWideChar(CP_ACP,0,str, -1, strW, maxLenW );

Or:
CString str = "Hello Daniel";
int lenW = MultiByteToWideChar(CP_ACP,0,str, -1, 0, 0 );
WCHAR *strW = new WCHAR[ lenW ];
MultiByteToWideChar(CP_ACP,0,str, -1, strW, lenW );

-- Dave Harris

Ajay Kalra
2004-02-17 18:58:50 UTC
Permalink
If you are using VS6, take a look at T2W and related macros. In VS7, you can use CT2W (T2W
has been deprecated).
--
Ajay Kalra [MVP - VC++]
***@yahoo.com


"Daniel" <***@discussions.microsoft.com> wrote in message news:6B11545B-DE4C-490F-BB18-***@microsoft.com...
| Hi
|
| Can anyone tell me how to convert from
|
| CString to wchar_t*
| and
| wchar_t* to CString
|
| thanks
|
| -Daniel
|
Daniel
2004-02-18 10:41:06 UTC
Permalink
Seems to me like Jagadeesh's way is fairly straight forward. CString.GeBuffer(CString.GetLength()) is the method I was looking for to get access to the Object's data buffer.

Thanks everyone for your help.
Continue reading on narkive:
Loading...