c# DLL接口回调函数

 一:回调函数

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;
    }

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chpdirector84/archive/2009/09/29/4616152.aspx

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页