473,408 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

CString -> LPCWSTR

Hi,

Any one knows how to convert CString to LPCWSTR?

Please advice, thanks!
-P
Nov 17 '05 #1
5 6568
Hello !

LPCWSTR is a pointer to a constant wchar_t string.

Using Visual Studio .NET 2003 this is an easy task. In the ATL-MFC version
7, a set of conversion macros are provided to simplify the process. So, in
order to convert it, the following command would do it

CT2CW( (LPCTSTR) MyStringObj );

This would return a constant pointer to a wchar_t string (LPCWSTR). Note
that if you wish to save this, reserve memory for a wchar_t string and then
use string copying to copy from the return value of this macro into the new
string. For further information, see MSDN with topic "ATL and MFC String
Conversion Macros". Or use keyword search with the macro name.

If you don't have .NET 2003, you can still do the conversion, but must use
the old-style macros, which are somewhat less sophisticated (read: might
contain bugs). The calling convention of this would be:

T2W( (LPCTSTR) MyStringObj );

Hope this helps !

-Antti Keskinen
"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
Hi,

Any one knows how to convert CString to LPCWSTR?

Please advice, thanks!
-P

Nov 17 '05 #2
Cool, thanks for the info!

-P

"Antti Keskinen" wrote:
Hello !

LPCWSTR is a pointer to a constant wchar_t string.

Using Visual Studio .NET 2003 this is an easy task. In the ATL-MFC version
7, a set of conversion macros are provided to simplify the process. So, in
order to convert it, the following command would do it

CT2CW( (LPCTSTR) MyStringObj );

This would return a constant pointer to a wchar_t string (LPCWSTR). Note
that if you wish to save this, reserve memory for a wchar_t string and then
use string copying to copy from the return value of this macro into the new
string. For further information, see MSDN with topic "ATL and MFC String
Conversion Macros". Or use keyword search with the macro name.

If you don't have .NET 2003, you can still do the conversion, but must use
the old-style macros, which are somewhat less sophisticated (read: might
contain bugs). The calling convention of this would be:

T2W( (LPCTSTR) MyStringObj );

Hope this helps !

-Antti Keskinen
"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
Hi,

Any one knows how to convert CString to LPCWSTR?

Please advice, thanks!
-P


Nov 17 '05 #3
Hello !

This is a simple process. Visual Studio .NET (and ATL-MFC version 7)
supports new conversion macros with the following syntax:

C<sourcetype>2<constant><destination type><ex>

In which, <sourcetype> is T for generic text, A for ANSI, W for wchar_t
types and OLE for COM/BSTR strings (wchar_t normally). <constant> is a 'C'
if the destination pointer must be a constant one, and <destination type> is
the target type, again from the above list of source types. <ex> is 'EX' if
the target string's length needs to be defined in the template.

Thus, converting from CString to LPCWSTR is as simple as this:

CT2CW( (LPCTSTR) MyStringObj );

You could read this like "convert generic type to constant wchar_t".
Creating sentences like this helps you quickly understand how to build the
macro to support a specific conversion. The conversion is actually a class
that uses a certain piece of memory to hold a copy of the string in the
appropriate format. Thus, when the class goes out-of-scope, the new string
is freed. Generally, you should not rely on this, but use the conversion
macro as a parameter to a function or source of a string copying routine.
This allows you to create local strings that last just as long as you want
them to. The conversion should be used with "in-place conversion from one
pointer type to other" -mantra.

Following this idea, this piece of code is BAD, DO NOT USE:

LPCWSTR lpwStr = CT2CW( (LPCTSTR) StringObj );

Instead, something like this is much better, if the string is no longer than
20 characters

wchar_t string[20];
wcscpy( string, CT2CW( (LPCTSTR) StringObj );

If you don't have ATL-MFC v7, you can use the old-style conversion macros,
such as

T2CW( (LPCTSTR) StringObj );

Note that these macros MUST be used in the above mentioned string copying
way. Using them otherwise will have undesirable results.

See MSDN with "ATL and MFC String Conversion Macros" for more information on
the conversion routines. Same topic will cover the old-style conversion
macros.

-Antti Keskinen
"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:76**********************************@microsof t.com...
Hi,

Any one knows how to convert CString to LPCWSTR?

Please advice, thanks!
-P

Nov 17 '05 #4
I've never understood why people rely on these macros instead of doing
things like CStringW(s). Why those ugly hidden stack variables are better
than letting the compiler generate the temporaries automatically?
Nov 17 '05 #5
Guess it's just a matter of preference.

I generally write all my production code with Unicode enabled. This ensures
that the program will work on every Windows operating system, no matter
where it's ran. It makes programming and string manipulation a bit more
difficult (two bytes instead of one for each character), but after getting
used to, it's quite a breeze, while ensuring compatibility.

-Antti Keskinen

"Gabest" <ga****@freemail.hu> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl...
I've never understood why people rely on these macros instead of doing
things like CStringW(s). Why those ugly hidden stack variables are better
than letting the compiler generate the temporaries automatically?

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Rajesh Garg | last post by:
Why and in what situations is CString not preferred? RVG
6
by: Markus Hämmerli | last post by:
I ' ll tra to convert a Cstring to char* without success. I working in a Unicode enabled environment this is working in Unicode CString source = _T("TestString"); TCHAR *szSource =...
8
by: Oskar | last post by:
Hi. I`m new in cpp and i have a litlle problem. i have a CString from Edit Box (eg."aaa bbb ccc 7327373 d feaf 323 dvjiv 234") and i want to put the data (space separated) into an array.It shuld...
5
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/...
3
by: nsyforce | last post by:
What is the correct way to convert a const char* to a CString? I'm somewhat of a newbie and have tried several ways. While they all convert ok, I'm using a profiler that shows a memory leak for...
25
by: Gareth | last post by:
I want to do the following to strings: 1) Check if first four characters are "DATA" 2) Get the middle 'word' from the following string "DATA 123 xyz" (the middle word is variable length) -...
4
by: huguogang | last post by:
Just curious, any one know what the 3 part parameter "class CString filename" would mean. The code: int TestFunc(class CString filename) { fopen(filename, "w"); } Compile using Visaul C++,...
4
by: Susan Rice | last post by:
I'm new to using CString. Why won't the following compile? I'm using Microsoft Visual C++ 6.0 Line 37 which it complains about is the function: 37 CString ConvertFile(char *szFileName) I...
2
by: flyingxu | last post by:
Hi, I run into a cstring related link problem in VC7. My solution has 3 projects, one MFC exe, two MFC extersion DLL. the two MFC extersion DLL export functions which use CString as parameters....
9
by: Donos | last post by:
I have a CString with 100 characters. Now i want to make that to 2 lines. For example, CString str = "This is just a test to find out how to break a cstring"; I want this CString in the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.