Creating C# application using Hikvision SDK for DS-7116HVI-SL

Hi mr. Srinivas.
i have problem when preview camera 16 ch in one page on C# cause have Bandwidth so much.
where can i adjust it from program c# ( "public bool StartPlay(Panel PanelName, int PanelNo, int Index" or another place)
and close green box on video preview.
Please help me.
C# SDK from Hikvision.
 

Attachments

  • CCTV.png
    CCTV.png
    702.2 KB · Views: 37
Hi mr. Srinivas.
i have problem when preview camera 16 ch in one page on C# cause have Bandwidth so much.
where can i adjust it from program c# ( "public bool StartPlay(Panel PanelName, int PanelNo, int Index" or another place)
and close green box on video preview.
Please help me.
C# SDK from Hikvision.

Hi Phonixs,

Dont know if you can achive it by C# program. I think that is a camera config issue. Those green rectagles are from motion detection which probably triggers recording on DRV.
Maybe it is avaialbe to get rid off them by connecting to subchannel of this camera instead of main channel?

Best regards
 
hello guys,

can someone help me with NET_DVR_RealPlay_V40 function?
when i try to start live preview i get this error: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
this.host1.Handle is my WindowsFormsHost

NET_DVR_RealPlay_V40 import (class SDKMethods):
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_RealPlay_V40(int lUserID, Structs.LPNET_DVR_PREVIEWINFO lpPreviewInfo, Structs.REALDATACALLBACK fRealDataCallBack_V30, IntPtr pUser);

here my structures (class Structs):
public delegate void REALDATACALLBACK(int lPlayHandle, int dwDataType, IntPtr pBuffer, int dwBufSize, IntPtr pUser);

public struct LPNET_DVR_PREVIEWINFO
{
public long lChannel; // Channel no.
public int dwStreamType; // Stream type 0-main stream,1-sub stream,2-third stream,3-forth stream,4-fifth stream,5-sixth stream,7-seventh stream,8-eighth stream,9-ninth stream,10-tenth stream
public int dwLinkMode; // Protocol type: 0-TCP, 1-UDP, 2-Muticast, 3-RTP,4-RTP/RTSP, 5-RSTP/HTTP
public IntPtr hPlayWnd; // Play window's handle; set NULL to disable preview
public int bBlocked; //If data stream requesting process is blocked or not: 0-no, 1-yes
//if true, the SDK Connect failure return until 5s timeout , not suitable for polling to preview.
public int bPassbackRecord; //0- not enable ,1 enable
public byte byPreviewMode; // Preview mode 0-normal preview,2-delay preview
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = Defines.STREAM_ID_LEN, ArraySubType = UnmanagedType.I1)]
public byte[] byStreamID;//Stream ID
public byte byProtoType; //0-private,1-RTSP
public byte byRes1;
public byte byVideoCodingType;
public int dwDisplayBufNum; //soft player display buffer size(number of frames), range:1-50, default:1
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 216, ArraySubType = UnmanagedType.I1)]
public byte[] byRes;
}

and there is my calling function:
Structs.LPNET_DVR_PREVIEWINFO PrevInfo = new Structs.LPNET_DVR_PREVIEWINFO();
PrevInfo.hPlayWnd = this.host1.Handle;
PrevInfo.lChannel = 1;
PrevInfo.dwStreamType = 0;
PrevInfo.dwLinkMode = 0;
PrevInfo.bBlocked = 0;



int player = SDKMethods.NET_DVR_RealPlay_V40(UserID, PrevInfo, null, IntPtr.Zero);
Console.WriteLine(player);
 
Last edited:
i solve this error by putting 64bit dll's in source. But still, cant get live stream...

i am getting this error now:
107
Live view component is failed to upload
 
Last edited:
Hey Vilius,

I have found few errors in ur code. Changes marked with red bolded font. Let me know if it worked and if not I will paste my code snippets.


NET_DVR_RealPlay_V40 import (class SDKMethods):
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_RealPlay_V40(int lUserID, ref Structs.LPNET_DVR_PREVIEWINFO lpPreviewInfo, Structs.REALDATACALLBACK fRealDataCallBack_V30, IntPtr pUser);

here my structures (class Structs):
public delegate void REALDATACALLBACK(int lPlayHandle, uint dwDataType, byte[] pBuffer, uint dwBufSize, IntPtr pUser);

public struct LPNET_DVR_PREVIEWINFO
{
public int lChannel; // Channel no.
public uint dwStreamType; // Stream type 0-main stream,1-sub stream,2-third stream,3-forth stream,4-fifth stream,5-sixth stream,7-seventh stream,8-eighth stream,9-ninth stream,10-tenth stream
public uint dwLinkMode; // Protocol type: 0-TCP, 1-UDP, 2-Muticast, 3-RTP,4-RTP/RTSP, 5-RSTP/HTTP
public IntPtr hPlayWnd; // Play window's handle; set NULL to disable preview
public uint bBlocked; //If data stream requesting process is blocked or not: 0-no, 1-yes
//if true, the SDK Connect failure return until 5s timeout , not suitable for polling to preview.
public uint bPassbackRecord; //0- not enable ,1 enable
public byte byPreviewMode; // Preview mode 0-normal preview,2-delay preview
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = Defines.STREAM_ID_LEN, ArraySubType = UnmanagedType.I1)]
public byte[] byStreamID;//Stream ID
public byte byProtoType; //0-private,1-RTSP
public byte byRes1;
public byte byVideoCodingType;
public uint dwDisplayBufNum; //soft player display buffer size(number of frames), range:1-50, default:1
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 216, ArraySubType = UnmanagedType.I1)]
public byte[] byRes;
}

and there is my calling function:
Structs.LPNET_DVR_PREVIEWINFO PrevInfo = new Structs.LPNET_DVR_PREVIEWINFO();
PrevInfo.hPlayWnd = this.host1.Handle;
PrevInfo.lChannel = 33; //check your digital channels for DVR, number 1 which you assigned is probably analog one. For example my digital channels starts with 33
PrevInfo.dwStreamType = 0;
PrevInfo.dwLinkMode = 1;
PrevInfo.bBlocked = 0;
PrevInfo.byPreviewMode = 0;



int player = SDKMethods.NET_DVR_RealPlay_V40(UserID, ref PrevInfo, null, IntPtr.Zero);
Console.WriteLine(player);


Best regards
 
Hi curiousDeve,

i made changes, but i am still getting same error: 107: Live view component is failed to upload.
I tested Structs.LPNET_DVR_PREVIEWINFO configuration by making c++ dll. There i receive callback with every frame. But when i try to use this in c# code, i always receive same error code....
by the way, i am trying to connect to hikvision camera, not dvr. Everything works with demo sdk.
 
If my mind doesnt trick me that error also showed up when one of dlls of SDK was missing make sure u have them in right places.

Here i provide u my folders and file structures. Also i dont know if x64 version arent causing problems i am using x86.

EDIT 1:
I honestly admit i wasnt trying to connect directly to camera. I will try to do this but probably not today.

EDIT 2:
Of course it doesnt need to be at D disk. Just to make it clear, treat CppDlls as a root folder (doesnt matter what name it has and where it is) and HCNetSDKCom (this name is important dont change it) as subfolder.

upload_2018-8-7_15-16-39.png upload_2018-8-7_15-16-51.png
 
Last edited:
i reuploaded dll's from 32bit version and that made difference. by the usage of ram i can see that live preview is kinda working. Maybe you can provide source how you handle live video? for now, my callback isn't working. (getting error: 'A callback was made on a garbage collected delegate of type..)
 
Of course, try my code. Just keep in mind i've made it for DVR i dont know if there is any diffrence for direct connection to camera.

import of DLLs:

#region RealPlay_V40
public delegate void fRealDataCallBack_V30(int lRealHandle, uint dwDataType, byte[] pBuffer, uint dwBufSize, IntPtr pUser);
[DllImport(@"\Hikvision\CppDlls\HCNetSDK.dll", EntryPoint = "NET_DVR_RealPlay_V40")]
public static extern int NET_DVR_RealPlay_V40(int lUserID, ref NET_DVR_PREVIEWINFO lpPreviewInfo, fRealDataCallBack_V30 fRealDataCallBack_V30, IntPtr pUser);
#endregion RealPlay_V40

structure definition:

#region RealPlay_V40
public struct NET_DVR_PREVIEWINFO
{
public int lChannel; // Channel no.
public uint dwStreamType; // Stream type 0-main stream,1-sub stream,2-third stream,3-forth stream,4-fifth stream,5-sixth stream,7-seventh stream,8-eighth stream,9-ninth stream,10-tenth stream
public uint dwLinkMode; // Protocol type: 0-TCP, 1-UDP, 2-Muticast, 3-RTP,4-RTP/RTSP, 5-RSTP/HTTP
public IntPtr hPlayWnd; // Play window's handle; set NULL to disable preview
public uint bBlocked; //If data stream requesting process is blocked or not: 0-no, 1-yes
//if true, the SDK Connect failure return until 5s timeout , not suitable for polling to preview.
public uint bPassbackRecord; //0- not enable ,1 enable
public byte byPreviewMode; // Preview mode 0-normal preview,2-delay preview
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = SDKConst.STREAM_ID_LEN, ArraySubType = UnmanagedType.I1)]
public byte[] byStreamID;//Stream ID
public byte byProtoType; //0-private,1-RTSP
public byte byRes1;
public byte byVideoCodingType;
public uint dwDisplayBufNum; //soft player display buffer size(number of frames), range:1-50, default:1
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 216, ArraySubType = UnmanagedType.I1)]
public byte[] byRes;
}
#endregion RealPlay_V40


call:

public Result RealPlayTest_V40(IntPtr phWnd)
{
Result result = new Result(true);
//tt = WrapperClass.NET_DVR_Init();
SDKMethods.NET_DVR_Init();
SDKMethods.NET_DVR_SetConnectTime(2000, 1);
SDKMethods.NET_DVR_SetConnectTime(10000, 5);
int lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo = new NET_DVR_DEVICEINFO_V30();
string ip = "192.168.1.240";
ushort port = 8000;
string user = "user";
string password = "password";
lUserID = SDKMethods.NET_DVR_Login_V30(ip, port, user, password, ref struDeviceInfo);
if (lUserID < 0)
{
result.AddError(string.Format("Login failed, error code: {0}\n", SDKMethods.NET_DVR_GetLastError()));
result.IsSuccessful = false;
SDKMethods.NET_DVR_Cleanup();
return result;
}
SDKMethods.fRealDataCallBack_V30 callback = new SDKMethods.fRealDataCallBack_V30(g_RealDataCallBack_V30);
int realPlayId = 0;
NET_DVR_PREVIEWINFO PrevInfo = new NET_DVR_PREVIEWINFO();
PrevInfo.hPlayWnd = phWnd;
PrevInfo.lChannel = 36;
PrevInfo.dwStreamType = 0;
PrevInfo.dwLinkMode = 1;
PrevInfo.bBlocked = 0;
PrevInfo.byPreviewMode = 0;
realPlayId = SDKMethods.NET_DVR_RealPlay_V40(lUserID, ref PrevInfo, callback, IntPtr.Zero);
if (realPlayId < 0)
{
string message = string.Format("RealPlay_v40 failed, error code: {0}\n", SDKMethods.NET_DVR_GetLastError());
Console.WriteLine(message);
result.AddError(message);
result.IsSuccessful = false;
SDKMethods.NET_DVR_Cleanup();
return result;
}
//Streaming by ~10 seconds
int counter = 0;
while (counter < 10)
{
System.Threading.Thread.Sleep(1000);
counter++;
}
if (!SDKMethods.NET_DVR_StopRealPlay(realPlayId))
{
string message = string.Format("RealPlay_V40 failed, error code: {0}\n", SDKMethods.NET_DVR_GetLastError());
Console.WriteLine(message);
result.AddError(message);
result.IsSuccessful = false;
SDKMethods.NET_DVR_Cleanup();
return result;
}
return result;
}
public void g_RealDataCallBack_V30(int lRealHandle, uint dwDataType, byte[] pBuffer, uint dwBufSize, IntPtr pUser)
{
Console.WriteLine("Hello from g_RealDataCallBack_V30"); // for testing purposes
}

additional thing:

public class Result
{
private bool _isSuccessful;
private List<string> _errors;
private int _intValue = -1;
public Result()
{
_errors = new List<string>();
}
public Result(bool isSuccess)
{
_isSuccessful = isSuccess;
_errors = new List<string>();
}
public bool IsSuccessful
{
get
{
return _isSuccessful;
}
set
{
_isSuccessful = value;
}
}
public List<string> Errors
{
get
{
return _errors;
}
set
{
_errors = value;
}
}
public int IntValue
{
get
{
return _intValue;
}
set
{
_intValue = value;
}
}
public void AddError(string message)
{
_errors.Add(message);
}
}
 
curiousDev, thank you a lot for your help. I have solved all my issues with source code. No it seems that everything is working. i have live video stream.

you're welcome :)
I also learnt new thing. Now i know it works for camera as well.
 
Hello again,

maybe someone have any ideas how can i get image from live stream? i need to get image that going to be used in Neural Networks. I tried to get image from IntPtr, but no success..
 
Hello again,

maybe someone have any ideas how can i get image from live stream? i need to get image that going to be used in Neural Networks. I tried to get image from IntPtr, but no success..

I think you will need to use NET_DVR_CaptureJPEGPicture_NEW or NET_DVR_CaptureJPEGPicture method from SDK.
 
I also found two other methods NET_DVR_CapturePicture and NET_DVR_CapturePictureBlock it requires live stream handle so maybe that will be better approach for you?

Both of them have also _V50 version e.g. NET_DVR_CapturePicture_V50
 
Hi curiousDev, I'm having the same issue as Phonixs (green boxes from movement detection during live view)

you said: "Maybe it is avaialbe to get rid off them by connecting to subchannel of this camera instead of main channel?"

How do I figure out what are the subchannels of my cam?

Actually, I don't even know how to find the main channels, I found out by luck that my main channels start at 33 but I'm struggling to find the infos.

Thank you

Edit: I'm connecting to a dvr (16 channels) not a cam directly
 
Last edited:
i am not sure if this is correct. I am always getting error that buffer is not enough.

[DllImport(DLL)]
public static extern bool NET_DVR_CapturePicture_V50(int lUserID, int lChannel,ref Structs.LPNET_DVR_PICPARAM_V50 lpPicParam, ref byte[] sPicBuffer, int dwPicSize,ref ulong lpSizeReturned);

Structs.LPNET_DVR_PICPARAM_V50 picParam = new Structs.LPNET_DVR_PICPARAM_V50();


byte[] data = new byte[65530];
ulong Len = 0;
int BuffSize = 65530;

bool result = SDKMethods.NET_DVR_CapturePicture_V50(UserID, 1,ref picParam,ref data, BuffSize,ref Len);
if(!result)
{
Console.WriteLine(errors.ErrorByCode(SDKMethods.NET_DVR_GetLastError()));
}
 
Hi curiousDev, I'm having the same issue as Phonixs (green boxes from movement detection during live view)

you said: "Maybe it is avaialbe to get rid off them by connecting to subchannel of this camera instead of main channel?"

How do I figure out what are the subchannels of my cam?

Actually, I don't even know how to find the main channels, I found out by luck that my main channels start at 33 but I'm struggling to find the infos.

Thank you

Edit: I'm connecting to a dvr (16 channels) not a cam directly

Can you provide some more details?
Best to see a part of code which you use for that.
For now I can give u small tip it was even mention in code snippets by Vilius and me before in this thread. I am not sure if it will help you need to test it out.
public int dwStreamType; // Stream type 0-main stream,1-sub stream,2-third stream,3-forth stream,4-fifth stream,5-sixth stream,7-seventh stream,8-eighth stream,9-ninth stream,10-tenth stream
 
i am not sure if this is correct. I am always getting error that buffer is not enough.

[DllImport(DLL)]
public static extern bool NET_DVR_CapturePicture_V50(int lUserID, int lChannel,ref Structs.LPNET_DVR_PICPARAM_V50 lpPicParam, ref byte[] sPicBuffer, int dwPicSize,ref ulong lpSizeReturned);

Structs.LPNET_DVR_PICPARAM_V50 picParam = new Structs.LPNET_DVR_PICPARAM_V50();


byte[] data = new byte[65530];
ulong Len = 0;
int BuffSize = 65530;

bool result = SDKMethods.NET_DVR_CapturePicture_V50(UserID, 1,ref picParam,ref data, BuffSize,ref Len);
if(!result)
{
Console.WriteLine(errors.ErrorByCode(SDKMethods.NET_DVR_GetLastError()));
}

I am pretty sure that you have some problems with dll import. parameter types are incorrect. I will try to fix it for you but i dont have oportunity to test it in this moment.

[DllImport(DLL)] (i am not sure about 'ref byte[] sPicBuffer' can be a 'ref string sPicBuffer' or just 'string sPicBuffer')
public static extern bool NET_DVR_CapturePicture_V50(int lUserID, int lChannel, ref Structs.LPNET_DVR_PICPARAM_V50 lpPicParam, ref byte[] sPicBuffer, unit dwPicSize, ref uint lpSizeReturned);
or
public static extern bool NET_DVR_CapturePicture_V50(int lUserID, int lChannel, ref Structs.LPNET_DVR_PICPARAM_V50 lpPicParam, ref byte[] sPicBuffer, unit dwPicSize, ref IntPtr lpSizeReturned);

Let me know if it worked, ok?


And small advice to everyone look carefully at types of variables e.g. LONG in Hik SDK isnt a long it's a defined type
typedef int LONG;

Most of troubles with code is caused by wrong 'mapping' to C# types.
Tomorrow I will put a mapper to C# types of some common types from this SDK.