C# 调用C++回调函数

转载自:http://www.cnblogs.com/dipwater/archive/2012/11/07/dipwater_delegate.html

C# 调用C++回调函数过程如下:

1. 定义回调函数对应的委托

1 [UnmanagedFunctionPointerAttribute(CallingConvention.StdCall, CharSet = CharSet.Ansi)]
2 public delegate void RealCallback(int hRealHandle, int dwDataType, IntPtr buffer, uint dwSize, IntPtr lpVoid);

  2. 参数定义为 Delegate,DllImport可参考http://blog.csdn.net/sibaison/article/details/70194273

1 [DllImport(DvxSdkPath, EntryPoint = "DVR_StartRealPlay", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
2 public static extern int StartRealPlay(int hUserHandle, Delegate callback);

  3. 创建一个委托的实例

1 private RealCallback realCallBack = null;

  4. 定义委托对应的函数

1 private void RealCallbackFun(int hRealHandle, int dwDataType, IntPtr lpReal, uint dwSize, IntPtr lpVoid)
2 {
3       
4 }

  5. 给委托赋值

1 realCallBack = RealCallbackFun;

  6. 使用委托

1 StartRealPlay(handle, realCallBack);


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