从 C# 应用程序链接 C# DLL 中的函数
从 C# 应用程序链接到 C# DLL 中的函数的正确方法是什么?
这就是我的 DLL 的外观:
namespace WMIQuery_Namespace
{
public class WMIQuery
{
public static string GetPortName()
{
/* Does some stuff returning valid port name. */
return string.Empty;
}
public static int ScanForAdapters()
{
/* Does some stuff returning valid adapter index. */
return _index = -1;
}
}
}
这就是我的应用程序中的接口的外观:
namespace MyApp_Namespace
{
class MyApp_Class
{
internal static class WMIQuery
{
[DllImport("WMIQuery.dll")]
internal static extern string GetPortName();
[DllImport("WMIQuery.dll")]
internal static extern int ScanForAdapters();
}
}
}
一切都编译时没有任何错误或警告,但是当我尝试从我的应用程序调用这些函数时,会引发此异常:
“无法找到名为的入口点 DLL 中的“ScanForAdapters” 'WMIQuery.dll'。”
我做错了什么?我知道我可以添加 WMIQuery
作为参考,但我需要将其链接为非托管 DLL。谢谢。
What is the proper way to link to functions in a C# DLL from a C# application?
This is how my DLL looks:
namespace WMIQuery_Namespace
{
public class WMIQuery
{
public static string GetPortName()
{
/* Does some stuff returning valid port name. */
return string.Empty;
}
public static int ScanForAdapters()
{
/* Does some stuff returning valid adapter index. */
return _index = -1;
}
}
}
This is how the interface in my application looks:
namespace MyApp_Namespace
{
class MyApp_Class
{
internal static class WMIQuery
{
[DllImport("WMIQuery.dll")]
internal static extern string GetPortName();
[DllImport("WMIQuery.dll")]
internal static extern int ScanForAdapters();
}
}
}
Everything compiles without any errors or warnings, but when I try to call these functions from my application this exception is thrown:
"Unable to find an entry point named
'ScanForAdapters' in DLL
'WMIQuery.dll'."
What am I doing wrong? I am aware that I can add WMIQuery
as a reference, but I need to link it as an unmanaged DLL. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果 DLL 是 C# DLL,则无需使用 P/Invoke 来“链接”到它。
只需将其添加为对您的项目的引用,然后在您想要使用它的类的顶部添加:
然后您可以直接执行以下操作:
根本不需要在“客户端”端添加任何代码...
至于:
请注意,直到应用程序使用该程序集时才会加载该程序集。 CLR 中的程序集不会在启动时加载,而是在程序集中使用第一种类型时加载。无需尝试通过 P/Invoke 加载它来实现后期绑定。
如果您不相信这一点,您可以随时使用 Assembly .Load 和反射来调用成员,但平台调用服务不适用于加载 C# 程序集。
If the DLL is a C# DLL, you don't use P/Invoke to "link" to it.
Just add it as a reference to your project, and add, at the top of the class where you want to use it:
You can then directly do:
No need for any code in the "client" side at all...
As for:
Realize that the assembly will not be loaded until the point in time that your application uses it. Assemblies in the CLR are not loaded at startup, but rather at the time the first type is used from within the assembly. There is no need to try to load it via P/Invoke in order to achieve late binding.
If you don't trust this, you can always use Assembly.Load and reflection to call the members, but Platform Invocation services will not work for loading a C# assembly.
这能满足您的需求吗?
http://www.codeproject.com/KB/cs/csharpreflection.aspx
克里斯
Does this satisfy your needs?
http://www.codeproject.com/KB/cs/csharpreflection.aspx
Chris