如何封送指向结构体指针数组的指针?
我有一个具有以下签名的 C 函数:
int my_function(int n, struct player **players)
players
是一个指向 structplayer
对象的指针数组的指针。 n
是数组中指针的数量。该函数不会修改数组或结构体的内容,并且返回后不保留任何指针。
我尝试了以下操作:
[DllImport("mylibary.dll")]
static extern int my_function(int n,
[In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
player_in []players);
但是,它将数据封送为指向结构数组的指针,而不是指向结构指针数组的指针。
I have a C function with the following signature:
int my_function(int n, struct player **players)
players
is a pointer to an array of pointers to struct player
objects. n
is the number of pointers in the array. The function does not modify the array nor the contents of the structures, and it does not retain any pointers after returning.
I tried the following:
[DllImport("mylibary.dll")]
static extern int my_function(int n,
[In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 0)]
player_in []players);
However, that marshals the data as a pointer to an array of structures, not a pointer to an array of pointers to structures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信您必须手动进行一些编组。函数声明应如下所示:
在将其传递给本机函数之前,我们需要分配一些本机内存并将结构编组到其中:
如果您的本机函数要保留并重新使用这些内存位置,这是行不通的。如果是这种情况,要么推迟释放内存,直到您认为不再使用它,要么在本机方法中复制传入的数据,并让托管端在调用后立即清理其内存。
I believe you'll have to do some of the marshaling manually. The function declaration should look like this:
We'll need to allocate some native memory and marshal the structures to it before passing it in to the native function:
If your native function is going to hold on to and re-use these memory locations, this won't work. If this is the case, either hold off on freeing the memory until you think it's not being used anymore or in the native method copy the data passed in and let the managed side clean up its memory immediately after the call.