什么是 DLL 导入属性

本文关键字:属性 导入 DLL 什么 | 更新日期: 2023-09-27 18:32:33

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导入属性,它到底有什么作用?

什么是 DLL 导入属性

它是一个

字符串,就像任何其他指定 DLL 名称的字符串一样。你需要引用它。

[DllImport("kernel32.dll")]

另外,这个:

System.Runtime.InteropServices.DllImportAttribute;

应该是:

using System.Runtime.InteropServices;

为什么不直接使用Console.Beep


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