[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to convert a CString to a LPCWSTR

Posted on 2005-04-26
9
Medium Priority
?
1,873 Views
Last Modified: 2008-01-09
I have a Cstring and I need to convert it to a LPCWSTR.  How can this be done?

PT
0
Comment
Question by:ptrennum
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:ptrennum
ID: 13869289
I actually need to convert it to a wchar_t - Sorry about that, the conversion for the CString would be:

(LPCWSTR)(cstring)
0
 

Author Comment

by:ptrennum
ID: 13869396
ok increased points on this one asI'm loooking for a relatively quick response
PT
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13869473
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13869484
0
 
LVL 86

Expert Comment

by:jkr
ID: 13869538
I'd use

CString strTest = "Test";
wchar_t* pwsz = new wchar_t[strTest.GetLength() + 1];

mbstowcs(awc, (LPCTSTR) strTest, strTest.GetLength() + 1);
0
 

Author Comment

by:ptrennum
ID: 13869637
The problem I'm having is with the privilege code from yesterday.  the pwszAccount is asking to be converted from a wchar_t* to a LPTSTR and when I do the conversion i only get the first character back from the original string.

PT
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 13869673
Ah, you mean the other way round? Then, that is

wchar_t* pwsz = L"Test";
char* psz = new char[wcslen(pwsz) + 1];

wcstombs(psz, pwst, wcslen(pwsz) + 1);
0
 

Author Comment

by:ptrennum
ID: 13869701
You're the man!

Thanks again

PT
0
 
LVL 86

Expert Comment

by:jkr
ID: 13869721
Thanx :o)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question