问题C ++处理C#的2D数组 [英] Problem C++ handle 2D array of C#

查看:75
本文介绍了问题C ++处理C#的2D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我在C#中有一个2D数组,想要用c ++处理它。

我为C ++编写代码:

Hi All !

I have a 2D array in C# and want handle it by c++.
I write code for C++:

extern "C"
{
     __declspec (dllexport) void __stdcall MatrixH(unsigned char **mymatrix,int matwidth,int matheight)
     {
          mymatrix[0][0]++;
     }
}



并将其编译为mylib.dll。



And我在C#中的代码:


and compile it to mylib.dll.

And my code in C# :

static byte[,] mymat = new byte[10,10];   // my 2d array

[DllImport("mylib.dll")]
extern static void MatrixH(byte[,] mymatrix,int matwidth,int matheight);

private static void MyHandle()
{
     MatrixH(mymat,10,10);
}





当我运行我的代码时,函数MatrixH(mymat,10,10)中出现错误:

尝试读取或写入受保护的内存。这通常表示内存已损坏。



请!帮助我:(



When I run my code, a error was appear in function "MatrixH(mymat,10,10)":
"Attempted to read or write protected memory.This is often an indication that memory is corrupted."

Please! help me :(

推荐答案

托管和非托管阵列的内存格式不同。

你必须使用编组来转换它们。 br />
见这个 [ ^ ]和 [ ^ ]。
The format in memory of managed and unmanaged arrays is different.
You must use marshalling to convert them.
See this[^] and this[^].


这篇关于问题C ++处理C#的2D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆