C#调用C回调函数后,程序奔溃问题

来源:互联网 发布:手机打网络电话软件 编辑:程序博客网 时间:2024/05/17 21:53

原始代理声明

delegate void DlgVideoStreamCallBack(IntPtr pData, int size, int height, int width, IntPtr pUserData);

C函数导入

 [DllImport("peerclient.dll", CallingConvention = CallingConvention.Cdecl, EntryPoint = "Webrtc_ResigsterLocalVideoCallBack")]public static extern void Webrtc_ResigsterLocalVideoCallBack(DlgVideoStreamCallBack callback, int thread_id);

回调函数

 static void LocalVideoStreamCallBack(IntPtr pData, int size, int height, int width, IntPtr pUserData){}

使用上面的方式,会导致C#在调用完后,释放pData内容,导致C程序崩溃

所以在声明代理的时候,说明是C回调,不回收里面资源
修改后代理

 [System.Runtime.InteropServices.UnmanagedFunctionPointerAttribute(System.Runtime.InteropServices.CallingConvention.Cdecl)]    delegate void DlgVideoStreamCallBack(IntPtr pData, int size, int height, int width, IntPtr pUserData);
0 0
原创粉丝点击