-3

I would like to know how to get the currenty logged on user's name as a wstring.

I only found LPWSTR examples like that:

#include <iostream>
#include <windows.h>
#include <Lmcons.h>
using namespace std;

int main()

{
    wchar_t name[UNLEN+1];
    DWORD size = UNLEN + 1;

    if (GetUserNameW( (LPWSTR)name, &size ))
    {
        cout << "Hello, " << name << "!\n";
    }
    else
    {
        cout << "Hello, unnamed person!\n";
    }
}
return 0;

}

Can anybody tell me how to convert this to a wstring?

Thank you.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
tmighty
  • 10,734
  • 21
  • 104
  • 218

1 Answers1

3

You can just use the constructor for std::wstring on the value in name after calling GetUserNameW:

if(GetUserNameW(name, &len))
{
    std::wstring strname(name); 
    std::wcout << L"Hello, " << strname << std::endl;
}

If you're asking in general about why the Windows API doesn't support C++'s std:: stuff out of the box, it's because it's designed to be compatibile with C first and foremost.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85