什么是DLL导入属性?

时间:2013-09-23 21:09:33

标签: c#

using System;
System.Runtime.InteropServices;
class Beeper
{
      DllImport("kernel32.dll")]//low level beep
      public static extern bool Beep(int frequency,int duration);
      static void Main()
      {
         Beep(1000,111);
      }
}

什么是DLL导入属性,它究竟做了什么?

1 个答案:

答案 0 :(得分:3)

这是一个像任何其他指定DLL名称的字符串。你需要引用它。

[DllImport("kernel32.dll")]

另外,这个:

System.Runtime.InteropServices.DllImportAttribute;

应该是:

using System.Runtime.InteropServices;

为什么不使用Console.Beep


无论如何,System.Runtime.InteropServices.DllImport attribute是一个属性,你可以放在空(extern)方法上,使它们引用一个带有引用DLL中签名的方法。