Click here to Skip to main content
15,899,754 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All !

I have a 2D array in C# and want handle it by c++.
I write code for C++:
C++
extern "C"
{
     __declspec (dllexport) void __stdcall MatrixH(unsigned char **mymatrix,int matwidth,int matheight)
     {
          mymatrix[0][0]++;
     }
}

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);
}


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 :(
Posted

1 solution

The format in memory of managed and unmanaged arrays is different.
You must use marshalling to convert them.
See this[^] and this[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900