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

Phi*_*ens 11 c# c++ dllimport entrypointnotfoundexcept

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

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

    static void testB(int a, float b){
    }
}
Run Code Online (Sandbox Code Playgroud)

我想在我的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);
    }
}
Run Code Online (Sandbox Code Playgroud)

这非常适用testA,但testB无法抛出EntryPointNotFoundException.

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

vrw*_*wim 10

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

您需要static在要使用时删除该关键字DllImport