5

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?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Squirrelsama
  • 5,480
  • 4
  • 28
  • 38
  • 4
    No 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 Answers5

14

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. Screenshot

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/.

ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
Mark Sowul
  • 10,244
  • 1
  • 45
  • 51
  • 1
    This 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
  • 1
    You 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
6

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.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
4

http://pinvoke.net/ is an excellent resource for this many common P/Invoke definitions.

Justin
  • 6,611
  • 3
  • 36
  • 57
  • 1
    Really? 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
1

The MagNumDB website (by SO user Simon Mourier) is an easy way to look up constants:

http://www.magnumdb.com/search?q=BN_CLICKED

enter image description here

Michael Kropat
  • 14,557
  • 12
  • 70
  • 91
0

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.

Michael Kropat
  • 14,557
  • 12
  • 70
  • 91