1. C#의 기본 데이터 유형은 대부분 C++의 기본 데이터 유형과 동일하므로 기본 데이터 유형의 변환이 상대적으로 간단합니다. 주의해야 할 것은 바이트 크기입니다. 2. C++ 배열과 C# 배열을 변환할 때 주의할 점은 C++에서 10개의 배열 요소로 배열을 정의하려면 [MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]와 같은 문을 추가해야 한다는 것입니다. 3. C++의 콜백 함수 인터페이스는 C#의 대리자 함수 메서드를 사용해야 합니다. C++의 콜백 함수 설정에서는 일반적으로 주소 값을 사용하므로 관리되는 함수를 C#으로 표시해야 합니다. 4. C++의 포인터 배열의 경우 C#의 IntPtr을 사용할 수 있습니다. 주소 포인터인 경우 C#의 참조 유형을 사용할 수 있습니다. 예를 들어 다음과 같이 유형 앞에 ref
를 추가합니다. using System.Text; using System.Runtime.InteropServices;
namespace IPNBSVTD_CSharp { Public Delegate void StatusCallBack( IntPtr dwInstance, IntPtr wParam, IntPtr lParam); 공개 위임 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); }
}