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。

我可以testB从我的 C# 代码访问吗?如何?

4

1 回答 1

10

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

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

于 2017-02-23T09:09:24.637 回答