How do I send floats in window messages.
Posted
by yngvedh
on Stack Overflow
See other posts from Stack Overflow
or by yngvedh
Published on 2010-03-23T09:33:58Z
Indexed on
2010/03/23
9:43 UTC
Read the original article
Hit count: 248
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 sinceWPARAM
is typedef'ed toUINT_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?
© Stack Overflow or respective owner