-2

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?

273K
  • 29,503
  • 10
  • 41
  • 64
Cesar
  • 41
  • 2
  • 5
  • 16
  • 5
    `std::hex` doesn't insert additional leading zeroes, so it should already give you `0x5D0512`. Something else must be going on. Is there any code you're not showing? Also, `title` is a pointer, it doesn't "store" anything. The pointer will be valid until `wstr` goes out of scope (or you call a non-const member function on it). – Etienne de Martel Aug 12 '22 at 00:57
  • @EtiennedeMartel take a look on how it looks into the debugger: https://i.imgur.com/AHd23FN.png, you can reproduce it by converting any data of type `HWND` with the code above. – Cesar Aug 12 '22 at 01:28
  • https://coliru.stacked-crooked.com/a/826fb0979e657a58 – Etienne de Martel Aug 12 '22 at 01:58
  • 1
    What does "convert a data of type HWND to LPCWSTR" mean? `HWND` is a window handle. `LPCWSTR` is a 16-bit unicode string. Can you explain, using your own words, the general process you expect to undertake that takes a handle for a window, and turns it into a Unicode string? How does that transformation work, in your mind? If a complete stranger walked off the street, how would you describe this process to them? This is like asking how to turn an apple into a bicycle. There is no meaningful, logical process for doing this, neither does exist one for that kind of a conversion. – Sam Varshavchik Aug 12 '22 at 02:09
  • @SamVarshavchik i only need to get the value of the hWnd and convert it to a string – Cesar Aug 12 '22 at 02:12
  • 1
    Well, you mostly have the code for this. The `std::hex` manipulator is the easiest way to do it. It won't provide a `"0x"` prefix, but so just add one yourself. As far as all that extra 0-padding goes the reason for that seems rather obvious -- the `<<` overload on a generic pointer -- so the workaround is the obvious one. Do you understand how the shown code works, every part of it? If yes, then the simple tweak, to this, is the easiest way to go. If not, you will find more information about stringstream, and its manipulators, in your C++ textbook. So, just do that, and call it a day. – Sam Varshavchik Aug 12 '22 at 02:16
  • 1
    Nah, it's not that, it's just some people believe a popular myth: how to write C++ code without learning core C++ fundamental concepts? Answer: run a Google search and copy/paste the results. This only works if the found code's functionality is 100% identical to what's needed. This doesn't work if the found code is junk code, but without learning core C++ fundamentals there's no way to tell if it's junk. And if it's not a 100% match, knowledge of core fundamentals is needed to change it accordingly, but a Google search won't help with this, now, by definition. – Sam Varshavchik Aug 12 '22 at 02:26
  • 2
    furthermore, converting HWND to LPCWSTR like that is an extremely wrong way to do. An address is internal to the process and you shouldn't pass it to other places in string form. Why waste so many bytes and effort to do that useless thing that the CPU can't use? This is exactly an [XY problem](https://meta.stackexchange.com/q/66377/230282). You need to explain what you really want to achieve – phuclv Aug 12 '22 at 02:43

1 Answers1

1

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;
selbie
  • 100,020
  • 15
  • 103
  • 173