29

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 ?

Bitterblue
  • 13,162
  • 17
  • 86
  • 124
  • See this: http://stackoverflow.com/questions/2515261/what-are-the-definitions-for-lparam-and-wparam – Oscar Jul 25 '13 at 07:15
  • 2
    [Windows Data Types](http://msdn.microsoft.com/en-us/library/windows/desktop/aa383751.aspx) – GSerg Jul 25 '13 at 07:15
  • When you use this command `WindowProc`, you need to send a command to handler as `hwnd` with message as `uMsg`, and `LPARAM` & `RPARAM` is additional info/data, which could be used and depend from message you using. If you need to understand, what you have to send in that variable, check your `uMsg` type, that you need (for example: `WM_NOTIFY` http://msdn.microsoft.com/en-us/library/windows/desktop/bb775583(v=vs.85).aspx) – Maxim Zhukov Jul 25 '13 at 07:17

4 Answers4

96

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 :)

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
8

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.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
3

That's an example for hungarian notation:

  • L in both LPARAM and LRESULT means "long", designating a 32-bit int
  • w in WPARAM means "word" (which used to be 16-bit int but is now also a 32 bit int — at least when targeting a 32-bit architecture)

The remainder of the type names / aliases is supposed to hint at their meaning, i.e. LRESULT containing some kind of result value, LPARAM and WPARAM being used for parameter variables.

The actual meaning of the wParam and lParam parameters' content depends on the particular message being sent; they are just generic buckets for message parameters. So it's quite likely that you won't be able to circumvent unsafe type casts.

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • 2
    L meant "long" and W meant "word" but you should clarify that these days they are all 32-bit (or 64-bit depending on the platform). – Jonathan Potter Jul 25 '13 at 07:19
  • @JonathanPotter: Thanks for the correction, I was about to check. I believe the type sizes used to be different in the Win16 API, so I suppose the Hungarian prefixes in these names have a historical background. – stakx - no longer contributing Jul 25 '13 at 07:22
  • @stakx Yes, [you are correct](http://blogs.msdn.com/b/oldnewthing/archive/2003/11/25/55850.aspx). – GSerg Jul 25 '13 at 07:24
  • There's a lot of history about Hungarian notation and how this version became the version that everyone knows about and is not how the inventor intended the prefixing system to work. See http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Hungarian_notation.html – Skizz Jul 25 '13 at 08:07
3

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.

typedef UINT_PTR WPARAM;

typedef LONG_PTR LPARAM;

In c# You can Use IntPtr

See What are the definitions for LPARAM and WPARAM?

Community
  • 1
  • 1
jiten
  • 5,128
  • 4
  • 44
  • 73