gpt4 book ai didi

c# 在 DllImport 中使用 const

转载 作者:行者123 更新时间:2023-11-30 14:37:49 26 4
gpt4 key购买 nike

我有一个来自 SDK 的代码示例,DLL 是用 C 语言编写的,并调用以下内容:

int getAttribute(const RawDevControl *控制, RawDevAttribute *属性)

我正在使用

[DllImport(@"Dev.dll",
SetLastError = true)]
internal static extern int getAttribute(const RControl *control, RAttribute *attribute);

但是在定义这个引用函数时当然不能使用const作为类型。

我怎样才能用 C# 实现它?

最佳答案

因为 C# 没有 const 引用的概念,所以你真的不需要担心它。在 DLL 端,代码仍会认为您有一个 const 指针。因此,您的导入更改为:

[DllImport(@"Dev.dll", SetLastError = true)]
internal static extern int getAttribute(RControl control, RAttribute attribute);

当然,这假定 RControlRAttribute 都已在 C# 中定义。如果它们是结构,请按照 MSDN 上的示例定义用于 P/Invoke 的结构。如果它们是类,那就是一组不同的问题。在这种情况下,类最好是基于 COM 的。

关于c# 在 DllImport 中使用 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8859060/

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