c#调用c++的结构体和函数

youth0826 youth0826 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
在c++的dll文件里是这样定义的:

// ============== 常量定义 =====================
#define ID_MAX_SIZE 8 // ID号最多8字节(64bits)
#define MAX_LABELS 5 // 一次读写操作最多不超过5个

typedef struct tagID
{
unsigned short Address; // ID号在标签内存中的地址
unsigned short Length; // ID号字节数
unsigned char Num[MAX_LABELS][ID_MAX_SIZE]; // 存放ID号的字节数组
} ID;

//读取标签ID号
apiReturn _stdcall ReadLabelID(HANDLE hScanner, int nMax, ID *IDBuffer, int *nCounter);


我在c#里面是这样写的,但是总是不对

[DllImport("Reader2022DLL.dll")]
unsafe public static extern UInt16 ReadLabelID(IntPtr hScanner,int nMax,ref tagID IDBuffer,ref int nCounter);

public const int ID_MAX_SIZE=8; // ID号最多8字节(64bits)
public const int MAX_LABELS = 5; // 一次读写操作最多不超过5个
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi,Pack=1)]
unsafe public struct tagID
{
public ushort Address; // ID号在标签内存中的地址
public ushort Length; // ID号字节数
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 40)]
public byte [,] Num; // 存放ID号的字节数组
};


主要c#在读struct结构体里面的Num的时候总是不对,读Address和Length都是对的
另外C++里面的byte* WORD*转为c#里面的什么类型最好?
谢谢了
...全文
评论 9
收藏 已收藏
分享
返回顶部
youth0826
C# 111,128 成员 642,537 内容