converting CString to LPWSTR?

Posted on 2000-04-15
Last Modified: 2013-12-03
I'm in the process of writing an application to create multiple users on an NT server. I'm using the Net Functions (NetUserAdd..etc) and cannot seem to convert a CString parameter (taken at the edit box) and convert it to a LPWSTR. There is prob. an easy way of doin this.... help!


PS. needed for final year project... 3 days overdue already!
Question by:astrix_1

Expert Comment

ID: 2719070
// here you go

CString strYourASCIIstring;
WCHAR thisisaunicodestring[30]; // size it as you want

swprintf("%S", strYourASCIIstring);

You can then call your Unicode-demanding function with the WCHAR array you've just created.

Expert Comment

ID: 2719072
Oops. Make it:

swprintf(thisisaunicodestring, L"%S", strYourASCIIstring);

LVL 22

Expert Comment

ID: 2719198
Does that work?  

An alternative is to use the MultiByteToWideChar() function.  It was written to do this type of conversion.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.


Expert Comment

ID: 2719201
It should work. I wrote it from memory but I'm pretty sure it *does* work. [If I was 100% sure I would have answered the question]
LVL 22

Expert Comment

ID: 2719211
My concern woud be, A) does swprintf() expect a byte character string (tecnically a multi-byte character strng) to coorespond to %s, I woudl have thought it would expect a wide character string.  but I don't know. and B) does passing a CString like that work?  Or do you need to convert it to a string pointer using operator LPCSTR().

Expert Comment

ID: 2719226
A) From past experience with Windows CE (Unicode-based) programming, I would go with %S instead of %s with swprintf - but then again, trying the piece of code should prove or disprove me.

B) The LPCTSTR() macro is implicit in that call, if I correctly recall - but it wouldn't hurt to include it.

Basically, I just don't have the time to try this out, but I assume astrix_1 will do and tell us the results.
LVL 23

Accepted Solution

chensu earned 50 total points
ID: 2719297
Use the T2W or T2OLE macros. See

TN059: Using MFC MBCS/Unicode Conversion Macros

#include <afxpriv.h>


CString str;

LPWSTR lpwStr = T2W(str);

Author Comment

ID: 2720409
I've tried both 'MultiByteToWideChar()' function and 'T2W' macro and found they both work well. I've decided to go with the 'T2W' option. Thanks very much for the help, it was really Appreciated!


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

839 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