1、C#中大部分基本数据类型都和C++的基本数据类型相同,因此基本数据类型的转换比较简单,需要注意的就是字节大小。 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); }
登录后复制
}