Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Casting LPCWSTR to WCHAR 1

Status
Not open for further replies.

lupidus

MIS
Jun 1, 2003
30
US
It has been a few years since I've worked with C++, so I'm having some difficulty with these casts.

The goal of this code is to pass lpPassword into CreateProcessWithLogonW successfully. The CreateProcessWithLogonW function takes LPCWSTR lpPassword as a parameter. However, I'm dealing with a std::string initially and need to get it to a LPCWSTR.


So far, I have :

Code:
std::string input="encrypted_password_text.."
std::string output; // decrypted password

Encryptor * e = NULL;
e = new Encryptor();

e->DecryptString(input, output);
const char* lpPasswordTmp = output.c_str();
const WCHAR * lpPassword = (LPCWSTR) lpPasswordTmp;

printf("%s", lpPassword); // decrypted password

delete e;
e = NULL;

When I printf, the all the password text comes out as it should, but when I pass the value to the Logon function, the logon fails. If I simply pass the Logon function the following then it works:

const WCHAR *lpPassword = L"password";

I think I need some way to tell the compiler that I'm dealing with a long pointer, but not sure how?

Thanks.
 
>I think I need some way to tell the compiler that I'm dealing with a long pointer, but not sure how?

No, you need to realize that char != WCHAR and that there is no string casting in c++.

std::string handles char based strings.
std::wstring handles w_char based strings.

Fortunately, transforming from std::string to std::wstring is easy.
Code:
std::string s("Foo");
std::wstring w;
// Transform s => w
std::string::const_iterator it = s.begin();
while(it!=s.end()) w += *it++;
const WCHAR * whatever = w.c_str();


/Per
[sub]
www.perfnurt.se[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top