简体   繁体   English

C#interop:处理非托管结构中的指针数组

[英]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. 我正在包装对非托管Aubio库dll(Aubio.org)的一些调用,我想知道处理Aubio样本缓冲区的好方法是什么。

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. Aubio使用正确设置的数据成员为我创建了结构,因此我得到了IntPtr。 I need to read/write to the data pointer(s) from my C# code. 我需要从C#代码读取/写入数据指针。

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 ? 将C#结构“映射”到fvec_t类型的正确方法是什么,以便我可以正确访问数据成员以对其进行读写?

(Or should I use Marshal.Copy,and how do I do that with the array-of-pointers ?) (或者我应该使用Marshal.Copy,以及如何使用指针数组?)

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: 我以为您可以定义托管结构和PtrToStructure所拥有的内容,然后进行修改,然后再修改StructureToPtr(返回相同的位置),但这可能就这么简单,因为已经分配了内存,并且所有这些都可以读出数组的intptr,然后使用Copy将float数组写入它们:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#指向带有数组的非托管结构的指针 - C# pointer to unmanaged struct with array 用于非托管互操作的C#对象的指针 - Pointer of a C# object for unmanaged interop 从C#调用非托管C DLL函数(带有指向struct的指针和指向bool数组的指针) - Calling unmanaged C DLL function from C# (with pointer to struct and pointer to bool array) C#互操作与非托管DLL - c# interop with unmanaged dll 如何使用指针调用非托管dll填充C#中的结构 - How to call unmanaged dll to populate struct in C# using a pointer 互操作:在给定平台上,C# 和非托管 C++ 的结构包装对齐默认值是否相同? - Interop: are struct packing alignment defaults the same for C# and unmanaged C++ on a given platform? 在 C# 互操作中传递结构指针导致 NULL - Passing a struct pointer in C# interop results in NULL 如何在非托管C的DLLImport的C#结构中使用unichar数组 - How to use unichar array in C# struct with DLLImport of unmanaged C 封送处理类型数组的指针(托管C#-&gt;非托管C ++) - Marshaling a pointer to an array of types (managed C# -> unmanaged C++) C#Interop:从带有out参数的非托管代码返回结构会产生奇怪的结果 - C# Interop : Returning struct from unmanaged code with out parameter gives strange results
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM