Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


converting CString to LPWSTR?

Posted on 2000-04-15
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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.
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


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 200 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

This article shows how to make a Windows 7 gadget that extends its U/I with a flyout panel -- a window that pops out next to the gadget.  The example gadget shows several additional techniques:  How to automatically resize a gadget or flyout panel t…
In this article, I will show how to use the Ribbon IDs Tool Window to assign the built-in Office icons to a ribbon button.  This tool will help us to find the OfficeImageId that corresponds to our desired built-in Office icon. The tool is part of…
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.…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

670 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