C# DLLImport for C++动态链接库

发布于 2024-12-17 05:15:16 字数 602 浏览 0 评论 0原文

我有一个用 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 技术交流群。

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

发布评论

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

评论(2

墨落画卷 2024-12-24 05:15:16

如果您想要两个函数,则必须添加 DllImport 属性两次:

[DllImport(@"maplib.dll")]
public static extern bool initialize(string a);
[DllImport(@"maplib.dll")]
public static extern bool runfile(string a, string b);

但是如果您的 dll 是 .NET 那么为什么不直接添加对其的常规引用并像使用 C# 代码一样使用它呢?

You must add the DllImport attribute twice if you want two functions:

[DllImport(@"maplib.dll")]
public static extern bool initialize(string a);
[DllImport(@"maplib.dll")]
public static extern bool runfile(string a, string b);

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?

揽清风入怀 2024-12-24 05:15:16

据我了解,您已经创建了一个用 C++ 实现的托管类库。您希望在其他托管代码(用 C#、VB.NET 等编写)中使用此类库(程序集)。目前,您已将一些方法(您的 API)导出为本机 C++ 调用:

public bool initialize(string a) {
    // ...
}

public bool runfile(string a, string b) {
    // ...
}

如果您希望能够从本机 C++ 代码调用您的库,那么这很有用,但如果您只想使用托管代码来调用您的库,我建议您将 API 创建为托管代码:

public ref class MyLibraryFunctions {
    public:
        static bool initialize(string a);
        static bool runfile(string a, string b);
};

这样,您的 C# 代码中就不需要 DllImport。你可以这样做:

using Something.MyLibrary;
....
    public void doSomethingThatNeedsMyLibrary() {
       MyLibraryFunctions.initialize(someString);
    }

我希望我正确理解你的问题。

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:

public bool initialize(string a) {
    // ...
}

public bool runfile(string a, string b) {
    // ...
}

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:

public ref class MyLibraryFunctions {
    public:
        static bool initialize(string a);
        static bool runfile(string a, string b);
};

This way, you do not need DllImport in your C# code. You can just do:

using Something.MyLibrary;
....
    public void doSomethingThatNeedsMyLibrary() {
       MyLibraryFunctions.initialize(someString);
    }

I hope I understood your question correctly.

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