SWIG (WIN) C#封装C++的动态(dll)或静态库(lib)
方法一、VS2010
第一步、准备
1:准备好swig (Download SWIG)
1:准备好需要封装的dll \lib与源代码
http://www.swig.org/Doc1.3/SWIG.html#SWIG_nn2
第二步、获取封装所需的源代码
一、新建及配置
1、新建Win32工程(封装工程)
2、配置工程属性
预编译头
命令行:
E:\soft\swig\swigwin-2.0.9\swigwin-2.0.9\swigwin-2.0.9\swig-c++ -csharp -o test_wrap.cpp -namespace "Xiao.Test" -outdirF:\Work_doc\test -dllimport swig-test test.i
Ø E:\soft\swig\swigwin-2.0.9\swigwin-2.0.9\swigwin-2.0.9\swig:swig.exe
Ø -c++:源语言
Ø -csharp:目标语言
Ø -o test_wrap.cpp:中间文件(test_wrap.cpp自定义)
Ø -namespace"Xiao.Test":封装后的命名空间("Xiao.Test" 自定义)
Ø -outdir F:\Work_doc\test:.cs文件生成目录(F:\Work_doc\test自定义)
Ø -dllimport swig-test :封装后的库名称
Ø test.i:此命令所操作的文件
添加依赖项(需要封装的dll或lib)
3、删除所有文件,只留下一个空工程
二、编译生成*_wrap.cpp
1、添加
2、编辑*.i文件
文件内容:
%module Test
%include<windows.i>
%{
#include"libXiao.h"
%}
%include "c:\Users\Hos\Documents\VisualStudio 2010\Projects\lib-test\lib-test\libXiao.h"
3、编译
生成test_wrap.cpp
三、编译生成 (.hFileName).cs、 (.iFileName).cs、 (.iFileName)PINVOKE.cs
1、拷贝
拷贝所有.h到封装工程下
2、添加
添加test_wrap.cpp到封装工程下
3、编译
生成 TestPINVOKE.cs、Test.cs、libXiao.cs、hosTest.cs、
第三步、构建封装类
一、创建工程
二、删除生成的cs文件,添加生成的cs
修改工程属性-》默认命名空间
编译生成
方法二、批处理
准备swig
编写三个文件clear.bat、makefile.bat、mydll.i
1、my_dll.i文件
%module MY_DLL %{ #include "../Def.h" #include "../DBStruct.h" %} %include <arrays_csharp.i> typedef unsigned long DWORD; typedef int BOOL; typedef unsigned char BYTE; typedef unsigned short WORD; typedef std::vector<CAE_CoordinateEntity *> CAECoordinateList; typedef std::list<void *> DATAList; %define %cs_marshal_array(TYPE, CSTYPE) %typemap(ctype) TYPE[] "void*" %typemap(imtype, inattributes="[MarshalAs(UnmanagedType.LPArray)]") TYPE[] "CSTYPE[]" %typemap(cstype) TYPE[] "CSTYPE[]" %typemap(in) TYPE[] %{ $1 = (TYPE*)$input; %} %typemap(csin) TYPE[] "$csinput" %enddef %cs_marshal_array(bool, bool) %cs_marshal_array(short, short) %cs_marshal_array(unsigned short, ushort) %cs_marshal_array(int, int) %cs_marshal_array(unsigned int, uint) %cs_marshal_array(long, int) %cs_marshal_array(unsigned long, uint) %cs_marshal_array(long long, long) %cs_marshal_array(unsigned long long, ulong) %cs_marshal_array(float, float) %cs_marshal_array(double, double) typedef wchar_t TCHAR; %include "wchar.i" // in swig lib // some fixes %typemap(csvarout, excode=SWIGEXCODE2) wchar_t * %{ get { string ret = System.Runtime.InteropServices.Marshal.PtrToStringUni($imcall);$excode return ret; } %} %typemap(csdirectorin) wchar_t * "System.Runtime.InteropServices.Marshal.PtrToStringUni($iminput)" %typemap(directorin) wchar_t const * "$input = ($1_ltype)$1;" //%typemap(in) wchar_t * %{ teest %} %include <windows.i> %typemap(cstype) float & "out float" %typemap(csin) float & %{out $csinput%} %typemap(imtype) float & "out float" %typemap(cstype) double & "out double" %typemap(csin) double & %{out $csinput%} %typemap(imtype) double & "out double" %include"../Def.h" %include "../DBStruct.h" |
例子
I have a C++library which provides the following enum and function:
typedef enum en{
a,
b
}myEnum;
int myFunction( myEnum &varToSet )
{
varToSet = 1;
return 0;
}
The function inthe C# Wrapper should look something like this:
public static int myFunction( ref myEnum varToSet )
I have tried toget this result by following typemap in the Swig interface file:
%typemap(cstype) myEnum & "ref myEnum"
%typemap(csin) myEnum & %{ref $csinput%}
Swig changed thetype from SWIGTYPE_p_myEnum to ref myEnum in Wrapper.cs, but notin WrapperPINVOKE.cs. What am I missing here?
2、makefile.bat
set c1= E:\soft\swig\swigwin-2.0.9\swigwin-2.0.9\swigwin-2.0.9 \swig.exe set flag=-c++ -csharp %c1% %flag% -o mydll_Wrap.cpp -namespace "MD" -dllimport MY_DLL my_dll.i pause |
set c1= E:\soft\swig\swigwin-2.0.9\swigwin-2.0.9\swigwin-2.0.9\swig.exe 指定程序
mydll_Wrap.cpp 自定义
"MD"自定义
MY_DLL自定义
my_dll.i指定文件
3、clear.bat
del *.cs del *.cpp |