从 C# 访问 C++ 静态方法

Access C++ static methods from C#

假设您有以下 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# 中的含义不同。在命名空间范围内,static 给出了名称内部链接,这意味着它只能在包含定义的翻译单元内访问。没有静态,它有外部链接,并且可以在任何翻译单元中访问。

如果要使用 DllImport

,您需要删除 static 关键字