C++:c#与c++中结构体对齐问题

使用C#调用C++的dll,参数传递结构体,发现定义的结构体大小不一样,同样定义的结构体,在C#和C++中使用sizeof查看后所占字节数不一样,导致乱码。

原来要对C#代码进行对齐处理

对齐方法如下

C++中定义结构体


typedef struct
{
	bool useLogo = false;
	Point p1;
	Point p2;
	Point p3;
	Point p4;
	bool useRestrictedArea = false;
	Point RestrictedAreaP1;
	Point RestrictedAreaP2;
	Point RestrictedAreaP3;
	Point RestrictedAreaP4;
	double highAngle;
	double lowAngle;
	int logoClass;
	int intFirstMatchImgNum ;
	double doubleSecondMatchAngleRange;
	double OtherLogoDetectAngle;
	int thresholdNum1 ;
	int thresholdNum2 ;
	int thresholdNum3 ;
	char csvDir[128];
	int xiaci_area_threshold;
	int xiaci_num_threshold;
	
} detectImgConfig;

c#中定义结构体


[StructLayout(LayoutKind.Sequential)]
public struct detectImgConfig
{
    public bool useLogo;
    public Point p1;
    public Point p2;
    public Point p3;
    public Point p4;
    public bool useRestrictedArea ;
    public Point RestrictedAreaP1;
    public Point RestrictedAreaP2;
    public Point RestrictedAreaP3;
    public Point RestrictedAreaP4;
    public double highAngle;
    public double lowAngle;
    public int logoClass;
    public int intFirstMatchImgNum;
    public double doubleSecondMatchAngleRange;
    public double OtherLogoDetectAngle;
    public int thresholdNum1;
    public int thresholdNum2;
    public int thresholdNum3;
    public int xiaci_area_threshold;
    public int xiaci_num_threshold;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
    public string csvDir;
};

按照这种方式定义的结构体就可以对齐了,不会产生乱码

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值