Casting pointers to/from LRESULT

568 views Asked by At

I'm looking for confirmation on this windows programming idiom, am I correct in thinking that many different types of "handles" are passed around as not only LRESULT objects but also lParam and wParam objects?

I'm guessing that as long as we know "what" type of handle is in LRESULT or lParam/wParam we can cast back into it.

For example

case WM_CREATE:
   ...
   //create a window
   //lParam is the CREATESTRUCT for new window created here
   ....
   return lParam;
...
...
CREATESTRUCT cStruct = (CREATESTRUCT)SendMessage(hwnd, msg /*WM_CREATE*/);
cStrcut.cx;//this is the width of the new window?

correct?

Is this is "correct"? Can anyone provide me and the StaticOverflow community a short thesis on this technique/idiom?

Questions: Should we only return lParam (or only wParam) values? Are there any pitfalls one should know about? Both LRESULT and LPARAM are LONG_PTR types, which are 32 or 64 bit integers. I'm not a seasoned C programmer, but it appears these integers are just being used as "buffers" which the programmer later casts into their "real" type before using... sound accurate?

1

There are 1 answers

4
Moo-Juice On

This style of programming is "bad" as there is no way to know what something really is at compile-time. That said, as the WIN32 API is a C API (instead of say, C++), there's not much else that can be done when one wants to pass around any type of struct using the same method signature.

So, the API design team decided to have two parameters to handle this. A 32-bit LPARAM and a 16-bit WPARAM.

When working with the Win32 C API, you're just going to have to make sure you read the documentation correctly and ensure you're casting to the right thing that the documentation says it is.

So to answer your question, you are correct - but only because you have no other choice. In an attempt to be complete here, MFC came along (for C++ programmers), but that isn't considered a good object-orientated library - it's more of a wrapper. There are others out there that do a much better job (e.g. wxWidgets).