亲宝软件园·资讯

展开

C++转C#函数代码 c++函数转c#函数示例程序分享

人气:0
想了解c++函数转c#函数示例程序讲解的相关内容吗,在本文为您仔细讲解C++转C#函数代码的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:C#函数,c++函数,下面大家一起来学习吧。

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace FDEP
{
    /// <summary>
    /// 罗旭成
    /// 深证通函数转换
    /// </summary>
    public class ImportDLL
    {
        #region * 常量定义
        //*****************************************************************
        //协议类型常量
        //名称                             定义值     说明
        //MR_PROTOCOLTYPE_MRSTANDAND       0x01      FDEP规定的标识业务协议
        //MR_PROTOCOLTYPE_SELFCUSTOM       0xFF      用户自定义协议类型
        /// <summary>
        /// 协议类型常量
        /// </summary>
        public const uint MR_PROTOCOLTYPE_MRSTANDAND = 0x01; // SBSP标准业务协议。
        public const uint MR_PROTOCOLTYPE_SELFCUSTOM = 0xFF; // 用户自定义协议类型。
        //*****************************************************************
        //消息标识位常量
        //名称                             定义值     说明
        //MR_MSGFLAG_PERSIST               0x01      持久消息标志,用于可靠传输。目前暂不支持
        //MR_MSGFLAG_COMPRESS              0x02      压缩标志,需进行压缩传输
        /// <summary>
        /// 消息标志位常量
        /// </summary>

        public const uint MR_MSGFLAG_PERSIST = 0x01; // 持久消息标志,用于可靠传输。
        public const uint MR_MSGFLAG_COMPRESS = 0x02; // 压缩标志,需进行压缩传输。
        //*****************************************************************
        //长度常量
        //名称                             定义值     说明
        //MR_MAXLEN_ADDR                   64        用户标识及应用标识的最大长度
        //MR_MAXLEN_PKGID                  64        消息包标识的最大长度
        //MR_MAXLEN_USERDATA               256       用户保留数据的最大长度
        //MR_FIXLEN_EXPIREDABSTIME         20        过期绝对时间固定长度
        /// <summary>
        /// 消息标志位常量
        /// </summary>
        public const int MR_MAXLEN_ADDR = 64; // 用户标识及应用标识的最大长度。
        public const int MR_MAXLEN_PKGID = 64; // 消息包标识的最大长度。
        public const int MR_MAXLEN_USERDATA = 256; // 用户保留数据的最大长度。
        public const int MR_FIXLEN_EXPIREDABSTIME = 20; // 过期绝对时间固定长度。
        //*****************************************************************
        //函数返回错误值
        //MR_ERRCODE_OK                   0
        //MR_ERRCODE_PARAMERR             -1
        //MR_ERRCODE_CONNERR              -2
        //MR_ERRCODE_TIMEEXPIRED          -3
        //MR_ERRCODE_TIMEOUT              -4
        //MR_ERRCODE_NOMSG                -5
        //MR_ERRCODE_BUFTOOSHORT          -6
        //MR_ERRCODE_BUFTOOBIG            -7
        //MR_ERRCODE_SYSERROR             -8
        #endregion

        #region * 结构体的定义
        /// <summary>
        /// 用来表示一条消息的各种属性
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct STUsgProperty
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_ADDR)]
            public string m_szSourceUserID;//MR_MAXLEN_ADDR 源用户标识,以“\0”结尾的字符串
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_ADDR)]
            public string m_szSourceAppID;//MR_MAXLEN_ADDR 源应用标识,以“\0”结尾的字符串
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_ADDR)]
            public string m_szDestUserID;//MR_MAXLEN_ADDR 目的用户标识,以“\0”结尾的字符串
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_ADDR)]
            public string m_szDestAppID;//MR_MAXLEN_ADDR 目的应用标识,以“\0”结尾的字符串
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_PKGID)]
            public string m_szPkgID;//MR_MAXLEN_PKGID 消息包的包标识,以“\0”结尾的字符串,或者由用户调用MrCreatePkgID函数生成,或者为空(即'\0')
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_PKGID)]
            public string m_szCorrPkgID;//MR_MAXLEN_PKGID 相关包标识,以“\0”结尾的字符串,供用户自用
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_USERDATA)]
            public string m_szUserData1;//MR_MAXLEN_USERDATA 用户数据1,以“\0”结尾的字符串,供用户自用
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_USERDATA)]
            public string m_szUserData2;//MR_MAXLEN_USERDATA 用户数据2,以“\0”结尾的字符串,供用户自用
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_FIXLEN_EXPIREDABSTIME)]
            public string m_szExpiredAbsTime;//MR_FIXLEN_EXPIREDABSTIME 该消息的过期时间,以“\0”结尾的字符串,格式为“YYYY-MM-DD HH:MM:SS”。也可以置空,此时如果目的用户不在线,或者目的应用未连接,则消息立即过期。
            public byte m_ucFlag;//消息标识,有8个二进制位组成,各位含义如下:位0 --为1表示持久消息,需可靠传输,暂不支持;
            //位1 --为1表示消息需压缩传输
            public byte m_ucProtocolType;//协议类型标识,取值可以是下列之一:MR_PROTOCOLTYPE_MRSTANDAND  0x01  FDEP规定的标准业务协议
            //MR_PROTOCOLTYPE_SELFCUSTOM  0xFF  用户自定义协议类型
        }

        /// <summary>
        /// 用来定义与接入客户端建立连接所需的各种信息
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct STUConnInfo
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string m_szMRIP;//接入客户端消息路由器的IP地址,以"\0"结尾的字符串,格式为“xxx.xxx.xxx.xxx”
            public UInt16 m_usMRPort;//接入客户端消息路由器的连接端口
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string m_szMRIPBak;//备用消息路由器的IP地址,不设置备用消息路由器时可以为空
            public UInt16 m_usMRPortBak;//备用消息路由器的连接端口,不设置备用消息路由器时可以为0
        }
        #endregion

        #region * 函数的定义
        /// <summary>
        /// 定义的回调函数
        /// </summary>
        /// <param name="psPkg">要发送的消息包缓冲区</param>
        /// <param name="iPkgLen">缓冲区中的消息包长度</param>
        /// <param name="pMsgPropery">消息包属性</param>
        /// <param name="pvUserData">供回调函数使用的用户数据</param>
        /// <returns></returns>
        [UnmanagedFunctionPointerAttribute(CallingConvention.StdCall)]
        public delegate int OnReceiveCallBack(string psPkg, int iPkgLen, ref STUsgProperty pMsgPropery, IntPtr pvUserData);
        //typedef int (*OnReceiveCallBack)(const char* psPkg, int iPkgLen, const STUMsgProperty* pMsgPropery, void* pvUserData);


        /// <summary>
        /// 1.初始化,获取相关资源,并尝试与接入客户端FDAP建立连接
        /// </summary>
        /// <param name="psAppID">本应用的应用标识</param>[in]
        /// <param name="psPasswd">本应用在接入客服端设置的密码,密码必须与预设的匹配才能继续</param>[in]
        /// <param name="onReceive">接收到消息包时的回调函数</param>[in]
        /// <param name="oConnInfo">接入客户端连接信息</param>[in]
        /// <param name="pvUserData">供回调函数使用的用户数据</param>[in]
        /// <returns>NULL 初始化失败 非NULL 初始化成功,返回一个连接句柄,给句柄将作为其他函数调用的参数</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrInit", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern IntPtr MrInit(string psAppID, string psPasswd, OnReceiveCallBack onReceive, STUConnInfo oConnInfo, IntPtr pvUserData);
        //void* _stdcall MrInit(const char* psAppID, const char* psAppPasswd,OnReceiveCallBack onReceive,const STUConnInfo oConnInfo, void* pvUserData);

        /// <summary>
        /// 2.初始化,获取相关资源,并尝试与接入客户端FDAP建立连接
        /// </summary>
        /// <param name="psUserCertID">本应用的用户标识</param>[in]
        /// <param name="psAppID">本应用的应用标识</param>[in]
        /// <param name="psPasswd">本应用在接入客服端设置的密码,密码必须与预设的匹配才能继续</param>[in]
        /// <param name="onReceive">接收到消息包时的回调函数</param>[in]
        /// <param name="oConnInfo">接入客户端连接信息</param>[in]
        /// <param name="pvUserData">供回调函数使用的用户数据</param>[in]
        /// <returns>NULL 初始化失败 非NULL 初始化成功,返回一个连接句柄,给句柄将作为其他函数调用的参数</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrInit1", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern IntPtr MrInit1(string psUserCertID, string psAppID, string psPasswd, OnReceiveCallBack onReceive, STUConnInfo oConnInfo, IntPtr pvUserData);

        /// <summary>
        /// 3.连接FDAP时的初始化函数。该函数对FDEAPI进行初始化,分配获取相关资源,并尝试与接入客户端建立通信连接
        /// </summary>
        /// <param name="pHandle">函数返回的句柄,该句柄将作为其他函数调用的参数</param>[out]
        /// <param name="psUserCertID">本应用的用户标识</param>[in]
        /// <param name="psAppID">本应用的应用标识</param>[in]
        /// <param name="psPasswd">本应用在接入客户端设置的密码,密码必须与预设的匹配才能继续</param>[in]
        /// <param name="pMsgProperty">消息包属性</param>[in]
        /// <param name="onReceive">接收到消息包时的回调函数</param>[in]
        /// <param name="oConnInfo">接入客户端连接信息</param>[in]
        /// <param name="pvUserData">供回调函数使用的用户数据</param>[in]
        /// <param name="iThreadCount">调用回头函数OnReceive的线程数目</param>[in]
        /// <returns>无</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrInit1Ex1", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern IntPtr MrInit1Ex1(out IntPtr pHandle, string psUserCertID, string psAppID, string psPasswd, ref STUsgProperty pMsgProperty, OnReceiveCallBack onReceive, STUConnInfo oConnInfo, IntPtr pvUserData, int iThreadCount);

        /// <summary>
        /// 4.连接FDAP时的初始化函数。该函数对FDEAPI进行初始化,分配获取相关资源,并尝试与接入客户端建立通信连接
        /// </summary>
        /// <param name="pHandle">函数返回的句柄,该句柄将作为其他函数调用的参数</param>[out]
        /// <param name="psAppID">本应用的应用标识</param>[in]
        /// <param name="psPassws">本应用在接入客户端设置的密码,密码必须与预设的匹配才能继续</param>[in]
        /// <param name="pOnRecvMsgPropery">这是回调函数OnReceive的接收条件,如果不需要任何条件,则可以填NULL</param>[in]
        /// <param name="onReceive">接收到消息包时的回调函数</param>[in]
        /// <param name="pConnInfo">接入客户端连接信息</param>[in]
        /// <param name="pvUserData">供回调函数使用的用户数据</param>[in]
        /// <param name="iThreadCount">调用回头函数OnReceive的线程数目</param>
        /// <returns>无</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrInit2", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern IntPtr MrInit2(out IntPtr pHandle, string psAppID, string psPassws, ref STUsgProperty pOnRecvMsgPropery, OnReceiveCallBack onReceive, ref STUConnInfo pConnInfo, IntPtr pvUserData, int iThreadCount);
        //void*  _stdcall MrInit2(void** ppHandle, const char* psAppID, const char* psAppPasswd, STUMsgProperty* pOnRecvMsgPropery,OnReceiveCallBack onReceive,const STUConnInfo* pConnInfo, void* pvUserData, int iThreadCount);

        /// <summary>
        /// 5.判断与交换中枢的连接是否正常
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>
        /// <returns>0不正常 1正常</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrIsLinkOK", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrIsLinkOK(IntPtr pHandle);
        //int  _stdcall MrIsLinkOK(void* pHandle)

        /// <summary>
        /// 6.消息包标识生成函数
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>[in]
        /// <param name="szPkgID">生成的消息包标识</param>[out]
        /// <returns>0 成功 其他 失败</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrCreatePkgID", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrCreatePkgID(IntPtr pHandle, StringBuilder szPkgID);
        //int _stdcall MrCreatePkgID(void* pHandle,char szPkgID[MR_MAXLEN_PKGID])

        /// <summary>
        /// 7.消息包发送函数
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>[in]
        /// <param name="psPkg">要发送的消息包缓冲区</param>[in]
        /// <param name="iPkgLen">缓冲区中的消息包长度</param>[in]
        /// <param name="pMsgPropery">消息包属性</param>[in/out]
        /// <param name="iMillSecTimeo">以毫米为单位的接收最大超时时间</param>[in]
        /// <returns>0 成功 其他 失败</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrSend", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrSend(IntPtr pHandle, string psPkg, int iPkgLen, ref STUsgProperty pMsgPropery, int iMillSecTimeo);
        //int _stdcall MrSend(void* pHandle,const char* psPkg,int iPkgLen,STUsgProperty* pMsgPropery,int iMillSecTimeo);

        /// <summary>
        /// 8.消息包接收函数1
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>[in]
        /// <param name="ppsPkg">双指针,返回包所指向的内存</param>[out]
        /// <param name="piOutPkgLen">接收到消息包的实际长度</param>[out]
        /// <param name="pMsgPropery">接收条件</param>[in/out]
        /// <param name="iMillSecTimeo">以毫米为单位的接收最大超时时间</param>[in]
        /// <returns>0 成功 其他 失败</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrReceive1", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrReceive1(IntPtr pHandle, out IntPtr ppsPkg, out int piOutPkgLen, ref STUsgProperty pMsgPropery, int iMillSecTimeo);
        //int _stdcall MrReceive1(void* pHandle, char** ppsPkg, int* piOutPkgLen, STUMsgProperty* pMsgPropery, int iMillSecTimeo);

        /// <summary>
        /// 9.消息包内存释放函数
        /// </summary>
        /// <param name="psPkg">由MrReceivel1函数的第二个参数返回的指针</param>[in]
        /// <returns>无</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrReceive1_FreeBuf", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern IntPtr MrReceive1_FreeBuf(string psPkg);
        //void _stdcall MrReceive1_FreeBuf(char* psPkg);

        /// <summary>
        /// 10.消息包浏览函数
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>[in]
        /// <param name="piOutPkgLen">接收到消息包的实际长度</param>[out]
        /// <param name="pMsgPropery">接收条件</param>[in/out]
        /// <param name="iMillSecTimeo">以毫米为单位的接收最大超时时间</param>[in]
        /// <returns>0 成功 其他 失败</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrBrowse", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrBrowse(IntPtr pHandle, out int piOutPkgLen, ref STUsgProperty pMsgPropery, int iMillSecTimeo);
        //int _stdcall MrBrowse(void* pHandle,  int* piOutPkgLen, STUMsgProperty* pMsgPropery, int iMillSecTimeo);

        /// <summary>
        /// 11.消息包接收函数2
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>[in]
        /// <param name="ppsPkg">双指针,返回包所指向的内存</param>[out]
        /// <param name="piOutPkgLen">接收到消息包的实际长度</param>[out]
        /// <param name="iBufLenIn">消息包缓冲区大小</param>[out]
        /// <param name="pMsgPropery">接收条件</param>[in/out]
        /// <param name="iMillSecTimeo">以毫米为单位的接收最大超时时间</param>[in]
        /// <returns>0 成功 其他 失败</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrReceive2", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrReceive2(IntPtr pHandle, out IntPtr ppsPkg, out int piOutPkgLen, out int iBufLenIn, ref STUsgProperty pMsgPropery, int iMillSecTimeo);
        //int _stdcall MrReceive2(void* pHandle, char** ppsPkg, int* piOutPkgLen, int* iBufLenIn, STUMsgProperty* pMsgPropery, int iMillSecTimeo);

        /// <summary>
        /// 12.消息包接收函数3
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>[in]
        /// <param name="ppsPkg">双指针,返回包所指向的内存</param>[out]
        /// <param name="piOutPkgLen">接收到消息包的实际长度</param>[out]
        /// <param name="piErrSXCode">交换错误的原因码</param>[out]
        /// <param name="pMsgPropery">接收条件</param>[in/out]
        /// <param name="iMillSecTimeo">以毫米为单位的接收最大超时时间</param>[in]
        /// <returns>0 成功 其他 失败</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrReceive3", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrReceive3(IntPtr pHandle, out IntPtr ppsPkg, out int piOutPkgLen, out int piErrSXCode, ref STUsgProperty pMsgPropery, int iMillSecTimeo);
        //int _stdcall MrReceive3(void* pHandle, char** ppsPkg, int* piOutPkgLen, int* piErrSXCode, STUMsgProperty* pMsgPropery, int iMillSecTimeo);

        /// <summary>
        /// 13.消息包接收函数4
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>[in]
        /// <param name="ppsPkg">双指针,返回包所指向的内存</param>[out]
        /// <param name="piOutPkgLen">接收到消息包的实际长度</param>[out]
        /// <param name="piErrSXCode">交换错误的原因码</param>[out]
        /// <param name="pMsgPropery">接收条件</param>[in/out]
        /// <param name="iMillSecTimeo">以毫米为单位的接收最大超时时间</param>[in]
        /// <returns>0 成功 其他 失败</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrReceive4", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrReceive4(IntPtr pHandle, out IntPtr ppsPkg, out int piOutPkgLen, out int piErrSXCode, ref STUsgProperty pMsgPropery, int iMillSecTimeo);
        //int _stdcall MrReceive4(void* pHandle, char** ppsPkg, int* piOutPkgLen, int* piErrSXCode, STUMsgProperty* pMsgPropery, int iMillSecTimeo);

        /// <summary>
        /// 14.释放资源
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值</param>
        /// <returns>无</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrDestroy", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern IntPtr MrDestroy(IntPtr pHandle);
        //void _stdcall MrDestroy(void* pHandle)

        /// <summary>
        /// 15.取得本API的版本号
        /// </summary>
        /// <param name="psBufVersion">返回的版本号</param>
        /// <param name="iBufLen">版本号长度</param>
        /// <returns>无</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrGetVersion", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
        public static extern IntPtr MrGetVersion(StringBuilder psBufVersion, int iBufLen);
        //void _stdcall MrGetVersion(char* psBufVersion,int iBufLen);

        /// <summary>
        /// 16.注册包下推条件
        /// </summary>
        /// <param name="pHandle">连接句柄,调用MrInit时返回的值<</param>
        /// <param name="pMsgPropery">消息的结构实体</param>
        /// <param name="iType">0 增加一个条件 1 删除一个条件 2 清空所有条件</param>
        /// <returns>0 成功 其他 失败</returns>
        [DllImport("mrapi.dll", EntryPoint = "MrRegRecvCondition", CallingConvention = CallingConvention.StdCall, SetLastError = true, CharSet = CharSet.Ansi)]
        public static extern int MrRegRecvCondition(IntPtr pHandle, ref STUsgProperty pMsgPropery, int iType);
        //int _stdcall MrRegRecvCondition(void* pHandle,STUMsgProperty* pMsgPropery,int iType);
        #endregion
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论