c++ function to c function sample program share

  • 2020-05-24 06:03:58
  • OfStack


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace FDEP
{
    /// <summary>
    ///  Luo Xucheng 
    ///  Function conversion of shenzhen securities pass 
    /// </summary>
    public class ImportDLL
    {
        #region *  Constants defined 
        //*****************************************************************
        // Protocol type constant 
        // The name of the                               Define the value       instructions 
        //MR_PROTOCOLTYPE_MRSTANDAND       0x01      FDEP Specified identification business agreement 
        //MR_PROTOCOLTYPE_SELFCUSTOM       0xFF       User-defined protocol types 
        /// <summary>
        ///  Protocol type constant 
        /// </summary>
        public const uint MR_PROTOCOLTYPE_MRSTANDAND = 0x01; // SBSP Standard business agreement. 
        public const uint MR_PROTOCOLTYPE_SELFCUSTOM = 0xFF; //  User-defined protocol types. 
        //*****************************************************************
        // The message identifies the bit constant 
        // The name of the                               Define the value       instructions 
        //MR_MSGFLAG_PERSIST               0x01       Persistent message flag for reliable transmission. Not currently supported 
        //MR_MSGFLAG_COMPRESS              0x02       Compressed flag, need to be compressed for transmission 
        /// <summary>
        ///  Message flag bit constant 
        /// </summary>
        public const uint MR_MSGFLAG_PERSIST = 0x01; //  Persistent message flag for reliable transmission. 
        public const uint MR_MSGFLAG_COMPRESS = 0x02; //  Compressed flag, need to be compressed for transmission. 
        //*****************************************************************
        // The length of the constants 
        // The name of the                               Define the value       instructions 
        //MR_MAXLEN_ADDR                   64         Maximum length of user id and application id 
        //MR_MAXLEN_PKGID                  64         The maximum length of the message package id 
        //MR_MAXLEN_USERDATA               256        The maximum length of data retained by the user 
        //MR_FIXLEN_EXPIREDABSTIME         20         Expiration absolute time fixed length 
        /// <summary>
        ///  Message flag bit constant 
        /// </summary>
        public const int MR_MAXLEN_ADDR = 64; //  Maximum length of user id and application id. 
        public const int MR_MAXLEN_PKGID = 64; //  The maximum length of the message package id. 
        public const int MR_MAXLEN_USERDATA = 256; //  The maximum length of data retained by the user. 
        public const int MR_FIXLEN_EXPIREDABSTIME = 20; //  Expiration absolute time fixed length. 
        //*****************************************************************
        // The function returns an error value 
        //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 *  The definition of a structure 
        /// <summary>
        ///  Used to represent 1 Properties of a message 
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct STUsgProperty
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_ADDR)]
            public string m_szSourceUserID;//MR_MAXLEN_ADDR  Source user id to" \0 "Ending of the string 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_ADDR)]
            public string m_szSourceAppID;//MR_MAXLEN_ADDR  Source application id to" \0 "Ending of the string 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_ADDR)]
            public string m_szDestUserID;//MR_MAXLEN_ADDR  Target user id to" \0 "Ending of the string 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_ADDR)]
            public string m_szDestAppID;//MR_MAXLEN_ADDR  Objective to apply the logo to" \0 "Ending of the string 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_PKGID)]
            public string m_szPkgID;//MR_MAXLEN_PKGID  The package identity of the message package to" \0 "Ends the string, or is called by the user MrCreatePkgID The function is generated, or it is empty ( namely '\0')
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_PKGID)]
            public string m_szCorrPkgID;//MR_MAXLEN_PKGID  The associated package is identified as" \0 "End of the string for user's own use 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_USERDATA)]
            public string m_szUserData1;//MR_MAXLEN_USERDATA  User data 1 In order to" \0 "End of the string for user's own use 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_MAXLEN_USERDATA)]
            public string m_szUserData2;//MR_MAXLEN_USERDATA  User data 2 In order to" \0 "End of the string for user's own use 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MR_FIXLEN_EXPIREDABSTIME)]
            public string m_szExpiredAbsTime;//MR_FIXLEN_EXPIREDABSTIME  The expiration time of the message, to" \0 "Ending of the string in the format of" YYYY-MM-DD HH:MM:SS ". It can also be left blank, where the message immediately expires if the destination user is not online or if the destination application is not connected. 
            public byte m_ucFlag;// Message id, yes 8 a 2 The base bits are composed, and they mean the following : position 0 -- for 1 Represents a persistent message, which needs to be transmitted reliably and is not supported for the time being. 
            // position 1 -- for 1 Indicates that the message needs to be compressed for transmission 
            public byte m_ucProtocolType;// Protocol type identifier, which can be either of the following values 1:MR_PROTOCOLTYPE_MRSTANDAND  0x01  FDEP Specified standard business agreement 
            //MR_PROTOCOLTYPE_SELFCUSTOM  0xFF   User-defined protocol types 
        }
        /// <summary>
        ///  Used to define the various information needed to establish a connection with an access client 
        /// </summary>
        [StructLayout(LayoutKind.Sequential)]
        public struct STUConnInfo
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string m_szMRIP;// Access to the client message router IP The address to "\0" The ending string, in the format of" xxx.xxx.xxx.xxx " 
            public UInt16 m_usMRPort;// Access the connection port of the client message router 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
            public string m_szMRIPBak;// Of the alternate message router IP Address can be empty without setting up the alternate message router 
            public UInt16 m_usMRPortBak;// The connection port of the standby message router, which can be used when the standby message router is not set 0
        }
        #endregion
        #region *  Definition of function 
        /// <summary>
        ///  The defined callback function 
        /// </summary>
        /// <param name="psPkg"> The message packet buffer to send </param>
        /// <param name="iPkgLen"> The length of the message package in the buffer </param>
        /// <param name="pMsgPropery"> Message package properties </param>
        /// <param name="pvUserData"> User data for use by the callback function </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. Initialize, get related resources, and try to access the client FDAP Establish a connection 
        /// </summary>
        /// <param name="psAppID"> The application logo for this application </param>[in]
        /// <param name="psPasswd"> This application in the access to the customer service side of the password set, password must match the preset to continue </param>[in]
        /// <param name="onReceive"> The callback function when a message packet is received </param>[in]
        /// <param name="oConnInfo"> Access client connection information </param>[in]
        /// <param name="pvUserData"> User data for use by the callback function </param>[in]
        /// <returns>NULL  Initialization failure   non NULL  Successful initialization, return 1 A connection handle that will be used as an argument to other function calls </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. Initialize, get related resources, and try to access the client FDAP Establish a connection 
        /// </summary>
        /// <param name="psUserCertID"> User id for this application </param>[in]
        /// <param name="psAppID"> The application logo for this application </param>[in]
        /// <param name="psPasswd"> This application in the access to the customer service side of the password set, password must match the preset to continue </param>[in]
        /// <param name="onReceive"> The callback function when a message packet is received </param>[in]
        /// <param name="oConnInfo"> Access client connection information </param>[in]
        /// <param name="pvUserData"> User data for use by the callback function </param>[in]
        /// <returns>NULL  Initialization failure   non NULL  Successful initialization, return 1 A connection handle that will be used as an argument to other function calls </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. The connection FDAP Is the initialization function of. The function of FDEAPI Initialize, allocate resources, and attempt to establish a communication connection with the access client 
        /// </summary>
        /// <param name="pHandle"> The function returns a handle that will be used as an argument to other function calls </param>[out]
        /// <param name="psUserCertID"> User id for this application </param>[in]
        /// <param name="psAppID"> The application logo for this application </param>[in]
        /// <param name="psPasswd"> This application in the access client set password, password must match the default to continue </param>[in]
        /// <param name="pMsgProperty"> Message package properties </param>[in]
        /// <param name="onReceive"> The callback function when a message packet is received </param>[in]
        /// <param name="oConnInfo"> Access client connection information </param>[in]
        /// <param name="pvUserData"> User data for use by the callback function </param>[in]
        /// <param name="iThreadCount"> Call back function OnReceive Number of threads </param>[in]
        /// <returns> There is no </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. The connection FDAP Is the initialization function of. The function of FDEAPI Initialize, allocate resources, and attempt to establish a communication connection with the access client 
        /// </summary>
        /// <param name="pHandle"> The function returns a handle that will be used as an argument to other function calls </param>[out]
        /// <param name="psAppID"> The application logo for this application </param>[in]
        /// <param name="psPassws"> This application in the access client set password, password must match the default to continue </param>[in]
        /// <param name="pOnRecvMsgPropery"> This is the callback function OnReceive If no conditions are required, it can be filled in NULL</param>[in]
        /// <param name="onReceive"> The callback function when a message packet is received </param>[in]
        /// <param name="pConnInfo"> Access client connection information </param>[in]
        /// <param name="pvUserData"> User data for use by the callback function </param>[in]
        /// <param name="iThreadCount"> Call back function OnReceive Number of threads </param>
        /// <returns> There is no </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. Determine if the connection to the exchange center is normal 
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>
        /// <returns>0 Is not normal  1 normal </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. The message package identifies the generation function 
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>[in]
        /// <param name="szPkgID"> The generated message package identity </param>[out]
        /// <returns>0  successful   other   failure </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. Message packet sending function 
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>[in]
        /// <param name="psPkg"> The message packet buffer to send </param>[in]
        /// <param name="iPkgLen"> The length of the message package in the buffer </param>[in]
        /// <param name="pMsgPropery"> Message package properties </param>[in/out]
        /// <param name="iMillSecTimeo"> Maximum received timeout in millimeters </param>[in]
        /// <returns>0  successful   other   failure </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. Message packet receiving function 1
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>[in]
        /// <param name="ppsPkg"> Double pointer, returns the memory to which the package points </param>[out]
        /// <param name="piOutPkgLen"> The actual length of the received message packet </param>[out]
        /// <param name="pMsgPropery"> Receiving conditions </param>[in/out]
        /// <param name="iMillSecTimeo"> Maximum received timeout in millimeters </param>[in]
        /// <returns>0  successful   other   failure </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. Message pack memory release function 
        /// </summary>
        /// <param name="psPkg"> by MrReceivel1 The function of the first 2 Returns a pointer to a parameter </param>[in]
        /// <returns> There is no </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. Message packet browsing function 
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>[in]
        /// <param name="piOutPkgLen"> The actual length of the received message packet </param>[out]
        /// <param name="pMsgPropery"> Receiving conditions </param>[in/out]
        /// <param name="iMillSecTimeo"> Maximum received timeout in millimeters </param>[in]
        /// <returns>0  successful   other   failure </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. Message packet receiving function 2
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>[in]
        /// <param name="ppsPkg"> Double pointer, returns the memory to which the package points </param>[out]
        /// <param name="piOutPkgLen"> The actual length of the received message packet </param>[out]
        /// <param name="iBufLenIn"> Message packet buffer size </param>[out]
        /// <param name="pMsgPropery"> Receiving conditions </param>[in/out]
        /// <param name="iMillSecTimeo"> Maximum received timeout in millimeters </param>[in]
        /// <returns>0  successful   other   failure </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. Message packet receiving function 3
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>[in]
        /// <param name="ppsPkg"> Double pointer, returns the memory to which the package points </param>[out]
        /// <param name="piOutPkgLen"> The actual length of the received message packet </param>[out]
        /// <param name="piErrSXCode"> Swap the cause code of the error </param>[out]
        /// <param name="pMsgPropery"> Receiving conditions </param>[in/out]
        /// <param name="iMillSecTimeo"> Maximum received timeout in millimeters </param>[in]
        /// <returns>0  successful   other   failure </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. Message packet receiving function 4
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>[in]
        /// <param name="ppsPkg"> Double pointer, returns the memory to which the package points </param>[out]
        /// <param name="piOutPkgLen"> The actual length of the received message packet </param>[out]
        /// <param name="piErrSXCode"> Swap the cause code of the error </param>[out]
        /// <param name="pMsgPropery"> Receiving conditions </param>[in/out]
        /// <param name="iMillSecTimeo"> Maximum received timeout in millimeters </param>[in]
        /// <returns>0  successful   other   failure </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. Release resources 
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when </param>
        /// <returns> There is no </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. Get this API The version number of the 
        /// </summary>
        /// <param name="psBufVersion"> The version number returned </param>
        /// <param name="iBufLen"> Version length </param>
        /// <returns> There is no </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. Registration package push-down conditions  
        /// </summary>
        /// <param name="pHandle"> Connection handle, call MrInit Is the value returned when <</param>
        /// <param name="pMsgPropery"> The structural entity of the message </param>
        /// <param name="iType">0  increase 1 A condition  1  delete 1 A condition  2  Clear out all the conditions </param>
        /// <returns>0  successful   other   failure </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
    }
}


Related articles: