I'm importing WinApi functions, writing callbacks etc. (example) in C# and always wonder:
LRESULT
as last result ? W-PARAM ? L-PARAM ?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
?
In the days of 16-bit Windows, a WPARAM was a 16-bit word, while LPARAM was a 32-bit long. These distinctions went away in Win32; they both became 32-bit values. According to this, LPARAM is defined as LONG_PTR , which in 64-bit Windows is a signed, 64-bit value.
You used the W parameter to pass things like handles and integers. You used the L parameter to pass pointers. When Windows was converted to 32-bit, the WPARAM parameter grew to a 32-bit value as well. So even though the “W” stands for “word”, it isn't a word any more.
LPARAM is a typedef for LONG_PTR which is a long (signed 32-bit) on win32 and __int64 (signed 64-bit) on x86_64. WPARAM is a typedef for UINT_PTR which is an unsigned int (unsigned 32-bit) on win32 and unsigned __int64 (unsigned 64-bit) on x86_64.
LRESULT is an integer value that your program returns to Windows. It contains your program's response to a particular message. The meaning of this value depends on the message code. CALLBACK is the calling convention for the function.
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 :)
That names came from historical reasons. In the age of WIndows16 bit WPARAM meant Word-Parameter and LPARAM Long-Parameter in an Hungarian Notation sort-of. moving to 32 bit collapsed both to the same size (32bit integers) but left the names unchanged. LRESULT meant long result, and, again name is kept for historical reasons. Another change happen when windows64 bit came out. Please have a look here in MSDN to have a complete list. In details: both LPARAM and LRESULT are a typedef for LONG_PTR, where LONG_PTR is:
#if defined(_WIN64) typedef __int64 LONG_PTR; #else typedef long LONG_PTR; #endif
WPARAM is a typedef for UINT_PTR, where UINT_PTR is:
#if defined(_WIN64) typedef unsigned __int64 UINT_PTR; #else typedef unsigned int UINT_PTR; #endif
You can see basically the types eventually pointing to the same size bits: the only real difference is if you are using windows 32 or 64. In term of usage meaning, they are general pourpose parameters you can use depending on what the window procedure needs to do. Typically, since a couple of number is not enought, poiter to complex data structures are used and their values passed as WPARAM or LPARAM, so you cannot assign any particular meaning unless you focalize a context.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With