C/C++教程

调用c++写的Dll文件并且有回调的写法

本文主要是介绍调用c++写的Dll文件并且有回调的写法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

其中回调函数为 RenderCallback p_func

/// <summary>
        /// 初始化
        /// </summary>
        /// <param name="p_enterise_id">企业ID</param>
        /// <param name="p_settings">设备属性</param>
        /// <param name="p_device_infos">获取到设备信息 JSON</param>
        /// <param name="p_server_url">预留的server URL地址,目前可设置为"https://evn.xjnetworks.com/" </param>
        /// <param name="p_func">回调函数接口</param>
        /// <returns></returns>
        [DllImport("xjsdk.dll", EntryPoint = "XJSdkInit", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern int XJSdkInit(string p_enterise_id, string p_settings, string p_device_infos, string p_server_url, RenderCallback p_func);

1定义委托类:

  /// <summary>
        /// 设定委托
        /// </summary>
        /// <param name="event_type"></param>
        /// <param name="p_data"></param>
        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        public delegate void RenderCallback(int event_type, string p_data);
 public static RenderCallback RemoteMsgCallBack;///实例化
        public static void RegistRemoteMsgCallBack(string p_enterise_id, string p_settings, string p_device_infos, string p_server_url, RenderCallback p_func)
        {
           XJSdkInit(p_enterise_id, p_settings, p_device_infos, p_server_url, RemoteMsgCallBack); 
        }///重写操作

程序端调用方法:

 DllBaseSDK.RemoteMsgCallBack = CallBackFunction;
    DllBaseSDK.RegistRemoteMsgCallBack(Mmsg.QiyeId, Mmsg.SBName, MsgAll, Url, CallBackFunction);

也可以是这样的:

 public RenderCallback globalCallbackProcedure;//全局调用委托
 
 globalCallbackProcedure = new RenderCallback(CallBackFunction);//loading里写
 
  DllBaseSDK.RegistRemoteMsgCallBack(Mmsg.QiyeId, Mmsg.SBName, MsgAll, Url, globalCallbackProcedure );//最后是调用
这篇关于调用c++写的Dll文件并且有回调的写法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!