SWIG (Simplified Wrapper and Interface Generator)
SWIG是个帮助使用C或者C++编写的软件能与其它各种高级编程语言进行嵌入联接的开发工具。SWIG能应用于各种不同类型的语言包括常用脚本编译语言例如Perl, PHP, Python, Tcl, Ruby and PHP。支持语言列表中也包括非脚本编译语言,例如C#, Common Lisp (CLISP, Allegro CL, CFFI, UFFI), Java, Modula-3, OCAML以及R,甚至是编译器或者汇编的计划应用(Guile, MzScheme, Chicken)。SWIG普遍应用于创建高级语言解析或汇编程序环境,用户接口,作为一种用来测试C/C++或进行原型设计的工具。SWIG还能够导出XML或Lisp s-expressions格式的解析树。SWIG可以被自由使用,发布,修改用于商业或非商业中。
C++ C#数组互操作
from 20.4.2 Managed arrays using P/Invoke default array marshalling
In the P/Invoke default marshalling scheme, one needs to designate whether the invoked function will treat a managed array parameter as input, output, or both. When the function is invoked, the CLR allocates a separate chunk of memory as big as the given managed array, which is automatically released at the end of the function call. If the array parameter is marked as being input, the content of the managed array is copied into this buffer when the call is made. Correspondingly, if the array parameter is marked as being output, the contents of the reserved buffer are copied back into the managed array after the call returns. A pointer to this buffer is passed to the native function.
The reason for allocating a separate buffer is to leave the CLR free to relocate the managed array object during garbage collection. If the overhead caused by the copying is causing a significant performance penalty, consider pinning the managed array and passing a direct reference as described in the next section.
C#向C++传递数组时,CLR会创建数组的副本,这会影响效率,此时可以考虑pin数组和传递引用
*强调内容*from 20.4.3 Managed arrays using pinning
using pinning, thus avoiding the CLR making copies of the arrays passed as parameters.
On the method signature level the only difference to the version using P/Invoke default marshalling is the “unsafe” quantifier, which is required because we are handling pointers.
Also the intermediary class method looks a little different from the default marshalling example - the method is expecting an IntPtr as the parameter type.
实质上就是传指针