我正在尝试为 C .lib 创建一个 C# 包装器,其中包含使用 SWIG 获取 void 指针的函数。
int inputPointExample(void* input);
int outputPointerExample(void* output);
默认情况下,SWIG 不处理 void 指针转换,您必须以某种方式使用类型映射。我找到了这个页面 -> http://www.nickdarnell.com/2011/05/swig-and-a-miss/ Number 9 表示使用以下类型映射来处理 void 指针...
%typemap(ctype) void * "void *"
%typemap(imtype) void * "IntPtr"
%typemap(cstype) void * "IntPtr"
%typemap(csin) void * "$csinput"
%typemap(in) void * %{ $1 = $input; %}
%typemap(out) void * %{ $result = $1; %}
%typemap(csout) void * { return $imcall; }
当我尝试在该函数的 exampleVectorType.cs 中遇到编译错误...
public IntPtr pData {
set {
examplePINVOKE.ExampleVectorType_pData_set(swigCPtr, value);
}
get {
global::System.IntPtr cPtr = examplePINVOKE.ExampleVectorType_pData_get(swigCPtr);
SWIGTYPE_p_void ret = (cPtr == global::System.IntPtr.Zero) ? null : new SWIGTYPE_p_void(cPtr, false);
return ret; //Compile error occurs here
}
}
我得到了-
Cannot implicitly convert type 'SWIGTYPE_p_void' to 'System.IntPtr'
据我所知,许多其他人也遇到了这个问题,并且只有几个关于如何解决这个问题的糟糕例子。有人可以帮我吗?
最佳答案
我已经尝试过
// c++
void* GetRawPtr()
{
return (void*)_data;
}
痛饮
// swig generated c#
// Example.cs
IntPtr GetRawPtr()
{
return examplePINVOKE.Example_GetRawPtr(swigCPtr);
}
// examplePINVOKE.cs
[global::System.Runtime.InteropServices.DllImport("example", EntryPoint="CSharp_Example_GetRawPtr")]
public static extern IntPtr Example_GetRawPtr(global::System.Runtime.InteropServices.HandleRef jarg1);
如果删除以下行,那么我会得到您的代码:
%typemap(csout) void * { return $imcall; }
也许 SWIG 不能很好地支持属性的类型映射?如果您不为 pData 使用 get/set 属性,它应该可以工作(因为我已经让它为我工作了)
关于c# - 为包含 void* 参数的函数创建 SWIG C# 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25310530/