Convert HWND to Hex String in C++

Viewed 2240

What is the best way to convert HWND to Hex String in C++, I mean also with a "0x" prefix?

HWND hWnd = FindWindow(L"Notepad", L"Untitled - Notepad");
MessageBox(nullptr, LPCWSTR(hWnd), L"Hello World!", MB_ICONINFORMATION | MB_OK | MB_DEFBUTTON1);

But I expect this to output 0x00000000 (assuming Notepad windows is not open) , but it always returns an empty string.

I also tried this answer, but I ended up with returning 0000000000000000.

Anyone can help me on that conversion?

3 Answers

This:

HWND hwnd = FindWindowW(NULL, L"Untitled - Notepad");
std::wostringstream ss;
ss << std::hex << hwnd;
std::wstring strTitle = ss.str();
MessageBoxW(NULL, L"Caption message", strTitle.c_str(), MB_OK);
Related