swig实现c#和c++之间的函数调用

背景

  1. 用swig编写胶水代码
  2. c#加载dll(或者其他c++的库,比如.a .so),然后c++回调c#中的方法

实现

c层实现

typedef void(*pCBFunc)(bool success);

extern "C" {
    virtual void CPP_InitData(pCBFunc cb);
}

c#层实现

public class PointerTest
{
    public delegate void InitDelegate(bool success);
    public SWIGTYPE_p_f_bool_void m_initDelegate = null;

    public void Work(InitDelegate cb)
    {
        m_initDelegate = new SWIGTYPE_p_f_bool_void(Marshal.GetFunctionPointerForDelegate(cb), false);
        CPP_InitData(m_initDelegate);
    }
}
posted @ 2022-03-16 10:39  二律背反GG  阅读(329)  评论(0编辑  收藏  举报