2017-02-23 140 views
10

静态方法说你有下面的C++代码:访问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吗?怎么样?

+6

函数声明*静态*在全球范围内有没有外部链接,所以不能导出你将需要删除static关键字。你将不得不删除静态。你可能会混淆它与声明一个类成员函数,声明它静态做了一些非常不同的事情。 –

回答

8

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

当你想使用DllImport