?
Solved

Converting char array into LPCWSTR

Posted on 2003-03-28
11
Medium Priority
?
8,374 Views
Last Modified: 2008-07-15
I have a char array, id[15], that I read in from the user's input on the console using cin >> id, and I need to change this into a LPCWSTR.  I need the cin to go into the char array because I am using the system() command to run the net use command, but I cannot figure out how to change the char into a LPCWSTR.
0
Comment
Question by:batman900
[X]
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
  • 4
  • 3
  • 3
  • +1
11 Comments
 
LVL 1

Expert Comment

by:hsdhina
ID: 8228327
Hi,
 I hope the following website will be useful for you.

http://msdn.microsoft.com/library/default.asp?
url=/library/en-us/wmisdk/wmi/chstring.asp
0
 
LVL 12

Expert Comment

by:Salte
ID: 8228328
Try mbstowcs() function.

LPCWSTR is a constant pointer to wchar_t string, so LPCWSTR is just microsoft's way of saying:

const wchar_t *

and wchar_t * is exactly what mbstowcs gives you.

Alf
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8229458
you could try:

#include <string>

string command;


command = id;

system (command);

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Salte
ID: 8229941
Why do you need LPCWSTR for system()? system() takes regular char string (LPCSTR maybe) and not LPCWSTR as far as I know.

Alf
0
 
LVL 1

Author Comment

by:batman900
ID: 8231765
I need to convert to a LPCWSTR because the netUserChangePassword uses them.  I am writing a program that connects to a remote server and then makes the user change his password, and the only way I could find to do this was the netUserChangePassword command.
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8232546
Feed it the regular string and cast it to a LPCWSTR
0
 
LVL 1

Author Comment

by:batman900
ID: 8239912
ged325 if i feed it a string, idst, and cast it via (LPCWSTR)idst I get an error saying "Cannot convert from std::string to LPCWSTR"
0
 
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 8243408
what about a

strcpy (myLPC, mystring);
0
 
LVL 12

Accepted Solution

by:
Salte earned 200 total points
ID: 8244000
ged325,

you CANNOT just feed it a regular string and cast it to LPCWSTR. LPCWSTR means 'const wchar_t *' string and simply isn't the same as 'const char *'.

However, the function mbstowcs() does convert between the two as I already said.

batman900,

why don't you try mbstowcs(), it does what you want. It copies a const char * string and stores the equivalent string in a wchar_t buffer. Then you can feed that buffer as argument to your function. The cast from wchar_t * to const wchar_t * is simply a const cast and is done automatically by C++ you don't have to worry about it.

Alf
0
 
LVL 1

Author Comment

by:batman900
ID: 8245983
Thank you, sorry I was having some trouble with the mbstowcs() command for a bit.  It worked flawlessly, thank you very much.
0
 
LVL 12

Expert Comment

by:Salte
ID: 8246275
Perhaps because mbstowcs consider your locale and you perhaps have to make sure you use the right locale before calling it? The default "C" locale can do some but might not be sufficient.

Alf
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

765 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