简体   繁体   中英

C# interop : handling pointer-array in unmanaged struct

I'm wrapping a few calls to the unmanaged Aubio library dll (Aubio.org), and I'm wondering what a good way is to deal with the Aubio samplebuffer.

It's defined like this :

// Buffer for real values
struct _fvec_t {
  uint length;    // length of buffer
  uint channels;  // number of channels
  float **data;   // data array of size [length] * [channels]
};

Aubio creates the struct for me with the datamembers set up correctly, so I get an IntPtr. I need to read/write to the data pointer(s) from my C# code.

for (int chan_idx = 0; chan_idx < my_fvec.channels; ++chan_idx)
    for (int i=0; i<something; i++)
       my_fvec.data[chan_idx][i] = SomeRandomValue();

What is the correct way to 'map' a C# struct to the fvec_t type so I can access the data member properly to read/write to it ?

(Or should I use Marshal.Copy,and how do I do that with the array-of-pointers ?)

I'd imagine you could define a managed struct and PtrToStructure what you have, modify, then StructureToPtr (back to the same location), but it might be just as simple, since the memory is already allocated and all, to just read out the intptr's for the arrays and then write the float arrays to them with Copy:

http://msdn.microsoft.com/en-us/library/ez2e4559.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM