在C#中的PInvoke码使用 [英] PInvoke code usage in C#

查看:398
本文介绍了在C#中的PInvoke码使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用的DllImport下面的C#代码

I have the following C# code that uses DLLImport.

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"));
    }
  }
}

当我只需运行 CSC CSCODE.cs 我得到了错误。

When I simply run csc CSCODE.cs I got the errors.

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?)

什么是错误?什么可能的选项被添加?

What's wrong? What might be added in options?

推荐答案

您必须有

using System.Runtime.InteropServices;



此外,不存在功能Console.WriteLn。您需要

Moreover, there is no function "Console.WriteLn". You need

Console.WriteLine(IsDllRegistered("msdia100.dll"));

这篇关于在C#中的PInvoke码使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆