如何使用int数组和List 作为使用SWIG时从C#向C ++传递的参数

问题描述 投票:-1回答:1

使用SWIG,我需要将List和int数组作为参数传递给C ++函数。我尝试了以下代码

'''CPP接口代码

%module (directors="1") CppTestApp


%{
    #include "TestClass.h"

    #include "TestDataClass.h"
%}


 %include <windows.i>
 %include <std_list.i>
 %include <std_string.i>
 %include "arrays_csharp.i"

%feature("director") Base;

%include "TestClass.h"
%include "TestDataClass.h"

'''

但是我得到的参数为SWIGTYPE_p_std__listT_std__string_tSWIGTYPE_p_double我无法将List分配给上述变量。有人可以帮助解决这个问题吗?

c# c++ swig
1个回答
0
投票

我获得了如何使用Swig将数组从C#传递到C ++的答案。请在下面查看

'''SWIG接口代码

%module (directors="1") CppTestApp

%{
    #include "TestClass.h"

    #include "TestDataClass.h"
%}

%include <windows.i>
%include <std_string.i>

%include <arrays_csharp.i>
CSHARP_ARRAYS(char *, string)

%typemap(imtype, inattributes="[In, global::System.Runtime.InteropServices.MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0, ArraySubType=UnmanagedType.LPStr)]") char * INPUT[] "string[]"

%apply char * INPUT[]  { char * argv  [], char * argvqq  [] }
%apply int INPUT[]  { int sourceArray [] }
%apply double INPUT[]  { double sourcedoubleArray [] }
%apply char INPUT[]  { char chArray [] }
%apply bool INPUT[]  { bool bArray [] }
%apply long INPUT[]  { long lArray [] }
%apply float INPUT[]  { float fArray [] }

%feature("director") Base;

%include "TestClass.h"
%include "TestDataClass.h"

'''

'''C ++代码

class TestClass
{
public:

    int times2(int sourceArray[], double sourcedoubleArray[], char* argv[], char chArray[], bool bArray[], float fArray[], long lArray[]);

};

'''

'''生成的SWIG C#代码

[global::System.Runtime.InteropServices.DllImport("CppTestApp", EntryPoint="CSharp_TestClass_times2")]
  public static extern int TestClass_times2(global::System.Runtime.InteropServices.HandleRef jarg1, [global::System.Runtime.InteropServices.In, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray)]int[] jarg2, [global::System.Runtime.InteropServices.In, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray)]double[] jarg3, [In, global::System.Runtime.InteropServices.MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0, ArraySubType=UnmanagedType.LPStr)]string[] jarg4, string jarg5, [global::System.Runtime.InteropServices.In, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray,ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]bool[] jarg6, [global::System.Runtime.InteropServices.In, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray)]float[] jarg7, [global::System.Runtime.InteropServices.In, global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPArray)]int[] jarg8);

'''

© www.soinside.com 2019 - 2024. All rights reserved.