How do I send floats in window messages.
- by yngvedh
Hi,
What is the best way to send a float in a windows message using c++ casting operators?
The reason I ask is that the approach which first occurred to me did not work. For the record I'm using the standard win32 function to send messages:
PostWindowMessage(UINT nMsg, WPARAM wParam, LPARAM lParam)
What does not work:
Using static_cast<WPARAM>() does not work since WPARAM is typedef'ed to UINT_PTR and will do a numeric conversion from float to int, effectively truncating the value of the float.
Using reinterpret_cast<WPARAM>() does not work since it is meant for use with pointers and fails with a compilation error.
I can think of two workarounds at the moment:
Using reinterpret_cast in conjunction with the address of operator:
float f = 42.0f;
::PostWindowMessage(WM_SOME_MESSAGE, *reinterpret_cast<WPARAM*>(&f), 0);
Using an union:
union { WPARAM wParam, float f };
f = 42.0f;
::PostWindowMessage(WM_SOME_MESSAGE, wParam, 0);
Which of these are preffered? Are there any other more elegant way of accomplishing this?