Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WPARAM and LPARAM to int

1,553 views
Skip to first unread message

Piranha

unread,
Oct 13, 2009, 12:56:35 PM10/13/09
to
I´ve defined an own message

const UINT MyMessage = RegisterWindowMessage(L"SomeUniqueText");

Now I´m trying to send messages, using WPARAM and LPARAM, nothing
sophisticated, no pointers, just plain small values.

DWORD a = 123;
int x = 17;
int y = 35;
LPARAM b = MAKELPARAM(x,y);
SendMessage(SomeWindow,MyMessage,a,b);

So far, that´s all working fine, my problem is, submitting negative
values.
It works fine using

DWORD a = -1;
SendMessage(SomeWindow,MyMessage,a,0);

At the receiving window I get

(int)wParam = -1

But if I want to use two values per parameter

int x = -1;
int y = 0;
WPARAM a = MAKEWPARAM(x,y);
SendMessage(SomeWindow,MyMessage,a,0);

the receiving window gets

(int)LOWORD(wParam) = 65535 instead of -1

Obviously, somewhere on the way my value gets converted to UINT, but I
can´t find any hint at MSDN under MAKEWPARAM or LOWORD, why this would
happen.
Could anyone clear this up for me?
And how could I do this correct?

ScottMcP [MVP]

unread,
Oct 13, 2009, 4:47:30 PM10/13/09
to

You have done nothing wrong. It is inherent in the twos complement
binary number system used by the hardware. -1 is represented as
0xFFFF, which is 65535.


Piranha

unread,
Oct 13, 2009, 7:21:30 PM10/13/09
to
> 0xFFFF, which is 65535.- Zitierten Text ausblenden -
>
> - Zitierten Text anzeigen -

Thanks for the hint, yet the problem also occurs when I send any other
nagative value.

-101 arrives as 65425

Either way, is there a solution to it, other than restricting messages
to one value per parameter?

ScottMcP [MVP]

unread,
Oct 13, 2009, 10:19:19 PM10/13/09
to
On Oct 13, 7:21 pm, Piranha <eu_pira...@gmx.net> wrote:
> Either way, is there a solution to it, other than restricting messages
> to one value per parameter?

Just take it apart into the two elements, assign them to integers:

int x = LOWORD(lParam);
int y = HIWORD(lParam);

Uwe Sieber

unread,
Oct 14, 2009, 3:03:55 AM10/14/09
to
Piranha wrote:
>
> But if I want to use two values per parameter
>
> int x = -1;
> int y = 0;
> WPARAM a = MAKEWPARAM(x,y);
> SendMessage(SomeWindow,MyMessage,a,0);
>
> the receiving window gets
>
> (int)LOWORD(wParam) = 65535 instead of -1

An int has the sign bit at bit 31. Just tell
the compiler that he has to expect it at bit 15
by casting to short which is 16 bit.

int x = (short)LOWORD(wParam)

The problem starts at the point where you put
two ints into one DWORD using the MAKEWPARAM
macro.
Its better style to do this with two shorts
because they always fit while the ints loose
half their bits. This way you get a better
feeling for the fact that only values from
-32768 to 32767 will survive the transport.


Uwe

Piranha

unread,
Oct 14, 2009, 7:29:57 AM10/14/09
to

Thank you very much, I´ll keep that in mind.

0 new messages