DllImport two dimensional array

S

SeC

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[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# ?
 
S

SeC

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[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# ?


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 = new byte[2];
}

unsafe
{
IntPtr[] ptrs = new IntPtr[2];
for (int i = 0; i < 2; i++)
{
fixed (void* ptr = tab)
{
ptrs = 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[j] = ++tmp;
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top