user705414
user705414

Reputation: 21200

How to declare the following using PInvoke method?

For the simple Messagebox, checking the http://pinvoke.net/, I get the

[DllImport("user32.dll")]
static extern MessageBoxResult MessageBox(IntPtr hWnd, string text, string caption, int type);

However the compiler reports MessageBoxResult could not be found. If I change MessageBoxResult to int, then it compiles fine. Any hint about this problem?

Upvotes: 1

Views: 445

Answers (4)

ken2k
ken2k

Reputation: 48985

The definition is also given on pinvoke.net:

enter image description here

 /// <summary>
 /// Represents possible values returned by the MessageBox function.
 /// </summary>
 public enum MessageBoxResult : uint
 {
     Ok = 1,
     Cancel,
     Abort,
     Retry,
     Ignore,
     Yes,
     No,
     Close,
     Help,
     TryAgain,
     Continue,
     Timeout = 32000
 }

But as other mentioned, always check on MSDN that the value given by pinvoke.net is valid.

Upvotes: 2

ssube
ssube

Reputation: 48267

If you look at the MSDN page for the function, you'll see it does return an int, that's the native return type. I suspect pinvoke.net is glossing over that with the MessageBoxResult, although their page mentions in two places that the function returns an int (just under messageboxresult it says "uint 0-6" and later in the example they write the function as returning an int). I'd recommend just using an int and handling that (or converting to DialogResult if possible).

Upvotes: 0

Ignacio Soler Garcia
Ignacio Soler Garcia

Reputation: 21855

MessageBoxResult is defined here:

System.Windows.MessageBoxResult

Upvotes: 1

Stu
Stu

Reputation: 15769

Just add

using System.Windows;

Upvotes: 0

Related Questions