1、C#中大部分基礎資料型別 (Elementary Data Type)都和C++的基礎資料型別 (Elementary Data Type)相同,因此基礎資料型別 (Elementary Data Type)的轉換比較簡單,需要注意的就是位元組大小。 2、C++數組和C#的數群組轉換需要注意的就是需要注意,需要增加聲明如[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]才能定義一個C++中的數組元素為10個的數組。 3、 C++的回呼函數介面需要採用C#中的委託函數方法,C++的中回呼函數設定中,一般使用的是地址值,因此需要C#中顯示採用託管函數。 4、C++的指標數組,可用C#中的IntPtr ;如果只是地址指標,則C#中參考型別即可。類型前加ref
舉例說明: using System; using System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
namespace IPNBSVTD_CSharp { public delegate void StatusCallBack(IntPtr dwInstance, IntPtr wParam, IntPtr lParam); public delegate void VoiceCallBack(IntPtr dwInstance, byte ucDeviceIndex, IntPtr pInDataBuf, ref int pInDataLen, IntPtr pOutDataBuf, int nOutDataLen);
public enum VT_MESSAGE{ VT_MESSAGE_SET_DEVICE_INFO, VT_MESSAGE_UPDATE_DEVICE_INFO, VT_MESSAGE_UPDATE_MIXER_IN_CTRL, VT_MESSAGE_UPDATE_MIXER_OUT_CTRL, VT_MESSAGE_MISSED_CALLS, // 未接來電};public enum VT_TASK_TYPE //終端狀態{ VT_TASK_TYPE_NULL, //空閑(無任務) VT_TASK_TYPE_TO_MONITOR, //發起監聽 VT_TASK_TYPE_BY_MONITOR, //被監聽 VT_TASK_TYPE_TO_BROADCAST, //發起采播 VT_TASK_TYPE_BY_BROADCAST, //接收采播 VT_TASK_TYPE_TO_TALK, //發起對講 VT_TASK_TYPE_BY_TALK, //接收對講 VT_TASK_TYPE_MULTITALK, //多方通話 VT_TASK_TYPE_OFFLINETALK, //離線對講};public enum VT_TASK_STATE //終端任務狀態 { VT_TASK_STATE_NULL, // 無任務 VT_TASK_STATE_STOP, // 監聽停止,對講停止,廣播停止 VT_TASK_STATE_CONNECTING, // 監聽:串連中 VT_TASK_STATE_CALLING, // 對講:呼叫中 VT_TASK_STATE_BEGIN_BC_PROMPT, // 廣播:開始提示音 VT_TASK_STATE_END_BC_PROMPT, // 廣播:結束提示音 VT_TASK_STATE_ONGOING, // 監聽中,對講中,廣播中};public enum VT_TARGET_STATE // 目標狀態{ VT_TARGET_STATE_NULL = 0, // 未知 VT_TARGET_STATE_OFFLINE, // 目標離線 VT_TARGET_STATE_STOP, // 目標停止 VT_TARGET_STATE_REJECT, // 目標拒絕 VT_TARGET_STATE_BUSY, // 目標呼叫忙};[StructLayout(LayoutKind.Sequential)]public struct VT_TASK_TARGET{ public int nCount; // 監聽,對講,廣播選擇的目標數量 public int nEditPos; // 當前正在編輯的位置 public VT_TASK_TYPE xeTaskType; // 任務類型 public VT_TASK_STATE xeTaskState; // 工作群組的任務狀態 public VT_TARGET_STATE xeTargetState; // 對講目標狀態 public bool bIsBegin; // 任務是否已開始(呼叫...) [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] public VT_TASK_STATE[] pxeTerState; // 各終端的任務狀態 VT_DEVICE_MAX_COUNT 6 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] public ushort[] pwID; // 終端ID(0:1號終端...)或分區號(1:分區1...) VT_BROADCAST_MAX_COUNT 128 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)] public int[] piSerIdx; // 各終端對應的伺服器編號 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 6)] public byte[] pucPanelNo; // NAS-8521,呼叫到面板,終端對應的面板序號 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)] public char[] strFileName; // 檔案廣播時,當前廣播的檔案名稱 public uint offlineTargetIP; //離線目標IP public ushort offlineTargetPort; //離線目標連接埠};[StructLayout(LayoutKind.Sequential)]public struct tagSERVER{ public uint ipServer; // 伺服器IP (位元組反序) public ushort wServerPort; // 伺服器接收埠 (位元組反序) public bool bFixPort; // 是否固定接收埠 public ushort wHostPort; // 本機接收埠 (位元組反序) public bool bOnline; // 是否聯機 public bool bIsPCM; // 是否是PCM/ADPCM壓縮 public bool bIs8K; // 是否是8K/22K採樣 public bool bIsMultiCast; // 是否是組播/單播 public bool bRecord; // 是否錄音 public uint ipRecord; // 錄音IP public ushort wRecordPort; // 錄音連接埠};[StructLayout(LayoutKind.Sequential)]public struct VT_DEVICE_INFO{ public ushort wDeviceId; // 裝置編號(從0開始,顯示時要加1) public uint ipHost; // 主機IP (位元組反序) public bool bIsRegister; // 是否註冊 public ushort dwVol; // 當前輸出音量 public int iServerCount; // 伺服器的數量 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)] public tagSERVER[] pxsServer; // 伺服器 public int nTaskCount; // 任務數量 public int nCurTask; // 當前任務(對應於xsTaskTarget的下標) [MarshalAs(UnmanagedType.ByValArray, SizeConst = 1)] public VT_TASK_TARGET[] pxsTaskArray; // 任務目標};/// <summary>/// 跨平台叫用方法/// </summary>public class NativeMethod{ /// <summary> /// 路徑最大大小 /// </summary> public const int MAX_PATH = 260; [DllImport("IPNBSVTD.dll")] public static extern void IPNBSVTD_DeleteDevice(); [DllImport("IPNBSVTD.dll")] public static extern byte IPNBSVTD_CreateDevice(byte ucCount); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_DeviceMute(ref bool pbMute, bool bIsAudioOut, bool bIsSet); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_DeviceVolume(ref int pdwVolume, bool bIsAudioOut, bool bIsSet); [DllImport("IPNBSVTD.dll")] public static extern void IPNBSVTD_SetINIFilePath(byte ucDeviceIndex, string pINIFilePath); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_OpenServer(byte ucDeviceIndex, IntPtr hParentWnd, uint nMsg); //[DllImport("IPNBSVTD.dll")] //public static extern bool IPNBSVTD_SetBCFromFile(byte ucDeviceIndex, bool bBCFromFile, bool bLoop, // [MarshalAs(UnmanagedType.LPArray)]string[] pStrFile, short sFileCount); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetBCFromFileEx(byte ucDeviceIndex, bool bBCFromFile, bool bLoop, string strFile, bool bAdd); public static void SetBCFromFile(byte ucDeviceIndex, bool bBCFromFile, bool bLoop, List<string> files, short sFileCount) { for (int i = 0; i < files.Count; i++) IPNBSVTD_SetBCFromFileEx(ucDeviceIndex, bBCFromFile, bLoop, files[i], (i == 0) ? false : true); } [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetBCToTerm(byte ucDeviceIndex); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_GetCurTaskTarget(byte ucDeviceIndex, ref VT_TASK_TARGET pTaskTarget, int iTaskPos); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetCurTaskTarget(byte ucDeviceIndex, ref VT_TASK_TARGET pTaskTarget, int iTaskPos); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_TaskBegin(byte ucDeviceIndex, int iTaskPos); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_TaskEnd(byte ucDeviceIndex, int iTaskPos); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_GetDeviceInfo(byte ucDeviceIndex, ref VT_DEVICE_INFO pDeviceInfo); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_GetBCFileCurrent(byte ucDeviceIndex, StringBuilder strFile); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetStatusCallBack(byte ucDeviceIndex, StatusCallBack dwCallBack, IntPtr dwInstance); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetVoiceCallBack(byte ucDeviceIndex, VoiceCallBack dwCallBack, IntPtr dwInstance); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetFileBCNext(byte ucDeviceIndex); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetFileBCPause(byte ucDeviceIndex, bool bPause); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetFileBCPrevious(byte ucDeviceIndex); [DllImport("IPNBSVTD.dll")] public static extern uint IPNBSVTD_GetBCFileCurrentPlayPos(byte ucDeviceIndex, ref uint pBcFileLen); [DllImport("IPNBSVTD.dll")] public static extern bool IPNBSVTD_SetFileBCPlayListPos(byte ucDeviceIndex, byte ucPlayListPos);}
}