C 库的 C# 包装器类

标签 c# linux raspberry-pi wiringpi

我想用 mcp23017.c 中的函数创建一个 C# 包装器类在 WiringPi 内C 库。我使用了这个现有的 C# WiringPi WrapperClass对于其他功能。我想扩展这个包装类以使用 mcp23017 的功能。我尝试在包装器中创建一个具有一个函数的新类:

public class mcp23017
{

    [DllImport("libmcp23017.so", EntryPoint = "myPinMode")]
    public static extern void myPinMode(struct wiringPiNodeStruct *node,Int32 pin, Int32 mode);

}

但是我得到了结构元素的这些错误。

) expected.
; expected.
{ expected.
} expected.
Invalid token "*" in class struct or interface member declaration

我必须在包装类中定义一个结构吗?它是如何工作的?不熟悉这个,因为我是使用 C# 的新手。

最佳答案

在 C# 中对参数使用 struct 是无效的。编码器应该为您处理此行为。也许你的意思是这样的。

[StructLayout(LayoutKind.Sequential)]
public struct WiringNode
{
    //Struct members here
}

[DllImport("libmcp23017.so", EntryPoint = "myPinMode")]
public static extern void myPinMode(ref WiringNode node, Int32 pin, Int32 mode);

然后调用为:

var myStruct = new WiringStruct();
//Set struct members

myPinMode(ref myStruct, whatever, whatever);

关于C 库的 C# 包装器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38852323/

相关文章:

video - 使用avconv在设定时间从h264视频中获取单帧

c# - 更改 ClickOnce 应用程序的更新 URL

linux - Docker 统计数据与 Linux ps 与实际 CPU 利用率

Android 到 Linux 蓝牙找不到所有 UUID

c - 仅在树莓派上使用 libxml2 访问 xmlNode 时出现段错误

linux - 在树莓派上安装 Octave

c# - foreach循环

c# - 页面重定向而不改变 URL-Umbraco | C#

c# - 在 Visual Studio IDE 中从 XSD 自动生成 C#

linux - uinput虚拟设备和/dev/input/mice