简体   繁体   English

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

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

Say you have following C++ code: 假设您有以下C ++代码:

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

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

I want to access this in my C# project using DllImport : 我想使用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);
    }
}

This works perfectly fine for testA , but testB fails throwing an EntryPointNotFoundException. 这对testA完全正常,但testB无法抛出EntryPointNotFoundException。

Can I access testB from my C# code? 我可以从我的C#代码访问testB吗? How? 怎么样?

static does not mean the same in C++ as it does in C#. static在C ++中与在C#中的含义不同。 At namespace scope, static gives a name internal linkage, meaning that it is only accessible within the translation unit that contains the definition. 在命名空间范围内, static给出了一个名称内部链接,这意味着它只能在包含该定义的翻译单元中访问。 Without static, it has external linkage, and is accessible in any translation unit. 没有静态,它具有外部链接,并且可以在任何翻译单元中访问。

You will need to remove the static keyword when you want to use DllImport 如果要使用DllImport则需要删除static关键字

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

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