签名与Interop不兼容-从c#

时间:2019-01-15 23:48:14

标签: c# c interop marshalling

我正在尝试使用互操作性功能来为c编写的Pigpio库创建一个.net包装器。但是我似乎陷入了以下错误: “ 无法封送'parameter#2':签名与Interop不兼容”。 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; }
}

和BitMask:

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

知道我在做什么错吗?

1 个答案:

答案 0 :(得分:0)

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

相关问题