C# DLLImport for C++动态链接库
我有一个用 C++/CLI 和 .NET 构建的 .dll。因此,它是针对 .NET 应用程序的。该 API 是一组使用托管类型的包装器,因此它是 NET 原生的。我导入了 .dll 并添加了一个函数,如下所示:
[DllImport(@"maplib.dll")]
public static extern bool initialize(string a);
当我在 C# 代码中调用此函数时,它工作正常,但如果我想添加另一个函数,例如......,
[DllImport(@"maplib.dll")]
public static extern bool initialize(string a);
public static extern bool runfile(string a, string b);
我在运行程序时会收到此错误。它与第二个函数相关:
“无法从程序集“myapp,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null”加载类型“myapp.main”,因为方法“runfile”没有实现(无RVA) ”。
为什么会出现此错误以及如何修复它?
I have a .dll built in C++/CLI and .NET. Therefore, it is targeted to .NET applications. The API is a set of wrappers that uses managed types so it is NET native. I have imported the .dll and added a function like:
[DllImport(@"maplib.dll")]
public static extern bool initialize(string a);
When I call this function in my C# code, it works fine, but if I want to add another function, like..
[DllImport(@"maplib.dll")]
public static extern bool initialize(string a);
public static extern bool runfile(string a, string b);
I get this error when I run my program. It is related to the second function:
"Could not load type 'myapp.main' from assembly 'myapp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' because the method 'runfile' has no implementation (no RVA)."
Why does this error occur and how can I fix it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想要两个函数,则必须添加 DllImport 属性两次:
但是如果您的 dll 是 .NET 那么为什么不直接添加对其的常规引用并像使用 C# 代码一样使用它呢?
You must add the
DllImport
attribute twice if you want two functions:But if your dll is .NET then why not just add a regular reference to it and use it like you would use C# code?
据我了解,您已经创建了一个用 C++ 实现的托管类库。您希望在其他托管代码(用 C#、VB.NET 等编写)中使用此类库(程序集)。目前,您已将一些方法(您的 API)导出为本机 C++ 调用:
如果您希望能够从本机 C++ 代码调用您的库,那么这很有用,但如果您只想使用托管代码来调用您的库,我建议您将 API 创建为托管代码:
这样,您的 C# 代码中就不需要
DllImport
。你可以这样做:我希望我正确理解你的问题。
As I understand it, you have created a library of managed class(es) implemented in C++. You want to use this class library (assembly) in other managed code (written in C#, VB.NET, etc.). Currently, you have exported some methods (your API) as native C++ calls:
This is useful if you want to be able to call your library from native C++ code, but if you want to use only managed code to call your library, I suggest that you create your API as managed code:
This way, you do not need
DllImport
in your C# code. You can just do:I hope I understood your question correctly.