C# application using Hikvision SDK - Audio Detection

robsta

n3wb
Joined
Oct 24, 2020
Messages
2
Reaction score
0
Location
UK
Hello All

I am trying to integrate Audio Detection using C# and the Hikvision SDK. These are the definitions I have been following:






This is the code I have so far.

Code:
[DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_GetDeviceConfig")]
public static extern bool NET_DVR_GetDeviceConfig2(int lUserID, uint dwCommand, uint dwCount, IntPtr lpInBuffer, uint dwInBufferSize, IntPtr lpStatusList, IntPtr lpOutBuffer, uint dwOutBufferSize);

[StructLayoutAttribute(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct NET_DVR_CHANNEL_GROUP
{   
    public uint dwSize;
    public uint dwChannel;
    public uint dwGroup;   
    public byte[] ByID;
    public byte[] byRes1;
    public byte dwPositionNo;
    public byte[] byRes;
}

[StructLayoutAttribute(LayoutKind.Sequential)]
public struct NET_DVR_AUDIO_EXCEPTION
{
    public uint dwSize;
    public byte byEnableAudioInException;
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3, ArraySubType = UnmanagedType.I1)]   
    public byte[] byRes1;
    public NET_VCA_AUDIO_ABNORMAL struAudioAbnormal;
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_DAYS * MAX_TIMESEGMENT_V30, ArraySubType = UnmanagedType.Struct)]
    public NET_DVR_SCHEDTIME[] struAlarmSched;
    public NET_DVR_HANDLEEXCEPTION_V40 struHandleException;
    public uint dwMaxRelRecordChanNum;
      public uint dwRelRecordChanNum;
    [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = MAX_CHANNUM_V30, ArraySubType = UnmanagedType.U4)]
    public uint[] byRelRecordChan;
     [MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 32, ArraySubType = UnmanagedType.I1)]   
    public byte[] byRes2;
}

        public static CHCNetSDK.NET_DVR_AUDIO_EXCEPTION? GetDevice_AudioDetectConfig(int userID, uint channel)
        {
            NET_DVR_CHANNEL_GROUP channelGroup = new NET_DVR_CHANNEL_GROUP();
            channelGroup.dwSize = (uint)Marshal.SizeOf(channelGroup);
            channelGroup.dwChannel = channel;
            IntPtr lpInBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(channelGroup));
            Marshal.StructureToPtr(channelGroup, lpInBuffer, false);
            uint dwInBufferSize = Convert.ToUInt32(channelGroup.dwSize);

            NET_DVR_AUDIO_EXCEPTION result = new NET_DVR_AUDIO_EXCEPTION();
            result.dwSize = (uint)Marshal.SizeOf(result);
            IntPtr lpOutBuffer = Marshal.AllocHGlobal(Marshal.SizeOf(result));
            Marshal.StructureToPtr(result, lpOutBuffer, false);
            uint dwOutBufferSize = Convert.ToUInt32(result.dwSize);

            uint lp = 1;
            IntPtr lpStatusList = Marshal.AllocHGlobal(Marshal.SizeOf(lp));
           
            var b = CHCNetSDK.NET_DVR_GetDeviceConfig2(userID, NET_DVR_GET_AUDIOEXCEPTIONPARAM, 1, lpInBuffer, dwInBufferSize, lpStatusList, lpOutBuffer, dwOutBufferSize);
            if (b) /this is false
            {
                result = (CHCNetSDK.NET_DVR_AUDIO_EXCEPTION)Marshal.PtrToStructure(lpOutBuffer, typeof(CHCNetSDK.NET_DVR_AUDIO_EXCEPTION));
                Marshal.FreeHGlobal(lpInBuffer);
                Marshal.FreeHGlobal(lpOutBuffer);
                return result;
            }
            else
            {
                int errorID = (int)CHCNetSDK.NET_DVR_GetLastError(); /I get error 17
                Marshal.FreeHGlobal(lpInBuffer);
                Marshal.FreeHGlobal(lpOutBuffer);
                return null;
            }
        }

According to this URL NET_DVR_GetErrorMsg. Error 17 means "Parameter error. Input or output parameters in the SDK API is NULL, or the value or format of the parameters does not match with the requirement."

Can anyone see where I have gone wrong?
 
Last edited:
Top