本文介绍了创建函数SWIG C#包装包含void *的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个包含函数需要使用痛饮拿一个空指针是C的.lib一个C#包装。

I'm trying to create a C# wrapper for a C .lib that contains functions take take a void pointer using SWIG.

int inputPointExample(void* input);
int outputPointerExample(void* output);

默认情况下痛饮不处理空指针的转换,你必须以某种方式使用typemaps。我发现这个页面 - >
9号说,使用下面的typemaps处理空指针...

By default SWIG doesn't handle void pointer conversions, you have to use typemaps somehow. I found this page -> http://www.nickdarnell.com/2011/05/swig-and-a-miss/Number 9 says to use the following typemaps to handle void pointers...

%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得到一个编译错误此功能...

When I try that I get a compile error in my exampleVectorType.cs in this function...

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
    }
}



我 -

I got-

Cannot implicitly convert type 'SWIGTYPE_p_void' to 'System.IntPtr'

这是我能找到,还有许多人有这个问题,以及有的只是关于如何解决这几个可怜的例子。有人可以帮助我在这里?

From what I was able to find, many others are having problems with this as well and there are only a few poor examples on how to fix this. Can someone help me out here?

推荐答案

我和

// c++
void* GetRawPtr()
{
    return (void*)_data;
}



这swigs到

which swigs to

// 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);

如果下面的行被删除,然后我让你有代码:

if the following line is removed, then i get the code you had:

%typemap(csout)  void * { return $imcall; }



也许痛饮不支持使用性能typemapping好?如果你不为你的pData使用get /设置属性,它应该工作(因为我已经得到它为我工作)

perhaps SWIG doesn't support typemapping well with properties? If you don't use get/set properties for your pData, it should work (as I've gotten it to work for me)

这篇关于创建函数SWIG C#包装包含void *的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 01:07