[英]How to parse struct to C++ dll from C#?
I am trying to call a function in a unmanaged C++ dll. 我试图在非托管C ++ dll中调用函数。
It has this prototype: 它具有以下原型:
[DllImport("C:\\Program Files\\MySDK\\VSeries.dll", EntryPoint = "BII_Send_Index_Template_MT" )]
internal unsafe static extern Int32 BII_Send_Index_Template_MT(IntPtr pUnitHandle, ref BII_Template template, Int32 option, Boolean async);
BII_Template template = new BII_Template();
error_code = BII_Send_Index_Template_MT(pUnitHandle, ref template, option, false);
This is how I define the BII_Template struct in C#: 这就是我在C#中定义BII_Template结构的方法:
public unsafe struct BII_Template {
public ulong id;
public ulong employee_id;
public ulong password;
public byte sensor_version;
public byte template_version;
public fixed char name[16];
public byte finger;
public byte admin_level;
public byte schedule;
public byte security_thresh;
public fixed byte noise_level[18];
public byte corramb;
public byte reference_x;
public byte reference_y;
public fixed byte ihcore[3];
public fixed byte ivcore[3];
public byte temp_xoffset;
public byte temp_yoffset;
public byte index;
public fixed byte inphase[5500];
};
It builds and when I run it the dll return error_code = "The record checksum is invalid." 它生成,当我运行它时,dll返回error_code =“记录校验和无效。”
I assume that I am using ref
in a wrong way or the size of some of the elements in the struct is wrong. 我以为我以错误的方式使用了
ref
,或者结构中某些元素的大小错误。
----- EDIT ------------ -----编辑------------
Here is the struct in C++: 这是C ++中的结构:
typedef struct {
unsigned long id;
unsigned long employee_id;
unsigned long password;
unsigned char sensor_version;
unsigned char template_version;
char name[16];
unsigned char finger;
unsigned char admin_level;
unsigned char schedule;
unsigned char security_thresh;
unsigned char noise_level[18];
unsigned char corramb ;
unsigned char reference_x ;
unsigned char reference_y ;
unsigned char ihcore[NUM_CORE];
unsigned char ivcore[NUM_CORE];
unsigned char temp_xoffset;
unsigned char temp_yoffset;
unsigned char index;
unsigned char inphase[PACKED_ARRAY_SIZE];
} BII_Template;
After testing a lot it seams that using fixed char ... does not give the same as [MashalAs...]. 经过大量测试后,发现使用固定字符...与[MashalAs ...]的含义不同。 After making this change it worked.
进行此更改后,它起作用了。
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct BII_Template { public uint id; 公共结构BII_Template {public uint id; public uint employee_id;
公共uint employee_id; public uint password;
公共uint密码;
public byte sensor_version;
public byte template_version;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 16)]
public byte[] name;
public byte finger;
public byte admin_level;
public byte schedule;
public byte security_thresh;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 18)]
public byte[] noise_level;
public byte corramb;
public byte reference_x;
public byte reference_y;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] ihcore;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public byte[] ivcore;
public byte temp_xoffset;
public byte temp_yoffset;
public byte index;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5500)]
public byte[] inphase;
}; };
My standard answer: add a C++/CLI assembly to your C# project. 我的标准答案:将C ++ / CLI程序集添加到C#项目。 Then the problem will solve itself.
然后问题将自行解决。
'unsigned long' in C++ is NOT equal to ulong in C#. C ++中的'unsigned long'不等于C#中的ulong。
Use uint instead. 请改用uint。
您需要在BII_Template上使用structlayout.sequal属性,请在google上获取详细信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.