954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

LPWSTR to std::string help!

title is self explanatory how do i convert it to a std::string?
all this LP stuff makes my brain hurt...

twek
Newbie Poster
8 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
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
Team Colleague
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

twek
Newbie Poster
8 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 
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
 

oh there was one other thing,
so now i have a string [icode]windowName[/code] but every time i try to compare it or basiclay do anything with it the code breaks

if(windowName.find("Torrent 1.8.1")!=-1)
{
cout << windowName.c_str();
}

produces aWindows has triggered a breakpoint in sendMessageToProcess.exe.

This may be due to a corruption of the heap, which indicates a bug in sendMessageToProcess.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while sendMessageToProcess.exe has focus.

The output window may have more diagnostic information.

twek
Newbie Poster
8 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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
 

sorry about the lack of the end icode lol i spaced out but yeah,
um
so i rewrote the check loop

if(windowName.compare("")!=0)
	{
		if(windowName.find("Torrent 1.8.1")!=std::string::npos)
		{
			cout << processID<<":"<<windowName.c_str()<<endl;
		}
	}

u where right checking to make sure the string wasnt empty was what was giving me that error
as for cout <c:\documents and settings\tweak\my documents\visual studio 2008\projects\sendmessagetoprocess\sendmessagetoprocess\sendmessagetoprocess.cpp(50) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
1> c:\program files\microsoft visual studio 9.0\vc\include\ostream(653): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<>(std::basic_ostream<_Elem,_Traits> &,const char *)'

twek
Newbie Poster
8 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

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!!!

stiwari
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks ArkM, your are a genius!

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...

easysir
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You