DllImport two dimensional array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • SeC

    #1

    DllImport two dimensional array

    Hello.
    I have external DLL with function:

    extern "C" __declspec(dlle xport)
    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

    #2
    Re: DllImport two dimensional array

    On 28 Mar, 12:58, SeC <sec....@gmail. comwrote:
    Hello.
    I have external DLL with function:
    >
    extern "C" __declspec(dlle xport)
    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# ?
    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(dlle xport)
    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;
    }
    }
    }

    Comment

    Working...