C# DllImport 属性

[引]C# DllImport 属性

在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 User32.dll 库导入的 MessageBox 方法。

using System;
using System.Runtime.InteropServices;
class MainClass 
{
   [DllImport("User32.dll")]
   public static extern int MessageBox(int h, string m, string c, int type);

   static int Main() 
   {
      string myString; 
      Console.Write("Enter your message: ");
      myString = Console.ReadLine();
      return MessageBox(0, myString, "My Message Box", 0);
   }
}

extern 修饰符用于声明在外部实现的方法。

extern 修饰符的常见用法是在使用 Interop 服务调入非托管代码时与 DllImport 属性一起使用。

在这种情况下,还必须将方法声明为 static。

posted @ 2008-09-24 09:51  TONYBINLJ  阅读(366)  评论(0编辑  收藏  举报