swig in c# - HandleRef could not be found in portable class library

不问归期 提交于 2019-12-01 12:02:53

This question is a bit older, but having had the same problem when using .NET Core I thought I'd share my Solution.


Change the imtype and csbody

To tell SWIG to stop using HandleRef you have to change the %typemap(imtype) and %typemap(csbody) of all default (or of specific) types.

imtype specifies the type that appears in your modulenamePINVOKE method parameters. Change it to something that can be marshaled from/to a pointer type.

csbody replaces the entire body of your SWIGTYPE_ classes, meaning you'll have to implement you own (You have to, to change the variable that is stored as HandleRef). If your new implementation doesn't have a getCPtr method you have to change %typemap(csin) as well

Here is an example that uses System.IntPtr instead of HandleRef, place it at the top in your interface file:

%typemap(imtype) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [], SWIGTYPE (CLASS::*) "System.IntPtr"
%typemap(csin) SWIGTYPE, SWIGTYPE *, SWIGTYPE &, SWIGTYPE [], SWIGTYPE (CLASS::*) "$csinput.Pointer"

%typemap(csbody) SWIGTYPE *, SWIGTYPE &, SWIGTYPE [], SWIGTYPE (CLASS::*) %{

  private volatile System.IntPtr swigCPtr;

  protected $csclassname() 
  {
    swigCPtr = System.IntPtr.Zero;
  }

  internal System.IntPtr Pointer
  {
    get
    {
      return swigCPtr;
    }
  }
%}

Note: SWIGTYPE is a placeholder for any type.

Reference: SWIG 3.0 Documentation - Chapter 20 "SWIG and C#"

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!