Solved

converting CString to LPWSTR?

Posted on 2000-04-15
8
2,192 Views
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!

Ricky

PS. needed for final year project... 3 days overdue already!
0
Comment
Question by:astrix_1
8 Comments
 
LVL 2

Expert Comment

by:bbousquet
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.
0
 
LVL 2

Expert Comment

by:bbousquet
ID: 2719072
Oops. Make it:

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

;-)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2719198
Does that work?  

An alternative is to use the MultiByteToWideChar() function.  It was written to do this type of conversion.
0
 
LVL 2

Expert Comment

by:bbousquet
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]
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:nietod
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().
0
 
LVL 2

Expert Comment

by:bbousquet
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.
0
 
LVL 23

Accepted Solution

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

TN059: Using MFC MBCS/Unicode Conversion Macros
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/visualc/vcmfc/_mfcnotes_tn059.htm


#include <afxpriv.h>

USES_CONVERSION;

CString str;

LPWSTR lpwStr = T2W(str);
0
 

Author Comment

by:astrix_1
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!

Astrix_1
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This tutorial is about how to put some of your C++ program's functionality into a standard DLL, and how to make working with the EXE and the DLL simple and seamless.   We'll be using Microsoft Visual Studio 2008 and we will cut out the noise; that i…
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now