Home > Blockchain >  How to convert HWND to LPCWSTR
How to convert HWND to LPCWSTR

Time:08-12

While searching for how to convert a data of type HWND to LPCWSTR

I found this sample:

std::wostringstream ss;
ss << std::hex << hWnd;
std::wstring wstr = ss.str();
LPCWSTR title = wstr.c_str();

However title is storing the data as: 00000000005D0512

How i could get it in this format: 0x5D0512?

CodePudding user response:

Exactly as you have it, but with a cast to uintptr_t. And manually do the 0x prefix as well into the stream.

ss << L"0x" << std::hex << (uintptr_t)hWnd;

CodePudding user response:

    #include <wchar.h>
    
    const int i { std::stoi(title, nullptr, 16) };
    wchar_t *buf = new wchar_t[9];
    _snwprintf(buf, 9, L"%#X", i);
    LPCWSTR _wZTR = std::wstring(buf).c_str(); //convert to LPCWSTR & -732K
    LocalFree(buf); //bros recommended

  • Related