gpt4 book ai didi

c# - 签名不兼容互操作 - 从 c# 调用 c 方法

转载 作者:行者123 更新时间:2023-11-30 19:28:18 24 4
gpt4 key购买 nike

我正在尝试利用互操作功能为 C 编写的 Pigpio 库创建 .net 包装器。但是我似乎陷入了以下错误:“无法编码(marshal)‘参数#2’:签名与互操作不兼容”。 C 方法具有以下签名:

int gpioWaveAddGeneric(unsigned numPulses, gpioPulse_t *pulses);

gpioPulse_t 是:

typedef struct
{
uint32_t gpioOn;
uint32_t gpioOff;
uint32_t usDelay;
} gpioPulse_t;

我假设这将是调用该类的签名:

 [DllImport(Constants.PiGpioLibrary, CallingConvention = CallingConvention.Cdecl, EntryPoint = "gpioWaveAddGeneric")]
public static extern int GpioWaveAddGeneric(uint numPulses, [In, MarshalAs(UnmanagedType.LPArray)] GpioPulse[] pulses);

其中 GpioPulse[] 位于

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public class GpioPulse
{
private BitMask m_GpioOn;
private BitMask m_GpioOff;
private uint m_DelayMicroseconds;
public BitMask GpioOn { get => m_GpioOn; set => m_GpioOn = value; }
public BitMask GpioOff { get => m_GpioOff; set => m_GpioOff = value; }
public uint DurationMicroSecs { get => m_DelayMicroseconds; set => m_DelayMicroseconds = value; }
}

和位掩码:

[Flags]
public enum BitMask : uint
{
None = 0x00000000,
All = 0xFFFFFFFF,
Bit00 = 0x00000001,
Bit01 = 0x00000002,
Bit02 = 0x00000004,
Bit03 = 0x00000008,
}

知道我做错了什么吗?

最佳答案

解决方案:C# 中的 uint32_t 只是一个 uint,因此您的 GpioPulse C# 类应该与 C 版本一样简单,例如公共(public)结构GpioPulse { uint gpioOn; uint GPIOOff; uint usDelay; }

关于c# - 签名不兼容互操作 - 从 c# 调用 c 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54208467/

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