[英]How to Pass an int array and a List<string> as argument to C++ from C# when using SWIG
使用 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 分配給上述變量。 有人可以幫助解決這個問題嗎?
我得到了如何使用 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);
'''
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.