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

1 篇文章 0 订阅

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

...

}

     

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhang11wu4

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值