title is self explanatory how do i convert it to a std::string?
all this LP stuff makes my brain hurt...
try static_cast(the_LPWSTR_var);
or if it doesn't work
reinterpret_cast(the_LPWSTR_var);
minas1
Junior Poster in Training
82 posts since Nov 2008
Reputation Points: 13
Solved Threads: 8
If your program is being compiled for UNICODE and the string is non-English, then more than likely it can not be converted. If English, then there are conversion functions.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
bool cvtLPW2stdstring(std::string& s, const LPWSTR pw,
UINT codepage = CP_ACP)
{
bool res = false;
char* p = 0;
int bsz;
bsz = WideCharToMultiByte(codepage,
0,
pw,-1,
0,0,
0,0);
if (bsz > 0) {
p = new char[bsz];
int rc = WideCharToMultiByte(codepage,
0,
pw,-1,
p,bsz,
0,0);
if (rc != 0) {
p[bsz-1] = 0;
s = p;
res = true;
}
}
delete [] p;
return res;
}
int main()
{
wchar_t msg[] = L"Hello, world!";
std::string s("\?");
cvtLPW2stdstring(s,msg);
std::cout << s << std::endl;
return 0;
}
It's possible to add more parameters check ups...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
wow ArkM that function is like sent from god himself lol it worked fantastic, is there anywhere i can learn about all these long pointer string and stuff cuz they just dont make any sense to me and there used alot in windows programing
Well, God talks with us via other people...
I was glad to help you.
The best knowledge base? MSDN, of course...
Don't forget to set a proper (national) code page.
Good luck!
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
1. Use icode (not code) tag for inline codes.
2. Strictly speaking, bad position value of sttring::find is
const size_t badpos = std::string::npos;
3. Use string output directly, w/o c_str():
cout << windowName;
There is overloaded operator << for std::string.
4. Probably, the memory is corrupted and windowName value is not a valid std::string value. Why? It's the other question, the error context needed...
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
Hi ArkM, can you tell me what exactly you are trying to achieve in this step:
p[bsz-1] = 0;
Is it correct or was it supposed to be
p[bsz-1] = '\0';
Thanks in advance for your reply!!!
Both will do the same thing. The integral value of '\0' is 0.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401