C# 互操作:处理非托管结构中的指针数组
我正在包装对非托管 Aubio 库 dll (Aubio.org) 的一些调用, 我想知道处理 Aubio 样本缓冲区的好方法是什么。
它的定义是这样的:
// 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 使用正确设置的数据成员为我创建了结构,所以我得到了一个 IntPtr。 我需要从 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();
将 C# 结构“映射”到 fvec_t 类型以便我可以正确访问数据成员以对其进行读/写的正确方法是什么?
(或者我应该使用 Marshal.Copy,以及如何使用指针数组来做到这一点?)
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 ?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想你可以定义一个托管结构和 PtrToStructure 你所拥有的,修改,然后 StructureToPtr (回到同一位置),但它可能同样简单,因为内存已经分配了,所有的,只需读出intptr 用于数组,然后使用 Copy 将浮点数组写入其中:
http:// /msdn.microsoft.com/en-us/library/ez2e4559.aspx
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