【问题标题】:How to Pass an int array and a List<string> as argument to C++ from C# when using SWIG使用 SWIG 时如何将 int 数组和 List<string> 作为参数从 C# 传递给 C++
【发布时间】:2020-01-09 09:57:31
【问题描述】:

使用 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_t & SWIGTYPE_p_double 我无法将 List 分配给上述变量。有人可以帮忙解决这个问题吗?

【问题讨论】:

    标签: c# c++ swig


    【解决方案1】:

    我得到了如何使用 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);
    

    '''

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 2015-11-20
      • 2016-06-28
      • 2012-11-23
      • 2019-08-05
      • 1970-01-01
      相关资源
      最近更新 更多