C#调用c++的dll,结构体数组作为引用参数的传递方式

来源:互联网 发布:mac版beyond compare 编辑:程序博客网 时间:2024/05/01 00:57

C#调用c++的dll,返回的是dll的自定义结构体数组,在C#中传递的参数是自定义结构体数组的首元素,切记,是首元素,而不是首地址!C++的参数是自定义结构体的指针。

1.C#代码:

(1)

 public class Form1 : System.Windows.Forms.Form
{
        public struct LEAK_RECT
        {
            int x;
            int y;
            int width;
            int height;
        };
        [DllImport(@"ObjectScan.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall, EntryPoint = "Leak_Crack_Detect")]
        public extern static byte Leak_Crack_Detect(string imgname, string txtfile, ref int c_x, ref int c_y, ref int c_w, ref int c_h, ref LEAK_RECT leak_rects, ref int leak_num);

        ...

        }

        private void button3_Click(object sender, EventArgs e)
        {
            string txtfile= "d:\\detect.txt";//文本文件,每行的格式:文件名,1或0(有漏水为1,否则0),1或0(有裂缝1,无0)
            //在选定的目录下先建立detect.txt文件
            int c_x=0;//裂缝框x坐标
            int c_y = 0;//裂缝框y坐标
            int c_w=0;//裂缝框宽
            int c_h=0;//裂缝框高
            LEAK_RECT[] leak_rects = new LEAK_RECT[20];
            int leak_num=0;
            //画框最好还是漏水用红框,裂缝用绿框,与我生成的文件保持一致
            byte bDetect = Leak_Crack_Detect(m_FileName,txtfile,ref c_x,ref c_y,ref c_w,ref c_h,ref leak_rects[0],ref leak_num);
        }

      (2)C++代码

      .h文件:

       struct Rect_Stru
      {
int x;
int y;
int width;
int height;
      };

      .cpp文件:

      extern "C" __declspec(dllexport)  bool __stdcall Leak_Crack_Detect(char* imgname,char* txtfile,int &c_x,int &c_y,int &c_w,int &c_h,Rect_Stru *leak_rects,int       &leak_num)
{

...

}

     

0 0
原创粉丝点击