[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2263
  • Last Modified:

converting CString to LPWSTR?

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
astrix_1
Asked:
astrix_1
1 Solution
 
bbousquetCommented:
// 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
 
bbousquetCommented:
Oops. Make it:

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

;-)
0
 
nietodCommented:
Does that work?  

An alternative is to use the MultiByteToWideChar() function.  It was written to do this type of conversion.
0
Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

 
bbousquetCommented:
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
 
nietodCommented:
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
 
bbousquetCommented:
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
 
chensuCommented:
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
 
astrix_1Author Commented:
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now