Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

Converting char array into LPCWSTR

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
batman900
Asked:
batman900
  • 4
  • 3
  • 3
  • +1
1 Solution
 
hsdhinaCommented:
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
 
SalteCommented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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!

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

Alf
0
 
batman900Author Commented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Feed it the regular string and cast it to a LPCWSTR
0
 
batman900Author Commented:
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
 
Kyle AbrahamsSenior .Net DeveloperCommented:
what about a

strcpy (myLPC, mystring);
0
 
SalteCommented:
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
 
batman900Author Commented:
Thank you, sorry I was having some trouble with the mbstowcs() command for a bit.  It worked flawlessly, thank you very much.
0
 
SalteCommented:
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now