[英]How do I use SWIG typemaps to marshall structure members from C++ to C# using P/Invoke?
Given the following SWIG interface definition: 给定以下SWIG接口定义:
%module example
%include "arrays_csharp.i"
%apply int INOUT[] {int *x}
struct mystruct
{
int *x;
}
SWIG produces the following (snippet from mystruct.cs): SWIG生成以下内容(来自mystruct.cs的片段):
public int[] x {
set {
examplePINVOKE.mystruct_x_set(swigCPtr, value);
}
get {
IntPtr cPtr = examplePINVOKE.mystruct_x_get(swigCPtr); // Error 1
SWIGTYPE_p_int ret = (cPtr == IntPtr.Zero) ? null : new SWIGTYPE_p_int(cPtr, false);
return ret; //Error 2
}
}
This causes VS2010 to produce the following two errors: 这会导致VS2010产生以下两个错误:
Error 1 Cannot implicitly convert type 'int[]' to 'System.IntPtr'
Error 2 Cannot implicitly convert type 'LibLinear.SWIG.SWIGTYPE_p_int' to 'int[]'
at the areas marked in the above code snippet. 在上面的代码段中标记的区域。
I developed the interface according to http://www.swig.org/Doc1.3/CSharp.html#CSharp_arrays_pinvoke_default_array_marshalling , which only talks about functions but not structures. 我根据http://www.swig.org/Doc1.3/CSharp.html#CSharp_arrays_pinvoke_default_array_marshalling开发了这个接口,它只讨论函数而不是结构。 Should the interface definition for structure members be different? 结构成员的接口定义是否应该不同?
It might be that it can't handle an array of structs because of the unknown size/layout in memory (which is not a problem for int/double/etc...). 由于内存中未知的大小/布局(这对于int / double / etc来说不是问题),它可能无法处理结构数组。 Is it possible for you to use a vector of structs instead? 您是否可以使用结构矢量代替? I've had no problems passing vectors of structs or custom objects by simply including std_vector.i
and relevant typemaps, eg: 通过简单地包含std_vector.i
和相关的类型映射,传递结构或自定义对象的向量没有问题,例如:
%include "std_vector.i"
%typemap(MyClassVec) std::vector<MyClass>;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.