C# 如何調用 C++ DLL中的函數接口和回調函數

一、C#中大部分基本數據類型都和C++的基本數據類型相同,所以基本數據類型的轉換比較簡單,須要注意的就是字節大小。 二、C++數組和C#的數組轉換須要注意的就是須要注意,須要增長聲明如[MarshalAs(UnmanagedType.ByValArray, SizeConst = 10)]才能定義一個C++中的數組元素爲10個的數組。 三、 C++的回調函數接口須要採用C#中的委託函數方法,C++的中回調函數設置中,通常使用的是地址值,所以須要C#中顯示採用託管函數。 四、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);
}

}oop

相關文章
相關標籤/搜索