欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-22 20:09:40
复制代码 代码如下:using system;using system.collections.generic;using system.linq;using system...

复制代码 代码如下:

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
    }
}