Convert LPWSTR to string

Hi i'm trying to convert LPWSTR but always i dont know but i complicate my life when i have a data like this. I know that LPWSTR is a long pointer to a constant string. I must to convert in this code to string the code is the follow:
1
2
3
LPWSTR pi = L"PAPUCHI";
std::string MyString = CW2A (pi);
LPSTR vi = MyString.c_str();

Last edited on
1
2
3
4
5
6
7
8
//Warning: This function loses data!
std::string to_string(LPWSTR *ws){
    std::string ret;
    ret.reserve(wcslen(ws));
    for (;*ws; ws++)
        ret += (char)*ws;
    return ret;
}

Note that instead of this:
1
2
3
LPWSTR pi = L"PAPUCHI";
std::string MyString = to_string(pi);
LPSTR vi = MyString.c_str();
it would be much better to do this:
 
LPSTR vi = "PAPUCHI";
Last edited on
Topic archived. No new replies allowed.