What do LRESULT, WPARAM and LPARAM mean?

Bitterblue picture Bitterblue · Jul 25, 2013 · Viewed 15.5k times · Source

I'm importing WinApi functions, writing callbacks etc. (example) in C# and always wonder:

  • what do they mean ? LRESULT as last result ? W-PARAM ? L-PARAM ?
  • how to safely "wrap" them
    • WPARAM and LPARAM contain structs sometimes. So I need to use them as IntPtr. How about LRESULT ? Am I safe with int or better IntPtr ?

What type do I use for LRESULT in C# ? int or IntPtr ?

Answer

Hans Passant picture Hans Passant · Jul 25, 2013

enter image description here

That's Charles Simonyi, the former head of the Application Software group at Microsoft, the group that developed Word and Excel. He's the one that set identifier naming standards. Since nobody knows how to pronounce his last name, they picked the country he was born in and called it Hungarian notation. The Windows group adopted it as well, but picked the "bad" kind, System Hungarian. Where the first letter(s) of the identifier is chosen to note the type of the variable. As opposed to the "good" kind, Apps Hungarian, which selects the prefix by the logical type name instead of the physical type name. Simonyi's version.

So it is L as in Long, W as in Word. LPCWSTR is a doozy like that, Long Pointer to Constant Wide String. A clear problem with System Hungarian is that it doesn't work so well anymore when the architecture changes. Originally picked for 16-bit operating systems (L=32-bits, W=16-bits), migrated to 32-bit without changing the name (W=32-bits), we're at 64-bit today (L=W=64-bits).

So ignore these prefixes, they're just an historical accident. You really must pick IntPtr for the LRESULT type, it certainly can be a 64-bit value on the 64-bit version of Windows. Very hard to diagnose problems occur when you don't, a common question here.

Off topic, the fuzzy image you see in the background of the photograph is an interesting tidbit about Simonyi as well. Microsoft shared its great success with its employees and turned many of them into multi-millionaires. What you see in the background is a shot of the space shuttle docked to the International Space Station. Simonyi is one of the seven "space tourists" and bought himself a ticket to ISS. The only one to do so twice, set him back $60 million :)