gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:16:53 26 4
gpt4 key购买 nike

假设您有以下 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。

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

最佳答案

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

如果要使用DllImport,您需要移除static关键字

关于c# - 从 C# 访问 C++ 静态方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42411086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com