目前因为工作原因需要使用WPF制作用户界面,同时对于处理算法方面需要使用C/C++来写算法并封装成Dll,以便C#端可以调用使用,对于常用类型,比如int,void,char等类别的传递因为之前使用过所以还算驾轻就熟,后面应用到了复杂点的结构体。
例如结构体如下:
C部分的
typedef struct ErrorArea {//the closed rectangle area which contains the black pixels that are connected together.
int x;//positioin x of the rect left, it is in [0, Solution).
int top;//positioin y of the rect top it is in [0, MaxLinesPerPage).
int w;//width of the rect, in is in [0, Solution).
int bottom;//the last line (bottom of the rect)
int weight;//how many pixels are black in the area.
int minWidth;//minmum widht of a segment in the area
int maxWidth;//minmum widht of a segment in the area
int type;
//bool splited;//if is spliter into mulite vertical lines
//bool hasRing;//if has a ring
//bool finished;//is the error area is in detection.
bool merged;//if is spliter into mulite vertical lines
struct ErrorArea* prev;
struct ErrorArea* succ;
}Error;
对应C#部分定义
//define Struct
[StructLayout(LayoutKind.Sequential)]
public unsafe struct ErrorArea
{
public int x;//positioin x of the rect left, it is in [0, Solution).
public int top;//positioin y of the rect top it is in [0, MaxLinesPerPage).
public int w;//width of the rect, in is in [0, Solution).
public int bottom;//the last line (bottom of the rect)
public int weight;//how many pixels are black in the area.
public int minWidth;//minmum widht of a segment in the area
public int maxWidth;//minmum widht of a segment in the area
public int type;
public bool merged;//if is spliter into mulite vertical lines
ErrorArea* prev;
ErrorArea* succ;
}
但是在传递的时候出现了问题,之前C端 返回值传递的是Struct * ,但是在调用的时候总是出现问题,后来换了种实现方式,我将Struct* 作为参数指针传递过去进行修改,运行通过了。(18/2/8)