Unmanaged code: calling win32 api from .Net

The first and second shots show the happy path. The third shot shows the effect of willfully misspelling MessageBox. You don’t get a compile time error because it is after all unmanaged code that .Net is accessing.

Useful site here

 

using System;
using System.Runtime.InteropServices;
namespace cli01 {
 internal class Program {
enum Options {
 OptionOk = 0,
 OptionOkCancel,
 OptionAbortRetryIgnore,
 OptionYesNoCancel,
 OptionYesNo,
 OptionRetryCancel,
 OptionCancelTryagainContinue
 }
[DllImport("user32.dll")]
 private static extern int MessageBox(IntPtr hWnd, string boxContent, string boxCaption, Options options);
private static void Main() {
 MessageBox(IntPtr.Zero, "The message in the box", "The caption", Options.OptionOkCancel);
 }
 }
}


The MSDN link on MessageBox. Look to be some errors in there suggesting it doesn’t get much love.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s