C#調用c++的dll,結構體數組作爲引用參數的傳遞方式

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)
{

...

}

     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章