Hello.
I have external DLL with function:
extern "C" __declspec(dllexport)
void ByteTest(BYTE **arr, BYTE w, BYTE h)
{
BYTE tmp = 0;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
arr[i][j] = ++tmp;
}
}
}
Now in C#:
[DllImport("test.dll")]
extern static void ByteTest(ref IntPtr ptr, int w, int h);
and
byte[,] tab = new byte[2, 2];
unsafe
{
fixed (void* fptr = tab)
{
IntPtr ptr = new IntPtr(fptr);
ByteTest(ref ptr, 2, 2);
}
}
This works, but I have error values in tab(3, 4, 0, 0) instead of (1,
2, 3, 4). How to marshall BYTE** in C# ?
SeC - 28 Mar 2008 14:38 GMT
> Hello.
> I have external DLL with function:
[quoted text clipped - 29 lines]
> This works, but I have error values in tab(3, 4, 0, 0) instead of (1,
> 2, 3, 4). How to marshall BYTE** in C# ?
I managed to work it out. Here's sample:
c#:
[DllImport("test.dll")]
extern static void ByteTest(IntPtr[] ptr, int w, int h);
...
byte[][] tab = new byte[2][];
for (int i = 0; i < 2; i++)
{
tab[i] = new byte[2];
}
unsafe
{
IntPtr[] ptrs = new IntPtr[2];
for (int i = 0; i < 2; i++)
{
fixed (void* ptr = tab[i])
{
ptrs[i] = new IntPtr(ptr);
}
}
ByteTest(ptrs, 2, 2);
}
test.dll:
extern "C" __declspec(dllexport)
void ByteTest(BYTE **arr, BYTE w, BYTE h)
{
BYTE tmp = 0;
for(int i=0; i<h; i++)
{
for(int j=0; j<w; j++)
{
arr[i][j] = ++tmp;
}
}
}