如何封送可变大小的结构数组? C# 和 C++ 互操作帮助
我有以下 C++ 结构体
struct InnerStruct
{
int A;
int B;
};
struct OuterStruct
{
int numberStructs;
InnerStruct* innerStructs;
};
和 C++ 函数,
OuterStruct getStructs();
如何将其编组到 C#? C# 定义在哪里
struct OuterStruct {
InnerStruct[] innerStructs;
};
I have the following C++ structs
struct InnerStruct
{
int A;
int B;
};
struct OuterStruct
{
int numberStructs;
InnerStruct* innerStructs;
};
And a C++ function
OuterStruct getStructs();
How can I marshal this to C#? Where the C# definitions is
struct OuterStruct {
InnerStruct[] innerStructs;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须手动执行此操作,因为无法告诉 P/Invoke 层要从 C++ 返回值中封送多少数据。
请注意,如果您想从 C# 代码中释放
innerStructs
的内存,则必须在 C++ 代码中使用标准分配器CoTaskMemAlloc
,然后您可以调用Marshal.CoTaskMemFree
来释放innerStructs
。You'll have to do this manually, since there's no way to tell the P/Invoke layer how much data to marshal from your C++ return value.
Note that if you want to free the memory for
innerStructs
from your C# code, you have to use the standard allocatorCoTaskMemAlloc
in your C++ code--then you can callMarshal.CoTaskMemFree
to freeinnerStructs
.