在 C# 中封送固定大小的枚举数组
Marshaling fixed size enum array in C#
我正在尝试在 C# 中封送固定大小的数组枚举。
这是 C:
中的本地声明
typedef enum GPIO_Dir
{
GPIO_OUTPUT =0,
GPIO_INPUT,
}
GPIO_Dir;
FT4222_STATUS FT4222_GPIO_Init(FT_HANDLE ftHandle, GPIO_Dir gpioDir[4]);
代码示例如下:
GPIO_Dir gpioDir[4];
gpioDir[0] = GPIO_OUTPUT;
gpioDir[1] = GPIO_OUTPUT;
gpioDir[2] = GPIO_OUTPUT;
gpioDir[3] = GPIO_OUTPUT;
FT4222_GPIO_Init(ftHandle, gpioDir);
本机代码运行正常。
我可以处理 FT_HANDLE。
我尝试了多种选择,但似乎没有任何效果。我一直在尝试多个定义但没有成功,例如:
[DllImport("LibFT4222.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern FtStatus FT4222_GPIO_Init(IntPtr ftHandle, [MarshalAs(UnmanagedType.LPArray, SizeConst = 4)] GpioPinMode[] gpioDir);
我也一直在尝试装饰数组以传递:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
private GpioPinMode[] _gpioDirections = new GpioPinMode[PinCountConst];
GpioPinMode 是一个简单的枚举:
internal enum GpioPinMode : int
{
Output = 0,
Input,
}
非常感谢。
我编写了一个非常简单的程序来将枚举[] 发送到 C++。 C++ 将读取枚举并将其当前内容写入文件。现在它没有 return 任何东西,因为我不知道 FtStatus
可以是什么。
C#
internal enum GpioPinMode : int
{
Output = 0,
Input,
}
[DllImport("library.dll", EntryPoint = "FT4222_GPIO_Init", CallingConvention = CallingConvention.Cdecl)]
public static extern void FT4222_GPIO_Init(GpioPinMode[] gpioDir);
static void Main(string[] args)
{
GpioPinMode[] gpioDir = new GpioPinMode[4];
gpioDir[0] = GpioPinMode.Input;
gpioDir[1] = GpioPinMode.Input;
gpioDir[2] = GpioPinMode.Output;
gpioDir[3] = GpioPinMode.Output;
FT4222_GPIO_Init(gpioDir);
}
C++
#include "pch.h"
#include <fstream>
#define DllExport extern "C" __declspec(dllexport)
typedef enum GPIO_Dir
{
GPIO_OUTPUT = 0,
GPIO_INPUT,
}
GPIO_Dir;
DllExport void FT4222_GPIO_Init(GPIO_Dir gpioDir[4])
{
std::ofstream myfile;
myfile.open("File.txt", std::ios::out);
if (gpioDir[0] == GPIO_INPUT)
myfile << 0 << ": input" << std::endl;
else
myfile << 0 << ": output" << std::endl;
if (gpioDir[1] == GPIO_INPUT)
myfile << 1 << ": input" << std::endl;
else
myfile << 1 << ": output" << std::endl;
if (gpioDir[2] == GPIO_INPUT)
myfile << 2 << ": input" << std::endl;
else
myfile << 2 << ": output" << std::endl;
if (gpioDir[3] == GPIO_INPUT)
myfile << 3 << ": input" << std::endl;
else
myfile << 3 << ": output" << std::endl;
}
我希望这能让您了解如何做到这一点。
我正在尝试在 C# 中封送固定大小的数组枚举。
这是 C:
中的本地声明typedef enum GPIO_Dir
{
GPIO_OUTPUT =0,
GPIO_INPUT,
}
GPIO_Dir;
FT4222_STATUS FT4222_GPIO_Init(FT_HANDLE ftHandle, GPIO_Dir gpioDir[4]);
代码示例如下:
GPIO_Dir gpioDir[4];
gpioDir[0] = GPIO_OUTPUT;
gpioDir[1] = GPIO_OUTPUT;
gpioDir[2] = GPIO_OUTPUT;
gpioDir[3] = GPIO_OUTPUT;
FT4222_GPIO_Init(ftHandle, gpioDir);
本机代码运行正常。
我可以处理 FT_HANDLE。
我尝试了多种选择,但似乎没有任何效果。我一直在尝试多个定义但没有成功,例如:
[DllImport("LibFT4222.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern FtStatus FT4222_GPIO_Init(IntPtr ftHandle, [MarshalAs(UnmanagedType.LPArray, SizeConst = 4)] GpioPinMode[] gpioDir);
我也一直在尝试装饰数组以传递:
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
private GpioPinMode[] _gpioDirections = new GpioPinMode[PinCountConst];
GpioPinMode 是一个简单的枚举:
internal enum GpioPinMode : int
{
Output = 0,
Input,
}
非常感谢。
我编写了一个非常简单的程序来将枚举[] 发送到 C++。 C++ 将读取枚举并将其当前内容写入文件。现在它没有 return 任何东西,因为我不知道 FtStatus
可以是什么。
C#
internal enum GpioPinMode : int
{
Output = 0,
Input,
}
[DllImport("library.dll", EntryPoint = "FT4222_GPIO_Init", CallingConvention = CallingConvention.Cdecl)]
public static extern void FT4222_GPIO_Init(GpioPinMode[] gpioDir);
static void Main(string[] args)
{
GpioPinMode[] gpioDir = new GpioPinMode[4];
gpioDir[0] = GpioPinMode.Input;
gpioDir[1] = GpioPinMode.Input;
gpioDir[2] = GpioPinMode.Output;
gpioDir[3] = GpioPinMode.Output;
FT4222_GPIO_Init(gpioDir);
}
C++
#include "pch.h"
#include <fstream>
#define DllExport extern "C" __declspec(dllexport)
typedef enum GPIO_Dir
{
GPIO_OUTPUT = 0,
GPIO_INPUT,
}
GPIO_Dir;
DllExport void FT4222_GPIO_Init(GPIO_Dir gpioDir[4])
{
std::ofstream myfile;
myfile.open("File.txt", std::ios::out);
if (gpioDir[0] == GPIO_INPUT)
myfile << 0 << ": input" << std::endl;
else
myfile << 0 << ": output" << std::endl;
if (gpioDir[1] == GPIO_INPUT)
myfile << 1 << ": input" << std::endl;
else
myfile << 1 << ": output" << std::endl;
if (gpioDir[2] == GPIO_INPUT)
myfile << 2 << ": input" << std::endl;
else
myfile << 2 << ": output" << std::endl;
if (gpioDir[3] == GPIO_INPUT)
myfile << 3 << ": input" << std::endl;
else
myfile << 3 << ": output" << std::endl;
}
我希望这能让您了解如何做到这一点。