您好,登錄后才能下訂單哦!
這篇文章主要講解了“c++函數轉c#函數的代碼怎么寫”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“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
}
}
感謝各位的閱讀,以上就是“c++函數轉c#函數的代碼怎么寫”的內容了,經過本文的學習后,相信大家對c++函數轉c#函數的代碼怎么寫這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。