Click to See Complete Forum and Search --> : What is actually LPARAM, WPARAM & LRESULT in detail?


Irwan
July 19th, 2010, 08:51 AM
typedef UINT_PTR WPARAM;
typedef LONG_PTR LPARAM;
typedef LONG_PTR LRESULT;



What actually there are?

LPARAM & LRESULT. Can i cast of point them to a class object?


For example,
in a WndProc, and inside a switch case (maybe WM_CREATE), can I cast LRESULT to point to a RECT myrect and use it as return tp the WndProc


LRESULT CALLBACK WndProc (...)
{

.....
swith(Msg){
case WM_CREATE:
.....
return lresult;
break;
....
}
return ::DefWindowProc(hwnd, iMsg, wParam, lParam);
}





then in somehere else WndProc (a child window), i can do this to retrieve the pointer to the myrect such as

Rect rect = SendMessage(hwndparent, WM_CREATE, null, null);



It is possible? How do i cast it?

Igor Vartanov
July 19th, 2010, 10:18 AM
What actually there are?Just numbers of certain size.
LPARAM & LRESULT. Can i cast of point them to a class object?
It is possible?Yes. But you should take special care about referenced object: its life cycle must allow you to access it in message handler. Until it gets handled you must not do anything that results in object deletion/relocation.
How do i cast it?Any C++ compliant way you do. Typically it happens by taking object address and casting it to corresponding xPARAM.

Irwan
July 19th, 2010, 10:24 AM
life cycle, you are right about it. I been put in my note from the beginning. Kind of dangerous if I miss it.


Can you show some example mate. Can I return a lresult (which point to a, say, a RECT) for the wndproc().

Igor Vartanov
July 19th, 2010, 10:33 AM
// you can do this
RECT g_myRect = {0}; // of global scope

LRESULT CALLBACK WndProc (...)
{

.....
swith(Msg){
case WM_CREATE:
.....
lresult = reinterpret_cast<LRESULT>(&g_myRect);
return lresult;
....
}
return ::DefWindowProc(hwnd, iMsg, wParam, lParam);
}

// you cannot do that
LRESULT CALLBACK WndProc (...)
{

.....
swith(Msg){
case WM_CREATE:
.....
RECT myRect = {0}; // of function scope, to be destroyed on function return!!!
.....
lresult = reinterpret_cast<LRESULT>(&myRect);
return lresult;
....
}
return ::DefWindowProc(hwnd, iMsg, wParam, lParam);
}

Igor Vartanov
July 19th, 2010, 10:41 AM
BTW, WM_CREATE (http://msdn.microsoft.com/en-us/library/ms632619%28VS.85%29.aspx) is very rigid in aspect of returning LRESULT values. :)

Irwan
July 19th, 2010, 10:55 AM
Hahahah. Just for example. Anyways, that clear me up and now I know why MS have wparam and lparam as one for numeric data and one can be cast to point to any object.

Learn one more thing today. Thanks Mate. Really, thanks. I been sitting here from 5.30pm after work today and now its 12am in Malaysia. But it really a rewarding day, today. coding come to several milestone, and then, you. Thanks