tengfeidx的个人空间 https://bbs.21ic.com/?670183 [收藏] [复制] [RSS]

日志

extern C# DLL

已有 223 次阅读2017-3-25 09:53 |系统分类:兴趣爱好

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

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

在这种情况下,还必须将方法声明为 static,如下示例所示:

[DllImport("avifil32.dll")]
private static extern void AVIFileInit();

在该示例中,程序接收来自用户的字符串并将该字符串显示在消息框中。程序使用从 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);
   }
}//以上摘自MSDN    上述程序用csc  编译就可执行如 : csc /out:test.exe "test.cs"      test.exe

路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)