C# 中 PInvoke 代码的使用
我有以下使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
而且
,没有函数“Console.WriteLn”。你需要
You have to have
Moreover, there is no function "Console.WriteLn". You need
您在代码开头缺少
using System.Runtime.InteropServices;
。You are missing a
using System.Runtime.InteropServices;
at the beginning of the code.将其放在文件顶部:
您的声明是错误的,这在 64 位操作系统上不起作用。从 pinvoke.net 网站获取合适的。还添加错误检查 SetLastError 属性。
Put this at the top of the file:
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.