标题:C#调用C++ Dll内存指针报内存错误
取消只看楼主
醉眼朦胧
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2020-7-6
 问题点数:0 回复次数:0 
C#调用C++ Dll内存指针报内存错误
C++代码如下:
 extern "C"   __declspec(dllexport) void _cdecl Test(char* path, int& width, int& height, int& step,uchar*& data)
{
   
    Mat thresholdImg, img = imread(path, IMREAD_GRAYSCALE);

    uchar* imgdata = new  uchar[img.rows*img.cols*3];

    imgdata = img.data;
    width = img.size().width;
    height = img.size().height;
    step = img.step;

    data = imgdata;

}

C# 代码如下:
  public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);


        [DllImport("MyDll.dll", EntryPoint = "Test",CallingConvention = CallingConvention.Cdecl)]

 
        private static  extern void Test(string path, ref int width, ref int height, ref int step,ref IntPtr imgdata);


        private void button1_Click(object sender, EventArgs e)
        {

            int width = new int();
            int height = new int();
            int step = new int();
            IntPtr imgData = new IntPtr();
         
            string imgPath = @"E:\OpenCV\WindowsFormsApp1\WindowsFormsApp1\bin\x64\Debug\picture.bmp";
            
            Test(imgPath, ref width, ref height, ref step, ref imgData);//调用C++ dll
            
            Bitmap bmp = new Bitmap(width, height, step, System.Drawing.Imaging.PixelFormat.Format8bppIndexed, imgData);//绘制图片
            bmp.Save(@"E:\OpenCV\WindowsFormsApp1\WindowsFormsApp1\bin\x64\Debug\test.bmp");//保存图片

        }


但是C#在" bmp.Save(@"E:\OpenCV\WindowsFormsApp1\WindowsFormsApp1\bin\x64\Debug\test.bmp");//保存图片" 的地方会报"尝试读取受保护的内存"
请问各位大神,有没有什么好的方法解决?
搜索更多相关主题的帖子: int img bmp step 内存 
2020-07-06 20:06



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-502407-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.144543 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved