views:

263

answers:

1

Let us pick the following Win API call as an example:

BOOL MessageBeep(UINT uType); // from User32.dll

The input parameter is UINT to specify the beep type, which is can be both 32bit and 64bit integer, depending on which Windows version we call it on (or am I wrong?).

If I want to P/Invoke message beep from C#, so I apply the DllImport declaration:

[DllImport("User32.dll")]
static extern Boolean MessageBeep(UInt32 beepType);

Will this code C# work under Windows x64?

  • If yes, why is it OK to invoke the call a 32bit integer, while a 64bit integer is expected (assuming that I am correct on this)?
  • If no, how should the proper platform agnostic P/Invoke declaration look like in C#?
+5  A: 

You are wrong.

However, for types like HANDLE that do vary with bitness, you should use IntPtr.

If you're uncertain about a P/Inoke declaration, the first place to check is here.

SLaks
Why is DWORD bitness dependent, and UINT not? It seems contrary to the C data type docs I read. Windows Data Types page on MSDN:http://msdn.microsoft.com/en-us/library/aa383751(VS.85).aspxDWORD: typedef unsigned long DWORD; UINT: typedef unsigned int UINT;
You're right; I meant `HANDLE`.
SLaks
To extend your answer, I found the following MSDN recommendation on 32/64bit safe coding:http://msdn.microsoft.com/en-us/library/aa384242(VS.85).aspx