使用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;
};
按照这种方式定义的结构体就可以对齐了,不会产生乱码