C# 中 PInvoke 代码的使用

发布于 2024-10-17 03:36:42 字数 1366 浏览 5 评论 0原文

我有以下使用 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"));
    }
  }
}

当我简单地运行 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?)

怎么了?选项中可以添加什么?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

表情可笑 2024-10-24 03:36:42

而且

using System.Runtime.InteropServices;

,没有函数“Console.WriteLn”。你需要

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

You have to have

using System.Runtime.InteropServices;

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

Console.WriteLine(IsDllRegistered("msdia100.dll"));
毅然前行 2024-10-24 03:36:42

您在代码开头缺少 using System.Runtime.InteropServices;

You are missing a using System.Runtime.InteropServices; at the beginning of the code.

情泪▽动烟 2024-10-24 03:36:42

将其放在文件顶部:

 using System.Runtime.InteropServices;

您的声明是错误的,这在 64 位操作系统上不起作用。从 pinvoke.net 网站获取合适的。还添加错误检查 SetLastError 属性。

Put this at the top of the file:

 using System.Runtime.InteropServices;

Your declarations are wrong, this won't work on a 64-bit operating system. Get the right ones from the pinvoke.net website. Also add error checking, SetLastError property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文