C# calls C++ callback function

Reprint address: http://www.cnblogs.com/dipwater/archive/2012/11/07/dipwater_delegate.html

Similar reference to:

http://www.cnblogs.com/cssmystyle/archive/2012/03/15/2397524.html

1. Define the delegate corresponding to the callback function

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

  2. The parameter is defined as Delegate

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

  3. Create an instance of the delegate

1  private RealCallback realCallBack = null ;

  4. Define the function corresponding to the delegate

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

  5. Assign a value to a delegate

1 realCallBack = RealCallbackFun;

  6. Use delegation

1 StartRealPlay(handle, realCallBack);

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324030083&siteId=291194637