首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> 關於C#

C# 視訊監控系列(2):客戶端 SDK 完整封裝——hikclient.dll P/Invoke 定義

     編輯:關於C#

本文提供海康客戶端核心 DLL hikclient.dll 的完整 C# P/Invoke 封裝,涵蓋所有公開 API:初始化/清理、連線控制、碼流回調、錄影、音訊、截圖、畫質、多播、OSD 繪圖、視訊參數等,並定義所需結構體、枚舉與委派,修正歷史版本中的型別錯誤。

結構體定義


[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct CLIENT_VIDEOINFO
{
    public byte m_bRemoteChannel;   // 伺服端通道
    public byte m_bSendMode;        // 傳輸模式
    public byte m_nImgFormat;       // 圖像格式 0/1
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
    public string m_sIPAddress;     // 伺服端 IP
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
    public string m_sUserName;      // 用戶名
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
    public string m_sUserPassword;  // 密碼
    public bool m_bUserCheck;       // 是否驗證
    public IntPtr m_hShowVideo;     // 顯示視窗句柄
}

枚舉定義


public enum SEND_MODE : byte { UDPMODE = 0, TCPMODE, MULTIMODE, DIALING, AUDIODETACH }
public enum DISP_MODE : uint { NORMALMODE = 0, OVERLAYMODE }

回調委派定義


[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void ReadDataCallBack(ulong nChannel, [MarshalAs(UnmanagedType.LPArray)] byte[] pPacketBuffer, ulong nPacketSize);

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void CapPicFun(int stockHandle, IntPtr pBuf, int nSize, int nWidth, int nHeight, int nStamp, int nType, int nReceaved);

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void DrawFun(int stockHandle, IntPtr hDc, int nUser);

核心 API 宣告(節錄)


public static class PcHikClient
{
    public static readonly uint WM_USER = 0x0400;

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientStartup(uint nMessage, IntPtr hWnd);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientCleanup();

    [DllImport("hikclient.dll")]
    public static extern int MP4_ClientStart(ref CLIENT_VIDEOINFO pClientinfo, ReadDataCallBack rdcb);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientStop(int stockHandle);

    [DllImport("hikclient.dll")]
    public static extern int MP4_ClientGetState(int stockHandle);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientStartCapture(int stockHandle);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientStartCaptureFile(int stockHandle, string fileName); // 修正: int

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientStopCapture(int stockHandle); // 修正: int

    [DllImport("hikclient.dll")]
    public static extern ushort MP4_ClientGetServerChanNum(string ip);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientCommandtoServer(string ip, string cmd, ushort len);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientShut(string ip, char channel);

    [DllImport("hikclient.dll")]
    public static extern void MP4_ClientReadLastMessage(string ip, out string cmd, out ushort len);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientAudioVolume(ushort volume);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientAudioStart(int stockHandle);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientAudioStop();

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSetBufferNum(int stockHandle, ushort bufNum);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSetNetPort(ushort serverPort, ushort clientPort);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSetTTL(char ttl);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSetPlayDelay(int stockHandle, ushort delay);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSetShowMode(ulong mode, int colorKey);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSetQuality(int stockHandle, ushort quality);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSetCapPicCallBack(int stockHandle, CapPicFun cb);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSavePicFile(IntPtr pBuf, int size, int w, int h, int type, string file);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientThrowBFrame(int stockHandle, ulong num);

    [DllImport("hikclient.dll")]
    public static extern ulong MP4_ClientGetFrameNum(int stockHandle);

    [DllImport("hikclient.dll")]
    public static extern uint MP4_ClientGetSdkVersion();

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientAudioMode(ushort mode);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientAudioStartShare(int stockHandle);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientAudioStopShare(int stockHandle);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientCastGroup(int stockHandle, out string ip, ushort port);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientRigisterDrawFun(int stockHandle, DrawFun cb, int user);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientCleanBuffer(int port, int type);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientSetVideoPara(int stockHandle, ulong region, int bright, int contrast, int sat, int hue);

    [DllImport("hikclient.dll")]
    public static extern bool MP4_ClientGetVideoPara(int stockHandle, ulong region, out int bright, out int contrast, out int sat, out int hue);
}

歷史修正記錄

日期項目修正前修正後
2009-02-27截圖委派 pBufbyte[]IntPtr
2009-02-27SavePicFile pBufbyte[]IntPtr
2009-03-04StartCaptureFile StockHandlelongint
2009-03-04StopCapture StockHandlelongint

使用提醒

  • 所有字串參數採用 CharSet.Ansi 或 ByValTStr,對應 VC++ char*/LPTSTR。
  • 回調委派 必須聲明為類別成員,並標記 [UnmanagedFunctionPointer(CallingConvention.StdCall)]。
  • 部分未使用的 API 參數型別可能仍有誤差,實際調用時請對照 VC++ 範例與 SDK 文件微調。
  • 本封裝為系列文章基礎設施,隨後續文章持續維護更新。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved