问题 从C#访问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。

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


6707
2018-02-23 08:58


起源

声明了一个函数 静态的 在全球范围内没有外部链接,因此永远不能导出。你必须删除静态。你可能会因为声明一个类成员函数而混淆它,声明它静态做了一些非常不同的事情。 - Hans Passant


答案:


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

你需要删除 static 您想要使用的关键字 DllImport


10
2018-02-23 09:09