0

I have a c program that I want to reprogram in c#.

In the C program, a function is used which accesses an address of a 2d array.

Dll.h

int Fg_AddMem(Fg_Struct * Fg, void *pBuffer, const size_t Size, const frameindex_t bufferIndex, dma_mem *memHandle);

Programm.cpp:

#include "Dll.h"
.
.
#define BUFFERS 4
#define BUFFERSIZE(1024*1024)
static char buf[BUFFERS+1][BUFFERSIZE]

for (int i=0; i<BUFFERS; i++)
{
int ret = Fg_AddMem(fg, buf[i], BUFFERSIZE, i, mhead);
.
.
}

Now i want to programm it in C#

First, i created a class to import the Dll function over the pInvoker addon

CAM_SISO.cs:

[DllImport ("fglib5.dll", setLastError = true, CallingConvention = CallingConvention.Cdecl)]
public extern static int Fg_AddMem(IntPtr Fg, IntPtr pBuffer, int size, int bufferIndex, IntPtr memHandle);

I'm also created a BackgroundWorker for the for-loop

Application.cs:

int BufCnt=4;
int Buffersize = 1024*1024;
BackgroundWorker worker_current_add_mem;
.
this.Load += new EventHandler(this.add_mem_backgroundworker);
.
private void add_mem_backgroundworker(object sender, EventArgs e)
{
.
.
worker_current_add_mem.DoWork += new DoWorkEventHandler(worker_Add_mem);
}
.
private void worker_Add_mem(object sender, DoWorkEventArgs e)
{
char[,]buf = new char[BufCnt+1,Buffersize];
for(int i=0; i<BufCnt; i++)
{
int ret = CAM.SISO.Fg_AddMem(Fg, ??? , Buffersize, i, mHead);
.
.
}
}

So now my question is, how can I pass the address of the 2d array to an IntPtr. Also I would like in c, that the array is incremented in the first field to add a memory.

How do I proceed??

I already tried it with GCHandle and Marshal.Copy but nothing works for me.

  • `static char buf[BUFFERS+1][BUFFERSIZE]` why +1? – Jeroen van Langen Nov 08 '16 at 11:07
  • You might start here: [How to get IntPtr from bytearray in C#](http://stackoverflow.com/questions/537573/how-to-get-intptr-from-byte-in-c-sharp) – Jeroen van Langen Nov 08 '16 at 11:10
  • Don't use IntPtr, there is no point. Declare it as `byte[]` instead. The pinvoke marshaller takes care of pinning it and generating the pointer. Your C# code should use a jagged array, a `byte[][]`, to get an equivalent to *buf* in your C++ code. Technically sbyte but seems unlikely that is what you meant. – Hans Passant Nov 08 '16 at 12:30
  • To the `static char buf [BUFFERS + 1] [BUFFERSIZE]` I've found a code in the header file which contains this `BUFFERS + 1` For this reason I have adopted it. – David Lattrell Nov 08 '16 at 14:10
  • Thanks Hans Passant, I have changed the code, but it seems to be missing something. An error is displayed, for "invalid argument". Changing in CAM_SISO.cs `[DllImport ("fglib5.dll", setLastError = true, CallingConvention = CallingConvention.Cdecl)] public extern static int Fg_AddMem(IntPtr Fg, byte[] buf, int size, int bufferIndex, IntPtr memHandle);` in Applications.cs `private void worker_Add_mem(object sender, DoWorkEventArgs e) { byte [][] buf = new char[BufCnt+1][]; for(int i=0; i – David Lattrell Nov 08 '16 at 14:18

0 Answers0