【问题标题】:Access C++ static methods from C#从 C# 访问 C++ 静态方法
【发布时间】:2017-02-23 08:58:11
【问题描述】:

假设你有以下 C++ 代码:

extern "C" {
    void testA(int a, float b) {
    }

    static void testB(int a, float b){
    }
}

我想在我的 C# 项目中使用 DllImport 访问它:

class PlatformInvokeTest
{
    [DllImport("test.so")]
    public static extern void testA(int a, float b);
    [DllImport("test.so")]
    internal static extern void testB(int a, float b);

    public static void Main() 
    {
        testA(0, 1.0f);
        testB(0, 1.0f);
    }
}

这对testA 非常有效,但testB 无法抛出EntryPointNotFoundException。

我可以从我的 C# 代码访问testB 吗?怎么样?

【问题讨论】:

  • 在全局范围内声明为 static 的函数没有外部链接,因此永远无法导出。你必须去除静电。您可能会将其与声明类成员函数混淆,将其声明为静态会做一些非常不同的事情。

标签: c# c++ dllimport entrypointnotfoundexcept


【解决方案1】:

static 在 C++ 中的含义与在 C# 中的含义不同。在命名空间范围内,static 给出了一个名称内部链接,这意味着它只能在包含定义的翻译单元内访问。没有静态,它有外部链接,并且可以在任何翻译单元中访问。

当你想使用DllImport时,你需要删除static关键字

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-07
    • 2015-02-18
    • 2014-08-31
    • 2013-04-20
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多