1. 简介  

  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可以被自由使用,发布,修改用于商业或非商业中。

2.使用方法学习

  从官网下载了swigwin-2.0.11。

  Swig使用大概有两种方式,一种是配置Vs对外部工具,一种是配置环境变量通过Cmd命令。前一种方法比较简单,Vs2010配置如图。

  

测试使用的步骤和相关的类。

1.新建VC Win32空项目Dll

2. 添加类CMath

 #pragma once
class CMath
{
public:
CMath(void);
~CMath(void);
int Add(int a, int b);
int n;
};

实现文件:

 #include "Math.h"

 CMath::CMath(void)
{
} CMath::~CMath(void)
{
} int CMath::Add(int a, int b)
{
return a+b;
}

Math.cpp

3.添加一个test.i文件,添加代码,注意module名要和编译C++的dll名称一致。

当前页执行外部工具Swig,生成4个文件:Test_wrap.cxx,CMath.cs,MathLib.cs和MathLibPINVOKE.cs。

 %module MathLib
%include <windows.i> %{
#include "Math.h"
%} %include "Math.h"

 /* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.11
*
* This file is not intended to be easily readable and contains a number of
* coding conventions designed to improve portability and efficiency. Do not make
* changes to this file unless you know what you are doing--modify the SWIG
* interface file instead.
* ----------------------------------------------------------------------------- */ #define SWIGCSHARP #ifdef __cplusplus
/* SwigValueWrapper is described in swig.swg */
template<typename T> class SwigValueWrapper {
struct SwigMovePointer {
T *ptr;
SwigMovePointer(T *p) : ptr(p) { }
~SwigMovePointer() { delete ptr; }
SwigMovePointer& operator=(SwigMovePointer& rhs) { T* oldptr = ptr; ptr = ; delete oldptr; ptr = rhs.ptr; rhs.ptr = ; return *this; }
} pointer;
SwigValueWrapper& operator=(const SwigValueWrapper<T>& rhs);
SwigValueWrapper(const SwigValueWrapper<T>& rhs);
public:
SwigValueWrapper() : pointer() { }
SwigValueWrapper& operator=(const T& t) { SwigMovePointer tmp(new T(t)); pointer = tmp; return *this; }
operator T&() const { return *pointer.ptr; }
T *operator&() { return pointer.ptr; }
}; template <typename T> T SwigValueInit() {
return T();
}
#endif /* -----------------------------------------------------------------------------
* This section contains generic SWIG labels for method/variable
* declarations/attributes, and other compiler dependent labels.
* ----------------------------------------------------------------------------- */ /* template workaround for compilers that cannot correctly implement the C++ standard */
#ifndef SWIGTEMPLATEDISAMBIGUATOR
# if defined(__SUNPRO_CC) && (__SUNPRO_CC <= 0x560)
# define SWIGTEMPLATEDISAMBIGUATOR template
# elif defined(__HP_aCC)
/* Needed even with `aCC -AA' when `aCC -V' reports HP ANSI C++ B3910B A.03.55 */
/* If we find a maximum version that requires this, the test would be __HP_aCC <= 35500 for A.03.55 */
# define SWIGTEMPLATEDISAMBIGUATOR template
# else
# define SWIGTEMPLATEDISAMBIGUATOR
# endif
#endif /* inline attribute */
#ifndef SWIGINLINE
# if defined(__cplusplus) || (defined(__GNUC__) && !defined(__STRICT_ANSI__))
# define SWIGINLINE inline
# else
# define SWIGINLINE
# endif
#endif /* attribute recognised by some compilers to avoid 'unused' warnings */
#ifndef SWIGUNUSED
# if defined(__GNUC__)
# if !(defined(__cplusplus)) || (__GNUC__ > || (__GNUC__ == && __GNUC_MINOR__ >= ))
# define SWIGUNUSED __attribute__ ((__unused__))
# else
# define SWIGUNUSED
# endif
# elif defined(__ICC)
# define SWIGUNUSED __attribute__ ((__unused__))
# else
# define SWIGUNUSED
# endif
#endif #ifndef SWIG_MSC_UNSUPPRESS_4505
# if defined(_MSC_VER)
# pragma warning(disable : ) /* unreferenced local function has been removed */
# endif
#endif #ifndef SWIGUNUSEDPARM
# ifdef __cplusplus
# define SWIGUNUSEDPARM(p)
# else
# define SWIGUNUSEDPARM(p) p SWIGUNUSED
# endif
#endif /* internal SWIG method */
#ifndef SWIGINTERN
# define SWIGINTERN static SWIGUNUSED
#endif /* internal inline SWIG method */
#ifndef SWIGINTERNINLINE
# define SWIGINTERNINLINE SWIGINTERN SWIGINLINE
#endif /* exporting methods */
#if (__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
# ifndef GCC_HASCLASSVISIBILITY
# define GCC_HASCLASSVISIBILITY
# endif
#endif #ifndef SWIGEXPORT
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
# if defined(STATIC_LINKED)
# define SWIGEXPORT
# else
# define SWIGEXPORT __declspec(dllexport)
# endif
# else
# if defined(__GNUC__) && defined(GCC_HASCLASSVISIBILITY)
# define SWIGEXPORT __attribute__ ((visibility("default")))
# else
# define SWIGEXPORT
# endif
# endif
#endif /* calling conventions for Windows */
#ifndef SWIGSTDCALL
# if defined(_WIN32) || defined(__WIN32__) || defined(__CYGWIN__)
# define SWIGSTDCALL __stdcall
# else
# define SWIGSTDCALL
# endif
#endif /* Deal with Microsoft's attempt at deprecating C standard runtime functions */
#if !defined(SWIG_NO_CRT_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_CRT_SECURE_NO_DEPRECATE)
# define _CRT_SECURE_NO_DEPRECATE
#endif /* Deal with Microsoft's attempt at deprecating methods in the standard C++ library */
#if !defined(SWIG_NO_SCL_SECURE_NO_DEPRECATE) && defined(_MSC_VER) && !defined(_SCL_SECURE_NO_DEPRECATE)
# define _SCL_SECURE_NO_DEPRECATE
#endif #include <stdlib.h>
#include <string.h>
#include <stdio.h> /* Support for throwing C# exceptions from C/C++. There are two types:
* Exceptions that take a message and ArgumentExceptions that take a message and a parameter name. */
typedef enum {
SWIG_CSharpApplicationException,
SWIG_CSharpArithmeticException,
SWIG_CSharpDivideByZeroException,
SWIG_CSharpIndexOutOfRangeException,
SWIG_CSharpInvalidCastException,
SWIG_CSharpInvalidOperationException,
SWIG_CSharpIOException,
SWIG_CSharpNullReferenceException,
SWIG_CSharpOutOfMemoryException,
SWIG_CSharpOverflowException,
SWIG_CSharpSystemException
} SWIG_CSharpExceptionCodes; typedef enum {
SWIG_CSharpArgumentException,
SWIG_CSharpArgumentNullException,
SWIG_CSharpArgumentOutOfRangeException
} SWIG_CSharpExceptionArgumentCodes; typedef void (SWIGSTDCALL* SWIG_CSharpExceptionCallback_t)(const char *);
typedef void (SWIGSTDCALL* SWIG_CSharpExceptionArgumentCallback_t)(const char *, const char *); typedef struct {
SWIG_CSharpExceptionCodes code;
SWIG_CSharpExceptionCallback_t callback;
} SWIG_CSharpException_t; typedef struct {
SWIG_CSharpExceptionArgumentCodes code;
SWIG_CSharpExceptionArgumentCallback_t callback;
} SWIG_CSharpExceptionArgument_t; static SWIG_CSharpException_t SWIG_csharp_exceptions[] = {
{ SWIG_CSharpApplicationException, NULL },
{ SWIG_CSharpArithmeticException, NULL },
{ SWIG_CSharpDivideByZeroException, NULL },
{ SWIG_CSharpIndexOutOfRangeException, NULL },
{ SWIG_CSharpInvalidCastException, NULL },
{ SWIG_CSharpInvalidOperationException, NULL },
{ SWIG_CSharpIOException, NULL },
{ SWIG_CSharpNullReferenceException, NULL },
{ SWIG_CSharpOutOfMemoryException, NULL },
{ SWIG_CSharpOverflowException, NULL },
{ SWIG_CSharpSystemException, NULL }
}; static SWIG_CSharpExceptionArgument_t SWIG_csharp_exceptions_argument[] = {
{ SWIG_CSharpArgumentException, NULL },
{ SWIG_CSharpArgumentNullException, NULL },
{ SWIG_CSharpArgumentOutOfRangeException, NULL }
}; static void SWIGUNUSED SWIG_CSharpSetPendingException(SWIG_CSharpExceptionCodes code, const char *msg) {
SWIG_CSharpExceptionCallback_t callback = SWIG_csharp_exceptions[SWIG_CSharpApplicationException].callback;
if ((size_t)code < sizeof(SWIG_csharp_exceptions)/sizeof(SWIG_CSharpException_t)) {
callback = SWIG_csharp_exceptions[code].callback;
}
callback(msg);
} static void SWIGUNUSED SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpExceptionArgumentCodes code, const char *msg, const char *param_name) {
SWIG_CSharpExceptionArgumentCallback_t callback = SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentException].callback;
if ((size_t)code < sizeof(SWIG_csharp_exceptions_argument)/sizeof(SWIG_CSharpExceptionArgument_t)) {
callback = SWIG_csharp_exceptions_argument[code].callback;
}
callback(msg, param_name);
} #ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT void SWIGSTDCALL SWIGRegisterExceptionCallbacks_MathLib(
SWIG_CSharpExceptionCallback_t applicationCallback,
SWIG_CSharpExceptionCallback_t arithmeticCallback,
SWIG_CSharpExceptionCallback_t divideByZeroCallback,
SWIG_CSharpExceptionCallback_t indexOutOfRangeCallback,
SWIG_CSharpExceptionCallback_t invalidCastCallback,
SWIG_CSharpExceptionCallback_t invalidOperationCallback,
SWIG_CSharpExceptionCallback_t ioCallback,
SWIG_CSharpExceptionCallback_t nullReferenceCallback,
SWIG_CSharpExceptionCallback_t outOfMemoryCallback,
SWIG_CSharpExceptionCallback_t overflowCallback,
SWIG_CSharpExceptionCallback_t systemCallback) {
SWIG_csharp_exceptions[SWIG_CSharpApplicationException].callback = applicationCallback;
SWIG_csharp_exceptions[SWIG_CSharpArithmeticException].callback = arithmeticCallback;
SWIG_csharp_exceptions[SWIG_CSharpDivideByZeroException].callback = divideByZeroCallback;
SWIG_csharp_exceptions[SWIG_CSharpIndexOutOfRangeException].callback = indexOutOfRangeCallback;
SWIG_csharp_exceptions[SWIG_CSharpInvalidCastException].callback = invalidCastCallback;
SWIG_csharp_exceptions[SWIG_CSharpInvalidOperationException].callback = invalidOperationCallback;
SWIG_csharp_exceptions[SWIG_CSharpIOException].callback = ioCallback;
SWIG_csharp_exceptions[SWIG_CSharpNullReferenceException].callback = nullReferenceCallback;
SWIG_csharp_exceptions[SWIG_CSharpOutOfMemoryException].callback = outOfMemoryCallback;
SWIG_csharp_exceptions[SWIG_CSharpOverflowException].callback = overflowCallback;
SWIG_csharp_exceptions[SWIG_CSharpSystemException].callback = systemCallback;
} #ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT void SWIGSTDCALL SWIGRegisterExceptionArgumentCallbacks_MathLib(
SWIG_CSharpExceptionArgumentCallback_t argumentCallback,
SWIG_CSharpExceptionArgumentCallback_t argumentNullCallback,
SWIG_CSharpExceptionArgumentCallback_t argumentOutOfRangeCallback) {
SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentException].callback = argumentCallback;
SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentNullException].callback = argumentNullCallback;
SWIG_csharp_exceptions_argument[SWIG_CSharpArgumentOutOfRangeException].callback = argumentOutOfRangeCallback;
} /* Callback for returning strings to C# without leaking memory */
typedef char * (SWIGSTDCALL* SWIG_CSharpStringHelperCallback)(const char *);
static SWIG_CSharpStringHelperCallback SWIG_csharp_string_callback = NULL; #ifdef __cplusplus
extern "C"
#endif
SWIGEXPORT void SWIGSTDCALL SWIGRegisterStringCallback_MathLib(SWIG_CSharpStringHelperCallback callback) {
SWIG_csharp_string_callback = callback;
} /* Contract support */ #define SWIG_contract_assert(nullreturn, expr, msg) if (!(expr)) {SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentOutOfRangeException, msg, ""); return nullreturn; } else #include "Math.h" #ifdef __cplusplus
extern "C" {
#endif SWIGEXPORT void * SWIGSTDCALL CSharp_new_CMath() {
void * jresult ;
CMath *result = ; result = (CMath *)new CMath();
jresult = (void *)result;
return jresult;
} SWIGEXPORT void SWIGSTDCALL CSharp_delete_CMath(void * jarg1) {
CMath *arg1 = (CMath *) ; arg1 = (CMath *)jarg1;
delete arg1;
} SWIGEXPORT int SWIGSTDCALL CSharp_CMath_Add(void * jarg1, int jarg2, int jarg3) {
int jresult ;
CMath *arg1 = (CMath *) ;
int arg2 ;
int arg3 ;
int result; arg1 = (CMath *)jarg1;
arg2 = (int)jarg2;
arg3 = (int)jarg3;
result = (int)(arg1)->Add(arg2,arg3);
jresult = result;
return jresult;
} SWIGEXPORT void SWIGSTDCALL CSharp_CMath_n_set(void * jarg1, int jarg2) {
CMath *arg1 = (CMath *) ;
int arg2 ; arg1 = (CMath *)jarg1;
arg2 = (int)jarg2;
if (arg1) (arg1)->n = arg2;
} SWIGEXPORT int SWIGSTDCALL CSharp_CMath_n_get(void * jarg1) {
int jresult ;
CMath *arg1 = (CMath *) ;
int result; arg1 = (CMath *)jarg1;
result = (int) ((arg1)->n);
jresult = result;
return jresult;
} #ifdef __cplusplus
}
#endif

Test_wrap.cxx

 /* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.11
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */ using System;
using System.Runtime.InteropServices; public class CMath : IDisposable {
private HandleRef swigCPtr;
protected bool swigCMemOwn; internal CMath(IntPtr cPtr, bool cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = new HandleRef(this, cPtr);
} internal static HandleRef getCPtr(CMath obj) {
return (obj == null) ? new HandleRef(null, IntPtr.Zero) : obj.swigCPtr;
} ~CMath() {
Dispose();
} public virtual void Dispose() {
lock(this) {
if (swigCPtr.Handle != IntPtr.Zero) {
if (swigCMemOwn) {
swigCMemOwn = false;
MathLibPINVOKE.delete_CMath(swigCPtr);
}
swigCPtr = new HandleRef(null, IntPtr.Zero);
}
GC.SuppressFinalize(this);
}
} public CMath() : this(MathLibPINVOKE.new_CMath(), true) {
} public int Add(int a, int b) {
int ret = MathLibPINVOKE.CMath_Add(swigCPtr, a, b);
return ret;
} public int n {
set {
MathLibPINVOKE.CMath_n_set(swigCPtr, value);
}
get {
int ret = MathLibPINVOKE.CMath_n_get(swigCPtr);
return ret;
}
} }

CMath.cs

 /* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.11
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */ using System;
using System.Runtime.InteropServices; public class MathLib {
}

MathLib.cs

 /* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 2.0.11
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */ using System;
using System.Runtime.InteropServices; class MathLibPINVOKE { protected class SWIGExceptionHelper { public delegate void ExceptionDelegate(string message);
public delegate void ExceptionArgumentDelegate(string message, string paramName); static ExceptionDelegate applicationDelegate = new ExceptionDelegate(SetPendingApplicationException);
static ExceptionDelegate arithmeticDelegate = new ExceptionDelegate(SetPendingArithmeticException);
static ExceptionDelegate divideByZeroDelegate = new ExceptionDelegate(SetPendingDivideByZeroException);
static ExceptionDelegate indexOutOfRangeDelegate = new ExceptionDelegate(SetPendingIndexOutOfRangeException);
static ExceptionDelegate invalidCastDelegate = new ExceptionDelegate(SetPendingInvalidCastException);
static ExceptionDelegate invalidOperationDelegate = new ExceptionDelegate(SetPendingInvalidOperationException);
static ExceptionDelegate ioDelegate = new ExceptionDelegate(SetPendingIOException);
static ExceptionDelegate nullReferenceDelegate = new ExceptionDelegate(SetPendingNullReferenceException);
static ExceptionDelegate outOfMemoryDelegate = new ExceptionDelegate(SetPendingOutOfMemoryException);
static ExceptionDelegate overflowDelegate = new ExceptionDelegate(SetPendingOverflowException);
static ExceptionDelegate systemDelegate = new ExceptionDelegate(SetPendingSystemException); static ExceptionArgumentDelegate argumentDelegate = new ExceptionArgumentDelegate(SetPendingArgumentException);
static ExceptionArgumentDelegate argumentNullDelegate = new ExceptionArgumentDelegate(SetPendingArgumentNullException);
static ExceptionArgumentDelegate argumentOutOfRangeDelegate = new ExceptionArgumentDelegate(SetPendingArgumentOutOfRangeException); [DllImport("MathLib", EntryPoint="SWIGRegisterExceptionCallbacks_MathLib")]
public static extern void SWIGRegisterExceptionCallbacks_MathLib(
ExceptionDelegate applicationDelegate,
ExceptionDelegate arithmeticDelegate,
ExceptionDelegate divideByZeroDelegate,
ExceptionDelegate indexOutOfRangeDelegate,
ExceptionDelegate invalidCastDelegate,
ExceptionDelegate invalidOperationDelegate,
ExceptionDelegate ioDelegate,
ExceptionDelegate nullReferenceDelegate,
ExceptionDelegate outOfMemoryDelegate,
ExceptionDelegate overflowDelegate,
ExceptionDelegate systemExceptionDelegate); [DllImport("MathLib", EntryPoint="SWIGRegisterExceptionArgumentCallbacks_MathLib")]
public static extern void SWIGRegisterExceptionCallbacksArgument_MathLib(
ExceptionArgumentDelegate argumentDelegate,
ExceptionArgumentDelegate argumentNullDelegate,
ExceptionArgumentDelegate argumentOutOfRangeDelegate); static void SetPendingApplicationException(string message) {
SWIGPendingException.Set(new System.ApplicationException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingArithmeticException(string message) {
SWIGPendingException.Set(new System.ArithmeticException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingDivideByZeroException(string message) {
SWIGPendingException.Set(new System.DivideByZeroException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingIndexOutOfRangeException(string message) {
SWIGPendingException.Set(new System.IndexOutOfRangeException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingInvalidCastException(string message) {
SWIGPendingException.Set(new System.InvalidCastException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingInvalidOperationException(string message) {
SWIGPendingException.Set(new System.InvalidOperationException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingIOException(string message) {
SWIGPendingException.Set(new System.IO.IOException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingNullReferenceException(string message) {
SWIGPendingException.Set(new System.NullReferenceException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingOutOfMemoryException(string message) {
SWIGPendingException.Set(new System.OutOfMemoryException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingOverflowException(string message) {
SWIGPendingException.Set(new System.OverflowException(message, SWIGPendingException.Retrieve()));
}
static void SetPendingSystemException(string message) {
SWIGPendingException.Set(new System.SystemException(message, SWIGPendingException.Retrieve()));
} static void SetPendingArgumentException(string message, string paramName) {
SWIGPendingException.Set(new System.ArgumentException(message, paramName, SWIGPendingException.Retrieve()));
}
static void SetPendingArgumentNullException(string message, string paramName) {
Exception e = SWIGPendingException.Retrieve();
if (e != null) message = message + " Inner Exception: " + e.Message;
SWIGPendingException.Set(new System.ArgumentNullException(paramName, message));
}
static void SetPendingArgumentOutOfRangeException(string message, string paramName) {
Exception e = SWIGPendingException.Retrieve();
if (e != null) message = message + " Inner Exception: " + e.Message;
SWIGPendingException.Set(new System.ArgumentOutOfRangeException(paramName, message));
} static SWIGExceptionHelper() {
SWIGRegisterExceptionCallbacks_MathLib(
applicationDelegate,
arithmeticDelegate,
divideByZeroDelegate,
indexOutOfRangeDelegate,
invalidCastDelegate,
invalidOperationDelegate,
ioDelegate,
nullReferenceDelegate,
outOfMemoryDelegate,
overflowDelegate,
systemDelegate); SWIGRegisterExceptionCallbacksArgument_MathLib(
argumentDelegate,
argumentNullDelegate,
argumentOutOfRangeDelegate);
}
} protected static SWIGExceptionHelper swigExceptionHelper = new SWIGExceptionHelper(); public class SWIGPendingException {
[ThreadStatic]
private static Exception pendingException = null;
private static int numExceptionsPending = ; public static bool Pending {
get {
bool pending = false;
if (numExceptionsPending > )
if (pendingException != null)
pending = true;
return pending;
}
} public static void Set(Exception e) {
if (pendingException != null)
throw new ApplicationException("FATAL: An earlier pending exception from unmanaged code was missed and thus not thrown (" + pendingException.ToString() + ")", e);
pendingException = e;
lock(typeof(MathLibPINVOKE)) {
numExceptionsPending++;
}
} public static Exception Retrieve() {
Exception e = null;
if (numExceptionsPending > ) {
if (pendingException != null) {
e = pendingException;
pendingException = null;
lock(typeof(MathLibPINVOKE)) {
numExceptionsPending--;
}
}
}
return e;
}
} protected class SWIGStringHelper { public delegate string SWIGStringDelegate(string message);
static SWIGStringDelegate stringDelegate = new SWIGStringDelegate(CreateString); [DllImport("MathLib", EntryPoint="SWIGRegisterStringCallback_MathLib")]
public static extern void SWIGRegisterStringCallback_MathLib(SWIGStringDelegate stringDelegate); static string CreateString(string cString) {
return cString;
} static SWIGStringHelper() {
SWIGRegisterStringCallback_MathLib(stringDelegate);
}
} static protected SWIGStringHelper swigStringHelper = new SWIGStringHelper(); static MathLibPINVOKE() {
} [DllImport("MathLib", EntryPoint="CSharp_new_CMath")]
public static extern IntPtr new_CMath(); [DllImport("MathLib", EntryPoint="CSharp_delete_CMath")]
public static extern void delete_CMath(HandleRef jarg1); [DllImport("MathLib", EntryPoint="CSharp_CMath_Add")]
public static extern int CMath_Add(HandleRef jarg1, int jarg2, int jarg3); [DllImport("MathLib", EntryPoint="CSharp_CMath_n_set")]
public static extern void CMath_n_set(HandleRef jarg1, int jarg2); [DllImport("MathLib", EntryPoint="CSharp_CMath_n_get")]
public static extern int CMath_n_get(HandleRef jarg1);
}

MathLibPINVOKE.cs

4.在Vc项目中添加Test_wrap.cxx,编译。生成dll :MathLib.dll。拷贝到新建的C#项目运行目录下。
5.在C#项目中添加CMath.cs,MathLib.cs和MathLibPINVOKE.cs,可以直接编译C#的Dll,也可以直接调用CMath的CLR类。

C#调用代码:

   private void button1_Click(object sender, EventArgs e)
{
CMath math = new CMath();
int c=math.Add(, );
MessageBox.Show(c.ToString()); }

SWIG和PInvoke学习(1)的更多相关文章

  1. [原创]阿里云RocketMQ踩过的哪些坑

    由于公司的最近开始使用RocketMQ来做支付业务处理, 便开启了学习阿里云RocketMQ的学习与实践之路, 其中踩了不少的坑, 大部份是由于没有仔细查看阿里云的技术文档而踩的坑. 但是有一个非常大 ...

  2. 公共语言运行库(CLR)开发系列课程(1):Pinvoke 简介 学习笔记

    前言 让拖管代码对象和非托管对象协同工作的过程称为互用性(Interoperability),通常简称为 Interop. P/Invoke在托管代码与非托管代码交互式时产生一个事务(Transiti ...

  3. 学习笔记:安装swig+用SWIG封装C++为Python模块+SWIG使用说明

    这段时间一直在摸索swing,用它来封装C++代码来生成python脚步语言.并总结了swing从安装到配置再到代码封装编译生成动态库的整个过程,下面这篇文章都是我在实际的运用中的一些经验总结,分享给 ...

  4. 公共语言运行库(CLR)开发系列课程(2):Pinvoke 进阶 学习笔记

    上一章地址 API版本 具有字符串参数的API通常有两种版本 GetWindowText GetWindowTextA GetWindowTextW 缺省情况下CLR会自动寻找合适的匹配 CharSe ...

  5. c#利用SWIG调用c++dll学习总结【转】

    开发环境: 操作系统:windows 7 IDE:Microsoft Visual Studio Professional 2015 SWIG: 3.0.12 swig的介绍 详细介绍可看官网,一下贴 ...

  6. [Python & Machine Learning] 学习笔记之scikit-learn机器学习库

    1. scikit-learn介绍 scikit-learn是Python的一个开源机器学习模块,它建立在NumPy,SciPy和matplotlib模块之上.值得一提的是,scikit-learn最 ...

  7. 深度学习主机环境配置: Ubuntu16.04 + GeForce GTX 1070 + CUDA8.0 + cuDNN5.1 + TensorFlow

    深度学习主机环境配置: Ubuntu16.04 + GeForce GTX 1070 + CUDA8.0 + cuDNN5.1 + TensorFlow 最近在公司做深度学习相关的学习和实验,原来一直 ...

  8. H264解码学习-2015.04.16

    今天看了不少,却感觉收获寥寥. 1.H264相关知识 因为RTP协议发过来的数据已经经过了H264编码,所以这边需要解码.补充一下H264的相关知识. 与以往的视频压缩标准相比,H.264 视频压缩标 ...

  9. PInvoke和Marshal的姿势

    PInvoke http://www.mono-project.com/docs/advanced/pinvoke/ https://msdn.microsoft.com/en-us/library/ ...

随机推荐

  1. 浅谈cookie测试

    Cookie 提供了一种在Web 应用程序中存储用户特定信息的方法,例如存储用户的上次 访问时间等信息.假如不进行cookie存储一个网站的用户行为,那么可能会造成以下问题:用户进行购买几件商品转到结 ...

  2. Unity5.5+easytouch5双摇杆控制角色移动

    第一步:新建两个Joystick,分别改名LeftJoyStick和RightJoyStick 在LeftJoyStick的ETC Joystick-Axes properties中的Horizont ...

  3. WPF图片模糊的解决之路

    设计稿转为xaml后,设计师开始review UI了,发现图片都模糊了. 这一张很神奇,三个图片都是同一张,中间的那个最清楚,上面的这个左右两边清楚,下面的那个四个边都不清楚. 这一张,右边是原图,左 ...

  4. 工作流JBPM_day01:4-管理流程定义

    工作流JBPM_day01:4-管理流程定义 管理流程(流程定义) 部署(添加) 查询 删除 查看流程图(xxx.png) -- 修改 --> 没有真正的修改,而是使用“再次部署+使用最新版本启 ...

  5. docker search 详解

    docker search 用于从 Docker Hub(https://hub.docker.com)中搜索指定的镜像,用法如下: [root@localhost ~]$ docker search ...

  6. iOS - 布局重绘机制相关方法的研究

    iOS View布局重绘机制相关方法 布局 - (void)layoutSubviews - (void)layoutIfNeeded- (void)setNeedsLayout —————————— ...

  7. css - 三种方法解决LI和内部Img的上下间距问题

    在火狐浏览器和谷歌浏览器(qq浏览器,谷歌内核)bug类似这张图: img的高度是190*127 但是放到li中,li并没有设置高度,却和内部的图片之间上下错位. 若强行给li设置高度127,他和im ...

  8. for update 和 t.rowid的区别

    select * from table_name for update; 和 select t.*, t.rowid from table_name t 的区别 前者会对你查询出来的结果加上锁,而后者 ...

  9. [Gradle] 输出构建 APK 的版本名到文件

    android { // 输出版本名到 build 目录下的 version_name.txt 文件 applicationVariants.all { variant -> project.t ...

  10. docker swarm+register-web+shipyard搭建

    1.swarm安装 swarm安装有很多种服务注册的方式,token.etcd.zookeeper,本文主要以swarm默认的token方式进行安装.因为最新的docker已经集成了swarm,所以从 ...