繁体   English   中英

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

[英]Access C++ static methods from C#

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

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

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

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

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关键字

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM