c# - 为包含 void* 参数的函数创建 SWIG C# 包装器

标签 c# c interface wrapper swig

我正在尝试为 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/

相关文章:

c# - 强名称程序集 - 必须更改数千个程序集

c# - TranslateTransform 取消 ScaleTransform

c - 基于低延迟中断的传输代码中的不当行为

go - 不能使用函数(类型 func())作为参数类型

database - CouchDB 和 Amazon S3 的通用接口(interface)

c# - 如何为具有身份列的 NHibernate 实体实现 GetHashCode?

c# - 确定应用程序位置的正确方法是什么?

linux - 如何在ubuntu上使用伯克利数据包过滤器(BPF)

c - 仅当上次 CURL 超过 10 秒前才执行 CURL

xml - 使用 Go 将 XML 解码为接口(interface)类型?