Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

converting CString to LPWSTR?

Posted on 2000-04-15
8
Medium Priority
?
2,245 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
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
 
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 200 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

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.

Question has a verified solution.

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

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 describes how to add a user-defined command button to the Windows 7 Explorer toolbar.  In the previous article (http://www.experts-exchange.com/A_2172.html), we saw how to put the Delete button back there where it belongs.  "Delete" is …
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…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

927 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