Quick question: How can I access the BN_CLICKED constant and other constants defined for the Win32 API from .NET? Are they defined in some library? Do I have to define them myself? If so, where can I find these values? And are the values version-specific between versions of Windows?
-
4No source is perfect. The web is infected with bad VB6 declarations, pinvoke.net is often ambiguous and misleading, pinvoke assistant is just ugly machine generated, the SDK requires master C skills. Only one nearly perfect one: stackoverflow.com – Hans Passant Feb 25 '11 at 21:28
5 Answers
I find the PInvoke Interop Assistant to be really helpful: http://blogs.microsoft.co.il/blogs/sasha/archive/2008/01/12/p-invoke-signature-generator.aspx.
It has almost everything and can convert the C++ to C#/VB for you. I rarely, if ever, resort to searching google/pinvoke.net anymore.
Here's the MSDN Magazine Article: http://msdn.microsoft.com/en-us/magazine/cc164193.aspx
The original January 2008 MSDN Magazine Article is now only available as a .CHM help file download, linked from the very bottom of https://msdn.microsoft.com/magazine/msdn-magazine-issues. (Column "CLR Inside Out: Marshaling between managed and unmanaged code.")
And here's the download: http://download.microsoft.com/download/f/2/7/f279e71e-efb0-4155-873d-5554a0608523/CLRInsideOut2008_01.exe. The source code can be found at http://clrinterop.codeplex.com/.

- 18,547
- 14
- 94
- 196

- 10,244
- 1
- 45
- 51
-
1This looks like a good tool to convert from the `.h` files (obtained using Uwe Keim's answer) to the C# P/Invoke signatures. – Justin Feb 25 '11 at 21:00
-
1You usually don't need them. Most of the constants and method signatures are included in its database. – Mark Sowul Feb 25 '11 at 21:01
You could download the Microsoft Platform SDK and take a look at the header files (*.h
). E.g. the BN_CLICKED
is defined in the winuser.h
file.
Usually, if you just need one or two constants, a Google search and a look at the first few results is also sufficient, since the value is printed there.

- 39,551
- 56
- 175
- 291
-
For WM_COMMAND, that was useful and standard. But I've found two different values for BN_CLICKED with google. – Squirrelsama Feb 25 '11 at 20:51
-
@Legatou No rule without exception ;-) Platform SDK headers are definitely the best choose in my opinion. – Uwe Keim Feb 25 '11 at 20:53
http://pinvoke.net/ is an excellent resource for this many common P/Invoke definitions.

- 6,611
- 3
- 36
- 57
-
1Really? I found that PInvoke.net is good for method signatures. I tried to find `BN_CLICKED` and did not succeed. – Uwe Keim Feb 25 '11 at 20:52
-
1@Uwe Keim - You're right -- I assumed it was a typo for `BM_CLICK`, but it looks like `BN_CLICKED` does exist. I couldn't find it on pinvoke.net either. – Justin Feb 25 '11 at 20:54
The MagNumDB website (by SO user Simon Mourier) is an easy way to look up constants:

- 14,557
- 12
- 70
- 91
It's kind of a proof-of-concept, but I put together a script that can look up most any Windows API constant. Example usage:
PS > .\Get-WindowsSDKConstant.ps1 BN_CLICKED
0
PS > .\Get-WindowsSDKConstant.ps1 BN_DBLCLK
5
PS > .\Get-WindowsSDKConstant.ps1 WM_COMMAND
273
It requires you to download Visual Studio and the Windows 10 SDK, because behind-the-scenes it compiles a program that looks up the constant.
Finally, here's some answers to the asker's questions:
Are [the constants] defined in some library?
The authoritative source is the Windows Platform SDK
Do I have to define them myself?
They're not built-in to Windows or .NET, which means you'll probably define them yourself (or copy them from somewhere).
And are the values version-specific between versions of Windows?
They're very stable, because otherwise a program compiled for one version of Windows might stop working when a user upgrades to a newer version of Windows. Microsoft goes to great lengths to prevent this from happening.
However, I've seen at least one place where the constants are different depending on what platform/architecture you're compiling on. I wouldn't assume that just because your code works on x86 64-bit Windows, it'll work on ARM 32-bit Windows RT, for example.

- 14,557
- 12
- 70
- 91