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

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

try static_cast<std::string>(the_LPWSTR_var);

or if it doesn't work


reinterpret_cast<std::string>(the_LPWSTR_var);

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.

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

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

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!

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 a

Windows 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.[/quote]

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

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 <<std::string;
yeah idk why but for some reason its not working for me
like it does usualy but in this program i keep getting

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 <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.