c# DLL接口回调函数

来源:互联网 发布:易语言源码大全下载 编辑:程序博客网 时间:2024/04/30 20:30

一:回调函数

1:DLL内部

    //声明回调函数
    typedef void (CALLBACK* ONSERIESDATA)(BYTE* buf, DWORD dwBufLen, DWORD dwTime, bool bDown);

    //DLL接口中的传入回到函数
    extern "C" __declspec(dllexport) void WINAPI Enable_Listening(ONSERIESDATA pSeriesCallBack);

    //引入地址
    void WINAPI Enable_Listening(ONSERIESDATA pSeriesCallBack)
    {
        gSeriesCallBack = pSeriesCallBack;
    }

    //回调函数调用
    if (gSeriesCallBack != NULL)
    {
        gSeriesCallBack(frameBuf, currentFrameLen, time, false);
    }

2:C# 上层

    //声明委托,
    public delegate void ONSERIESDATA(IntPtr buf, UInt32 dwBufLen, UInt32 dwTime, Boolean bDown);

    //导出接口
    [DllImport(strDllPath)]
    public static extern void Enable_Listening(ONSERIESDATA pSeriesCallBack);

    //函数邦定
    private RSUComm.ONSERIESDATA pSeriesCallBack = new RSUComm.ONSERIESDATA(ShowSeriesMessage);

    //注意,静态函数不可以直接更新控件,需借助UpdateControlEventHandler
    private delegate void UpdateControlEventHandler(Object sender, FireEventArgs e);
    private static event UpdateControlEventHandler UpdateListViewControl;
    UpdateListViewControl += new UpdateControlEventHandler(this.UpdateListView);
    private static void ShowSeriesMessage(IntPtr pBuf, UInt32 iBufLen, UInt32 iTime, Boolean bDown)
    {
        UpdateListViewControl(g_Form.pubSeriesListeningForm, new FireEventArgs(StringOut));
    }
   
    //显示信息
    public void UpdateListView(Object o, FireEventArgs e)  //事件处理函数,用来更新控件
    {
        delegateUpdateListViewTagResult(e.room);
    }

    //停止回调
    RSUComm.Enable_Listening(null);

    //启用回调
    RSUComm.Enable_Listening(pSeriesCallBack);

    //声明类
    public class FireEventArgs : EventArgs
    {
        public FireEventArgs(string room)
        {
            this.room = room;
        }
        public string room;
    }

原创粉丝点击