本文提供 HikServer.dll(網路服務層)的完整 C# P/Invoke 封裝,涵蓋服務啟動/停止、客戶端驗證、碼流寫入、訊息收發、多播設定、緩衝區控制等核心 API,並定義必要的結構體、委派與枚舉。
[StructLayout(LayoutKind.Sequential)]
public struct PSERVER_VIDEOINFO
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 64)]
public byte[] m_datatype;
public byte m_channum;
public int m_waittime;
public ulong m_bufnum;
}
public enum ChannelDataType { NORMAL = 50, DIALING, SMALLPIC }
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void SetIBP(int port, int framerat);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void MakeIFrame(ulong port);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void StartCap(int nChannel);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void StopCap(int nChannel);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int CheckPassword(string username, ushort namelen, string password, ushort passlen);
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate int CheckIP(int nChannel, string nIP);
public static class HikServer
{
public static readonly uint WM_USER = 0x0400;
[DllImport("HikServer.dll")]
public static extern int MP4_ServerStart(ref PSERVER_VIDEOINFO videoinfo);
[DllImport("HikServer.dll")]
public static extern bool MP4_ServerStop();
[DllImport("HikServer.dll")]
public static extern bool MP4_ServerGetState(out ushort clientnum);
[DllImport("HikServer.dll")]
public static extern void MP4_ServerSetMessage(uint nMessage, IntPtr hWnd);
[DllImport("HikServer.dll")]
public static extern void MP4_ServerCheckIP(CheckIP cip);
[DllImport("HikServer.dll")]
public static extern void MP4_ServerCheckPassword(CheckPassword cp);
[DllImport("HikServer.dll")]
public static extern void MP4_ServerWriteDataEx(int nPort, IntPtr pPacketBuffer, int nPacketSize, int frameType, int breakable, int nImgFormat);
[DllImport("HikServer.dll")]
public static extern void MP4_ServerSetStart(StartCap sc);
[DllImport("HikServer.dll")]
public static extern void MP4_ServerSetStop(StopCap sc);
[DllImport("HikServer.dll")]
public static extern void MP4_ServerSetCapIFrame(MakeIFrame mf);
[DllImport("HikServer.dll")]
public static extern int MP4_ServerSetNetPort(ushort dServerPort, ushort dClientPort);
[DllImport("HikServer.dll")]
public static extern int MP4_ServerSetTTL(byte cTTLVal);
[DllImport("HikServer.dll")]
public static extern int MP4_ServerSetBufNum(ushort nChannel, ushort dBufNum);
[DllImport("HikServer.dll")]
public static extern bool MP4_ServerCastGroup(bool bSet, ulong dChannel, string sIP, ushort wPort);
[DllImport("HikServer.dll")]
public static extern int MP4_ServerMaxUser(uint dwChannel, int nMaxNum);
[DllImport("HikServer.dll")]
public static extern ulong MP4_ServerGetSdkVersion();
}
MP4_ServerReadLastMessage 字串參數改為 StringBuilder,避免記憶體存取錯誤。string m_sIP, string m_sCommand, out ushort m_wLenStringBuilder m_sIP, StringBuilder m_sCommand, out ushort m_wLen[UnmanagedFunctionPointer(CallingConvention.StdCall)]。MP4_ServerWriteDataEx 使用 IntPtr 接收緩衝指標,配合 Marshal.Copy 讀取資料。HikServerWrapper,供服務端與播放器專案共同引用。SetMessage → CheckIP/Password → SetStart/SetCapIFrame → SetBufNum → ServerStart。STREAM_DIRECT_READ_CALLBACK 中呼叫 MP4_ServerWriteDataEx,nImgFormat=0 主碼流、1 子碼流。