Как передать массив int и список <string>в качестве аргумента C ++ из C# при использовании SWIG - PullRequest
0 голосов
/ 09 января 2020

Используя 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 указанной выше переменной. Может кто-нибудь помочь решить это?

1 Ответ

0 голосов
/ 15 января 2020

Я получил ответ как передать массив из C# в C ++ с помощью swig. См. Ниже

'' 'Код интерфейса 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);

'' '

...