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

Aug 3, 2017
8
3
Dear All,

I am writing a Windows desktop application (x64 - C#) to embed DVR utilities by consuming Hikvision SDK for Windows.

I downloaded the SDK (Filename: Device Network SDK V5.2.3.3 (for Windows 64-bit)) from -> Hikvision Europe

I am able to Login, Play live Video, Audio.etc well, but not able to Use the Voice talk feature.

Here is my code for triggering the two way Audio.

try

Code:
{
int voiceHandle = NativeMethods.NET_DVR_StartVoiceCom_V30(userID, 1, false, new NativeMethods.VoiceDataCallBack(f_VoiceDataCallBack), IntPtr.Zero);
if (voiceHandle == -1)
{
uint l = NativeMethods.NET_DVR_GetLastError();
MessageBox.Show("Error code: " + l);
}

} 
catch (Exception ex) {
}

Error: Unhandled exception at 0x00007FFE9D9CC695 (AudioIntercom.dll) in HikVisionWinforms.exe: 0xC0000005: Access violation reading location 0x000000006A5F4178.

I had been struggling since days to fruitlessly figure out the reason for this. Looks like the Exception occurs in the code from API.

Please help me!

Thanks,
 
  • Like
Reactions: Mike and mat200
Hi,

Any progress with this issue? I noticed you are able to play live video and just wondering how your code looks for this part, can you share it?
Basically, I stuck on calling method NET_DVR_GetFileByTime which results with following error 17: Parameter error. Input or output parameter in the SDK API is NULL.
Maybe you had similar difficulties?

Best regards and thanks in advance.
 
Hey,

Not really helpful. I see you are using function which isn't having time struct as parameters. Let me explain by showing my code below.

Extern function from C++
[DllImport(@"D:\Projekty\HIK\Hikvision\EN-HCNetSDK(Windows32)V5.2.7.5_build20170217\lib\HCNetSDK.dll", EntryPoint = "NET_DVR_GetFileByTime")]
public static extern int NET_DVR_GetFileByTime(long lUserID, long lChannel, ref LPNET_DVR_TIME lpStartTime, ref LPNET_DVR_TIME lpStopTime, [MarshalAs(UnmanagedType.LPStr)] string sSavedFileName);

here is struct for time object:

[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
[Serializable]
public struct LPNET_DVR_TIME
{
public uint dwYear;
public uint dwMonth;
public uint dwDay;
public uint dwHour;
public uint dwMinute;
public uint dwSecond;
}

and finally call (of course before that I called NET_DVR_Init and NET_DVR_Login_V30):

LPNET_DVR_TIME lpStartTime = new LPNET_DVR_TIME();
lpStartTime.dwYear = 2017;
lpStartTime.dwMonth = 7;
lpStartTime.dwDay = 10;
lpStartTime.dwHour = 10;
lpStartTime.dwMinute = 10;
lpStartTime.dwSecond = 1;

LPNET_DVR_TIME lpStopTime = new LPNET_DVR_TIME();
lpStopTime.dwYear = 2017;
lpStopTime.dwMonth = 7;
lpStopTime.dwDay = 10;
lpStopTime.dwHour = 10;
lpStopTime.dwMinute = 15;
lpStopTime.dwSecond = 1;

int channel = 33;
int result = NET_DVR_GetFileByTime(0, channel, ref lpStartTime, ref lpStopTime, file);
if (result < 0)
{
Console.WriteLine("GetFileByTime error: {1}", NET_DVR_GetLastError());
//NET_DVR_Cleanup();
}

This code ending up with result mentioned by me before which is: GetFileByTime error: 17 (Parameter error. Input or output parameter in the SDK API is NULL).
Tried many diffrent way of calling this function always ending up the same (or in most cases - dont remember others errors though).
Also when I tired call functions which had similar input parameter (with time struct) result was the same - examples of other functions: NET_DVR_FindFile, NET_DVR_FindPDCInfo.


Best regards
 
Last edited:
Looks like you are talking about Playback (Playing a past recording). I was talking about Live Preview.
I haven't worked on Playback as such.
 
Let me know if I can be of any help further. I would like to know if you have ever worked on integrating Cp Plus or Dahua DVR?
 
Looks like you are talking about Playback (Playing a past recording). I was talking about Live Preview.
I haven't worked on Playback as such.
That's true. I am interested in past records. Thought maybe I can find any helpful info in your issue.

Let me know if I can be of any help further. I would like to know if you have ever worked on integrating Cp Plus or Dahua DVR?
I havent been doing anything with Dahua... yet. I am working with Hikvision NVR device basically in this moment I am interested in getting storaged video on this recorder but so far no luck. Login in works fine, and all tracks leads to problem with this time struct not sure though.
 
Hello,

Several days ago I have found a reason why it wasnt working just reminded myself about this topic here. Maybe it will help somebody.
Problem was with type mismatch.


[DllImport(@"D:\Projekty\HIK\Hikvision\EN-HCNetSDK(Windows32)V5.2.7.5_build20170217\lib\HCNetSDK.dll", EntryPoint = "NET_DVR_GetFileByTime")]
public static extern int NET_DVR_GetFileByTime(long lUserID, long lChannel, ref LPNET_DVR_TIME lpStartTime, ref LPNET_DVR_TIME lpStopTime, [MarshalAs(UnmanagedType.LPStr)] string sSavedFileName);

[DllImport(@"D:\Projekty\HIK\Hikvision\EN-HCNetSDK(Windows32)V5.2.7.5_build20170217\lib\HCNetSDK.dll", EntryPoint = "NET_DVR_GetFileByTime")]
public static extern int NET_DVR_GetFileByTime(int lUserID, int lChannel, ref LPNET_DVR_TIME lpStartTime, ref LPNET_DVR_TIME lpStopTime, [MarshalAs(UnmanagedType.LPStr)] string sSavedFileName);

Best regards
 
Can you try using the C# Code that contains "P invoke" Calls, for consuming C/C++ functions from below link.
HCNetSDK.cs in longchang | source code search engine
I used the funtion NET_DVR_RealPlay_V30 (After NET_DVR_Init and NET_DVR_Login_V30), to do a Live Preview.
Found helpful?
Hi Srinivas,
The link does not seem to work.My problem is that the call back function is not working.Please see my code below


[DllImport("HCNetSDK.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int NET_DVR_RealPlay_V30(int lUserID, ref NET_DVR_CLIENTINFO lpClientInfo, RealDataCallBack_V30 fRealDataCallBack_V30, int pUser, bool bBlocked);

public delegate void RealDataCallBack_V30(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser);

tt = WrapperClass.NET_DVR_Init();
tt = WrapperClass.NET_DVR_SetConnectTime((DWORD)9001, (DWORD)5);
tt = WrapperClass.NET_DVR_SetReconnect(10000, true);

LPNET_DVR_DEVICEINFO_V30 cc = new LPNET_DVR_DEVICEINFO_V30();
NET_DVR_CLIENTINFO lpClientInfo = new NET_DVR_CLIENTINFO();
int tt1 = 0;

tt1 = WrapperClass.NET_DVR_Login_V30("61.1.227.204", 8000, "admin", "mfin1028", out cc);
tt1 = (int)WrapperClass.NET_DVR_GetLastError();

lpClientInfo.lChannel = 1;
lpClientInfo.hPlayWnd = pictureBox1.Handle.ToString();
lpClientInfo.lLinkMode = 0;
//lpClientInfo.sMultiCastIP = "";

g_RealDataCallBack_V = g_RealDataCallBack_V30;
Marshal.GetFunctionPointerForDelegate(g_RealDataCallBack_V);


var m_lRealHandle = WrapperClass.NET_DVR_RealPlay_V30(tt1, ref lpClientInfo, g_RealDataCallBack_V, 0, false);

tt1 = (int)WrapperClass.NET_DVR_GetLastError();

var test = WrapperClass.NET_DVR_SetRealDataCallBack(m_lRealHandle, g_RealDataCallBack_V,0);
tt1 = (int)WrapperClass.NET_DVR_GetLastError();


public void g_RealDataCallBack_V30(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser)
{
//Debugger.Break();
MessageBox.Show("Test");
textBox1.Text = "Started123123123";




}


The function g_RealDataCallBack_V30 is not getting called .
 
Can you try using the C# Code that contains "P invoke" Calls, for consuming C/C++ functions from below link.
HCNetSDK.cs in longchang | source code search engine
I used the funtion NET_DVR_RealPlay_V30 (After NET_DVR_Init and NET_DVR_Login_V30), to do a Live Preview.
Found helpful?

I am able to initialize and login. Could you please send me the code to get live stream using NET_DVR_RealPlay_V30 and show in the windows form? I am new to C# and windows forms. Please help me with that. Thanks in advance.
 
Hello Bobby,

Hi Srinivas,
The link does not seem to work.My problem is that the call back function is not working.Please see my code below


[DllImport("HCNetSDK.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int NET_DVR_RealPlay_V30(int lUserID, ref NET_DVR_CLIENTINFO lpClientInfo, RealDataCallBack_V30 fRealDataCallBack_V30, int pUser, bool bBlocked);

public delegate void RealDataCallBack_V30(int lRealHandle, uint dwDataType, IntPtr pBuffer, uint dwBufSize, IntPtr pUser);

...

I guess i can help with that if it's not too late now. Just took a look on this forum 1,5h ago. Probably type of pBuffer was an issue in ur case since u used it as IntPtr i did byte[] in my program but i can be wrong i simply made my own solution based on ur step and what you wanted to archive.

here is my test class for ur problem. I ran it and it worked fine. Callback method works also but it's not necessary to see the stream in fact it was even disturbing but you will check it on ur own.
I also added the stoping method. If you will have any trouble let me know I will try to help.

public class Test
{
public Result RealPlayTest(IntPtr phWnd)
{
Result result = new Result(true);

//tt = WrapperClass.NET_DVR_Init();
SDKMethods.NET_DVR_Init();
//tt = WrapperClass.NET_DVR_SetConnectTime((DWORD)9001, (DWORD)5);
SDKMethods.NET_DVR_SetConnectTime(2000, 1);
//tt = WrapperClass.NET_DVR_SetReconnect(10000, true);
SDKMethods.NET_DVR_SetConnectTime(10000, 5);


//NET_DVR_DEVICEINFO_V30 cc = new NET_DVR_DEVICEINFO_V30();
//NET_DVR_CLIENTINFO lpClientInfo = new NET_DVR_CLIENTINFO();
//int tt1 = 0;

//tt1 = WrapperClass.NET_DVR_Login_V30("61.1.227.204", 8000, "admin", "mfin1028", out cc);
//tt1 = (int)WrapperClass.NET_DVR_GetLastError();
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;
}

NET_DVR_CLIENTINFO lpClientInfo = new NET_DVR_CLIENTINFO();
lpClientInfo.lChannel = 36; //IMPORTANT!! to get video or life stream u need to connect to digital channels not analog one, in my case digital channels starts with 33 but i am using '3rd' camera
//lpClientInfo.hPlayWnd = pictureBox1.Handle.ToString();
lpClientInfo.hPlayWnd = phWnd; // pictureBox1.Handle.ToString();
lpClientInfo.lLinkMode = 0;

//(int lRealHandle, uint dwDataType, byte[] pBuffer, uint dwBufSize, IntPtr pUser);
SDKMethods.fRealDataCallBack_V30 callback = new SDKMethods.fRealDataCallBack_V30(g_RealDataCallBack_V30);
//Marshal.GetFunctionPointerForDelegate(g_RealDataCallBack_V);

IntPtr userPtr = new IntPtr();
int realPlayId = 0;
realPlayId = SDKMethods.NET_DVR_RealPlay_V30(lUserID, ref lpClientInfo, callback, userPtr, false);
if (realPlayId < 0)
{
string message = string.Format("RealPlay 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 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)
public void g_RealDataCallBack_V30(int lRealHandle, uint dwDataType, byte[] pBuffer, uint dwBufSize, IntPtr pUser)
{
//Debugger.Break();
Console.WriteLine("Hello from g_RealDataCallBack_V30");
//MessageBox.Show("Test");
}
}

here my C++ functions import (just limited myself to those new ones since I believe your Init, reconnect and login methods are working fine) If u will have some problems let me know so I will put missing parts as well if you will point them for me.

public delegate void fRealDataCallBack_V30(int lRealHandle, uint dwDataType, byte[] pBuffer, uint dwBufSize, IntPtr pUser);
[DllImport(@"D:\Projekty\Hik\Hikvision\EN-HCNetSDK(Windows32)V5.2.7.5_build20170217\lib\HCNetSDK.dll", EntryPoint = "NET_DVR_RealPlay_V30")]
public static extern int NET_DVR_RealPlay_V30(int lUserID, ref NET_DVR_CLIENTINFO lpClientInfo, fRealDataCallBack_V30 cbRealDataCallBack, IntPtr pUser, bool bBlocked);
[DllImport(@"D:\Projekty\Hik\Hikvision\EN-HCNetSDK(Windows32)V5.2.7.5_build20170217\lib\HCNetSDK.dll", EntryPoint = "NET_DVR_StopRealPlay")]
public static extern bool NET_DVR_StopRealPlay(int lRealHandle);

and of course calling this all. I am using console app but i think it's not a problem

static void Main(string[] args)
{

Test test = new Test();
IntPtr hWnd = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; //for console application
//IntPtr hWnd = this.Handle; //for winforms application
test.RealPlayTest(hWnd);
Console.ReadKey();
}

Best regards
 
Last edited:
  • Like
Reactions: annevah
Hi is anyone still here?

First of all thanks a ton. I have being searching for how to use the SDK and this thread saved me. I wanted to know how to view the live streaming ? You guys helped on initializing, logging in and calling the NET_DVR_RealPlay_V30() for live view. What should I do to view the streaming?

I also wanted to use PTZ control on the camera any guidance for that too will be of a great help.
 
Hi Annevah,

Here is the Code to Login into the DVR and Start Streaming live Video.

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
string ip = "192.168.0.240";
string port = "25001";
string userName = "admin";
string password = "12345";
NET_DEVICEINFO_Ex deviceInfo = new NET_DEVICEINFO_Ex();
int error = 0;
try
{
//call login function.
ushort dev_port = 0;
try
{
dev_port = Convert.ToUInt16(port);
}
catch (Exception ex)
{
MessageBox.Show(this, (string)App.Current.Resources["ID_PORTERROR"]);
return;
}
loginID = NETClient.Login(ip, dev_port, userName, password, EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref deviceInfo);
if (loginID != IntPtr.Zero)
{
IntPtr cam1Handle = host1.Child.Handle;
IntPtr cam2Handle = host2.Child.Handle;
realHandle0 = NETClient.StartRealPlay(loginID, 0, cam1Handle, EM_RealPlayType.Realplay, m_RealDataCallBack, m_RealPlayDisConnectCallBack, IntPtr.Zero, TimeOut);
IntPtr realHandle1 = NETClient.StartRealPlay(loginID, 1, cam2Handle, EM_RealPlayType.Realplay, m_RealDataCallBack, m_RealPlayDisConnectCallBack, IntPtr.Zero, TimeOut);
}
}
catch (Exception ex)
{
if (ex is NETClientExcetion)
{
MessageBox.Show(this, (ex as NETClientExcetion).Message);
}
else
{
MessageBox.Show(this, ex.Message);
}
}
}
 
  • Like
Reactions: annevah
Hi Annevah,

Here is the Code to Login into the DVR and Start Streaming live Video.

private void btnLogin_Click(object sender, RoutedEventArgs e)
{
string ip = "192.168.0.240";
string port = "25001";
string userName = "admin";
string password = "12345";
NET_DEVICEINFO_Ex deviceInfo = new NET_DEVICEINFO_Ex();
int error = 0;
try
{
//call login function.
ushort dev_port = 0;
try
{
dev_port = Convert.ToUInt16(port);
}
catch (Exception ex)
{
MessageBox.Show(this, (string)App.Current.Resources["ID_PORTERROR"]);
return;
}
loginID = NETClient.Login(ip, dev_port, userName, password, EM_LOGIN_SPAC_CAP_TYPE.TCP, IntPtr.Zero, ref deviceInfo);
if (loginID != IntPtr.Zero)
{
IntPtr cam1Handle = host1.Child.Handle;
IntPtr cam2Handle = host2.Child.Handle;
realHandle0 = NETClient.StartRealPlay(loginID, 0, cam1Handle, EM_RealPlayType.Realplay, m_RealDataCallBack, m_RealPlayDisConnectCallBack, IntPtr.Zero, TimeOut);
IntPtr realHandle1 = NETClient.StartRealPlay(loginID, 1, cam2Handle, EM_RealPlayType.Realplay, m_RealDataCallBack, m_RealPlayDisConnectCallBack, IntPtr.Zero, TimeOut);
}
}
catch (Exception ex)
{
if (ex is NETClientExcetion)
{
MessageBox.Show(this, (ex as NETClientExcetion).Message);
}
else
{
MessageBox.Show(this, ex.Message);
}
}
}
 
Thank you so much for replying so quick. If you don't mind what does NETClient.StartRealPlay do?
IntPtr realHandle1 = NETClient.StartRealPlay(loginID, 1, cam2Handle, EM_RealPlayType.Realplay, m_RealDataCallBack, m_RealPlayDisConnectCallBack, IntPtr.Zero, TimeOut);

How to view the stream? I am stuck at that. NET_DVR_RealPlay_V30 return a integer. How can I display the stream in a wpf app? Is it through the rspt link? Do you have a doc I can look at?
 
But there isn't a function named StartRealPlay in that code :/

NET_DVR_RealPlay_V30() method return a int. How can i display the stream with that?

I am absolutely lost. Please help
 
That was just a reference Code.

I am attaching the Libraries (add ref to Lib/NETSDKCS.dll) which you can include in your WPF application & start making calls.

Here is a sample Form.

The View:

<Window x:Class="IotDesktopApp.DvrDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:IotDesktopApp"
xmlns:host="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
mc:Ignorable="d"
Title="DvrDemo" Height="1000" Width="925.532">
<Grid Margin="0,0,-187,-190">
<Button x:Name="btnLogin" Click="btnLogin_Click" Content="Login" HorizontalAlignment="Left" Margin="82,23,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-3.97,1.022"/>

<WindowsFormsHost Name="host1" Margin="32,79,696,585">
<winform:panel BorderStyle="Fixed3D" Name="pnl1" Height="400" Width="600"/>
</WindowsFormsHost>

<WindowsFormsHost Name="host2" Margin="439,79,294,585">
<winform:panel BorderStyle="Fixed3D" Name="pnl2" Height="400" Width="600"/>
</WindowsFormsHost>

<Button x:Name="btnOpenSound" Click="btnOpenSound_Click" Content="OpenSound" HorizontalAlignment="Left" Margin="222,23,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btnCloseSound" Click="btnCloseSound_Click" Content="CloseSound" HorizontalAlignment="Left" Margin="312,23,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btnTwoWay" Click="btnTwoWay_Click" Content="Start Two way" HorizontalAlignment="Left" Margin="473,23,0,0" VerticalAlignment="Top" Width="88"/>
<Button x:Name="btnTwoWayStop" Click="btnTwoWayStop_Click" Content="StopTwoWay" HorizontalAlignment="Left" Margin="585,23,0,0" VerticalAlignment="Top" Width="75"/>
<Button x:Name="btnShowLastError" Click="btnShowLastError_Click" Content="Show Last error" HorizontalAlignment="Left" Margin="10,51,0,0" VerticalAlignment="Top" Width="110"/>

</Grid>
</Window>
----------------------------------------------------------------------------

The Code behind is that which I have already posted above.
 

Attachments

Last edited:
This post has been very helpful with my development. I have just downloaded the latest SDK and have noticed there is a new Login function "
NET_DVR_Login_V40".

I am trying to use it but unfortunately I keep getting error 17, Parameter invalid.

I have spent a long time and I cannot see the error of my way maybe someone can check my wrapper and ensure that it is correct and if they can use the new SDK function correctly. I have a feeling it may be in my wrapper struct declaration.

Thanks in advance.

[DllImport("HCNetSDK.dll")]
/**
* LONG NET_DVR_Login_V40( LPNET_DVR_USER_LOGIN_INFO pLoginInfo,
* LPNET_DVR_DEVICEINFO_V40 lpDeviceInfo);
*/
public static extern int NET_DVR_Login_V40(LPNET_DVR_USER_LOGIN_INFO pLoginInfo,
out LPNET_DVR_DEVICEINFO_V40 lpDeviceInfo);

/**
* typedef struct
{
char sDeviceAddress[NET_DVR_DEV_ADDRESS_MAX_LEN];
BYTE byUseTransport;
WORD wPort;
char sUserName[NET_DVR_LOGIN_USERNAME_MAX_LEN];
char sPassword[NET_DVR_LOGIN_PASSWD_MAX_LEN];
fLoginResultCallBack cbLoginResult;
void *pUser;
BOOL bUseAsynLogin;
BYTE byProxyType;
BYTE byUseUTCTime;
BYTE byRes2[2];
LONG iProxyID;
BYTE byRes3[120];
}NET_DVR_USER_LOGIN_INFO,*LPNET_DVR_USER_LOGIN_INFO;

*/
public struct LPNET_DVR_USER_LOGIN_INFO
{
public string sDeviceAddress;
public byte byRes1;
public ushort wPort;
public string sUserName;
public string sPassword;
public fLoginResultCallBack cbLoginResult;
public IntPtr pUser;
public bool bUseAsynLogin;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] byRes2;
}

/**
* typedef void(CALLBACK *fLoginResultCallBack)(
LONG lUserID,
DWORD dwResult,
LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo,
void *pUser
);
*/
public delegate void fLoginResultCallBack(int lUserID, uint dwResult,
NET_DVR_DEVICEINFO_V30 lpDeviceInfo, IntPtr pUser);

/*struct{
NET_DVR_DEVICEINFO_V30 struDeviceV30;
BYTE bySupportLock;
BYTE byRetryLoginTime;
BYTE byPasswordLevel;
BYTE byRes1;
DWORD dwSurplusLockTime;
BYTE byCharEncodeType;
BYTE byRes2[255];
}NET_DVR_DEVICEINFO_V30,*LPNET_DVR_DEVICEINFO_V30;
*/
public struct LPNET_DVR_DEVICEINFO_V40
{
public NET_DVR_DEVICEINFO_V30 struDeviceV30;
public byte bySupportLock;
public byte byRetryLoginTime;
public byte byPasswordLevel;
public byte byRes1;
public uint dwSurplusLockTime;
public byte byCharEncodeType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
public byte[] byRes2;

}
 
This post has been very helpful with my development. I have just downloaded the latest SDK and have noticed there is a new Login function "
NET_DVR_Login_V40".

I am trying to use it but unfortunately I keep getting error 17, Parameter invalid.

I have spent a long time and I cannot see the error of my way maybe someone can check my wrapper and ensure that it is correct and if they can use the new SDK function correctly. I have a feeling it may be in my wrapper struct declaration.

Thanks in advance.

[DllImport("HCNetSDK.dll")]
/**
* LONG NET_DVR_Login_V40( LPNET_DVR_USER_LOGIN_INFO pLoginInfo,
* LPNET_DVR_DEVICEINFO_V40 lpDeviceInfo);
*/
public static extern int NET_DVR_Login_V40(LPNET_DVR_USER_LOGIN_INFO pLoginInfo,
out LPNET_DVR_DEVICEINFO_V40 lpDeviceInfo);

/**
* typedef struct
{
char sDeviceAddress[NET_DVR_DEV_ADDRESS_MAX_LEN];
BYTE byUseTransport;
WORD wPort;
char sUserName[NET_DVR_LOGIN_USERNAME_MAX_LEN];
char sPassword[NET_DVR_LOGIN_PASSWD_MAX_LEN];
fLoginResultCallBack cbLoginResult;
void *pUser;
BOOL bUseAsynLogin;
BYTE byProxyType;
BYTE byUseUTCTime;
BYTE byRes2[2];
LONG iProxyID;
BYTE byRes3[120];
}NET_DVR_USER_LOGIN_INFO,*LPNET_DVR_USER_LOGIN_INFO;

*/
public struct LPNET_DVR_USER_LOGIN_INFO
{
public string sDeviceAddress;
public byte byRes1;
public ushort wPort;
public string sUserName;
public string sPassword;
public fLoginResultCallBack cbLoginResult;
public IntPtr pUser;
public bool bUseAsynLogin;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 128)]
public byte[] byRes2;
}

/**
* typedef void(CALLBACK *fLoginResultCallBack)(
LONG lUserID,
DWORD dwResult,
LPNET_DVR_DEVICEINFO_V30 lpDeviceInfo,
void *pUser
);
*/
public delegate void fLoginResultCallBack(int lUserID, uint dwResult,
NET_DVR_DEVICEINFO_V30 lpDeviceInfo, IntPtr pUser);

/*struct{
NET_DVR_DEVICEINFO_V30 struDeviceV30;
BYTE bySupportLock;
BYTE byRetryLoginTime;
BYTE byPasswordLevel;
BYTE byRes1;
DWORD dwSurplusLockTime;
BYTE byCharEncodeType;
BYTE byRes2[255];
}NET_DVR_DEVICEINFO_V30,*LPNET_DVR_DEVICEINFO_V30;
*/
public struct LPNET_DVR_DEVICEINFO_V40
{
public NET_DVR_DEVICEINFO_V30 struDeviceV30;
public byte bySupportLock;
public byte byRetryLoginTime;
public byte byPasswordLevel;
public byte byRes1;
public uint dwSurplusLockTime;
public byte byCharEncodeType;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 255)]
public byte[] byRes2;

}

Hi Mechalec,

I put some code under. I hope it be useful for you if you didnt figure it out on your own yet.
Also i will add that SDK Programming Manual has some errors and some of definitions of struct arn't full in it I advice to look at definitions from header file HCNetSDK.h

In my humble opinion Login_V30 is enough but maybe you need V40 for some certain purposes.

DLL import part and definition of delegate (of course i assume your NET_DVR_Init and NET_DVR_SetConnectTime and NET_DVR_SetReconnect are correct so i won't put them here):

public delegate void fLoginResultCallBack(int lUserID, uint dwResult, NET_DVR_DEVICEINFO_V30 lpDeviceInfo, IntPtr pUser);

[DllImport(@"\Hikvision\CppDlls\HCNetSDK.dll", EntryPoint = "NET_DVR_Login_V40")]
public static extern int NET_DVR_Login_V40(ref NET_DVR_USER_LOGIN_INFO pLoginInfo, ref NET_DVR_DEVICEINFO_V40 lpDeviceInfo);

structure definition:

#region Login_V40
public struct NET_DVR_USER_LOGIN_INFO
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = SDKConst.NET_DVR_DEV_ADDRESS_MAX_LEN)]
public string sDeviceAddress;
public byte byRes1;
public ushort wPort;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = SDKConst.NET_DVR_LOGIN_USERNAME_MAX_LEN)]
public string sUserName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = SDKConst.NET_DVR_LOGIN_PASSWD_MAX_LEN)]
public string sPassword;
public fLoginResultCallBack cbLoginResult;
public IntPtr pUser;
public bool bUseAsynLogin;
public byte byProxyType;
public byte byUseUTCTime;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 2, ArraySubType = UnmanagedType.I1)]
public byte[] byRes2;
public int iProxyID;
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 120, ArraySubType = UnmanagedType.I1)]
public byte[] byRes3;
}

public struct NET_DVR_DEVICEINFO_V40
{
public NET_DVR_DEVICEINFO_V30 struDeviceV30;
public byte bySupportLock;
public byte byRetryLoginTime;
public byte byPasswordLevel;
public byte byProxyType; //Proxy Type,0-not use proxy, 1-use socks5 proxy, 2-use EHome proxy
public uint dwSurplusLockTime; //surplus locked time
public byte byCharEncodeType; //character encode type
public byte bySupportDev5;//Support v50 version of the device parameters, device name and device type name length is extended to 64 bytes
[MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 254, ArraySubType = UnmanagedType.I1)]
public byte[] byRes2;
}
#endregion Login_V40


calling part:


public string Execute_V40(string ip, ushort port, string user, string password, uint channel, string fullPath, DateTime start, DateTime stop)
{
string message = string.Empty;
//---------------------------------------
// Initialize
SDKMethods.NET_DVR_Init();
//Set connected time and reconnected time
SDKMethods.NET_DVR_SetConnectTime(2000, 1);
SDKMethods.NET_DVR_SetReconnect(10000, true);
int lUserID = -1;
//Device information, output parameters
NET_DVR_DEVICEINFO_V30 struDeviceInfo = new NET_DVR_DEVICEINFO_V30();
NET_DVR_DEVICEINFO_V40 struDeviceInfo_V40 = new NET_DVR_DEVICEINFO_V40();
NET_DVR_USER_LOGIN_INFO loginInfo_V40 = new NET_DVR_USER_LOGIN_INFO();
loginInfo_V40.sDeviceAddress = ip;
loginInfo_V40.sUserName = user;
loginInfo_V40.sPassword = password;
loginInfo_V40.wPort = port;
loginInfo_V40.pUser = IntPtr.Zero;
//if you want to use async call then result will be in fLoginResultCallBack_Event method
//during tests i fugured out that there is a mistake in SDK Programming Manual
// dwResult
//[out] Login status: 0- Asynchronous login succeeded, 1- Asynchronous login failed // this part is opposite: 1 is succeeded and 0 is failed

loginInfo_V40.bUseAsynLogin = true;
loginInfo_V40.cbLoginResult = fLoginResultCallBack_Event;
lUserID = SDKMethods.NET_DVR_Login_V40(ref loginInfo_V40, ref struDeviceInfo_V40);
if (lUserID < 0)
{
message = string.Format("Login failed, error code: {0}\n", SDKMethods.NET_DVR_GetLastError());
SDKMethods.NET_DVR_Cleanup();
return message;
}
//System.Threading.Thread.Sleep(10000);
return null;
}


// dwResult (repeated from above)
// [out] Login status: 0- Asynchronous login succeeded, 1- Asynchronous login failed // this part is opposite: 1 is succeeded and 0 is failed

public void fLoginResultCallBack_Event(int lUserID, uint dwResult, NET_DVR_DEVICEINFO_V30 lpDeviceInfo, IntPtr pUser)
{
Console.WriteLine(string.Format("fLoginResultCallBack_Event has been called {0} {1}", lUserID, dwResult));
}



if you will have any problems let me know i will try to help.

Best regards
 
Last edited: