C#中的PInvoke代码用法

pro*_*eek 2 c# pinvoke dllimport

我有以下使用DLLImport的C#代码.

using System;

namespace LvFpga {
  class RegTest 
  {

    [DllImport("kernel32")]    
    public extern static int LoadLibrary(string lpLibFileName);
    [DllImport("kernel32")]    
    public extern static bool FreeLibrary(int hLibModule); 

    public static bool IsDllRegistered(string DllName)    
    {    
      int libId = LoadLibrary(DllName);
      if (libId>0) FreeLibrary(libId);
      return (libId>0);    
    }
    public static void Main(string[] args)
    {
        Console.WriteLn(IsDllRegistered("msdia100.dll"));
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

当我运行时,csc CSCODE.cs我得到了错误.

regtest.cs(7,6): error CS0246: The type or namespace name 'DllImport' could not be found (are you
        missing a using directive or an assembly reference?)
regtest.cs(7,6): error CS0246: The type or namespace name 'DllImportAttribute' could not be found
        (are you missing a using directive or an assembly reference?)
regtest.cs(9,6): error CS0246: The type or namespace name 'DllImport' could not be found (are you
        missing a using directive or an assembly reference?)
regtest.cs(9,6): error CS0246: The type or namespace name 'DllImportAttribute' could not be found
        (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)

怎么了?可能会在选项中添加什么?

irr*_*ate 7

你必须有

using System.Runtime.InteropServices;
Run Code Online (Sandbox Code Playgroud)

而且,没有"Console.WriteLn"功能.你需要

Console.WriteLine(IsDllRegistered("msdia100.dll"));
Run Code Online (Sandbox Code Playgroud)