Here is my code where I use DllImport for "NET_DVR_RealPlay_V40", not VideoHelper class and the result is the same -1:
private bool InitHikvision()
{
try
{
Hik_Init = VideoHelper.NET_DVR_Init();
if (Hik_Init)
{
VideoHelper.NET_DVR_SetConnectTime(2000, 1);
VideoHelper.NET_DVR_SetReconnect(10000, 3);
}
else
{
frmObject.frmMain.to_log("Hikvision initialization: " + VideoHelper.NET_DVR_GetLastError());
var bmp = new Bitmap(videoPanel.ClientSize.Width, videoPanel.ClientSize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawString("Hikvision initialization: " + VideoHelper.NET_DVR_GetLastError(), new Font("Verdana", (float)12, FontStyle.Bold, GraphicsUnit.Pixel), new SolidBrush(Color.Red), (float)20, (float)20);
g.Dispose();
}
videoPanel.Image = bmp;
return false;
}
}
catch (Exception ex)
{
return false ;
}
return true;
}
private void GetCameraInfo()
{
try
{
if (id > 0)
{
cm = frmObject.GetCameraByID(id);
camera_port = cm.port;
camera_zone = cm.zone.ToString();
camera_ip = cm.ip;
camera_user_name = cm.user_name.ToString();
camera_pass = cm.password.ToString();
camera_name = cm.camera.ToString();
camera_type = cm.camera_type;
device_type = cm.device_type;
device_channel = cm.device_channel;
GetCameraType(camera_type);
ushort port = Convert.ToUInt16(camera_port);
Hik_DeviceInfo = new VideoHelper.NET_DVR_DEVICEINFO_V30();
Hik_LoginID = VideoHelper.NET_DVR_Login_V30(camera_ip, port, camera_user_name, camera_pass, ref Hik_DeviceInfo);
if (Hik_LoginID < 0)
{
VideoHelper.NET_DVR_Logout(Hik_LoginID);
VideoHelper.NET_DVR_Cleanup();
play_video = false;
frmObject.frmMain.to_log("Hikvision camera login: " + VideoHelper.NET_DVR_GetLastError());
var bmp = new Bitmap(videoPanel.ClientSize.Width, videoPanel.ClientSize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawString("Unable login of Hikvision camera: " + camera_ip + ":" + camera_port + " - " + camera_name, new Font("Verdana", (float)12, FontStyle.Bold, GraphicsUnit.Pixel), new SolidBrush(Color.Red), (float)20, (float)20);
g.Dispose();
}
videoPanel.Image = bmp;
return;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
[DllImport("HCNetSDK.dll", EntryPoint = "NET_DVR_RealPlay_V40")]
public static extern int NET_DVR_RealPlay_V40(int lUserID, ref LPNET_DVR_PREVIEWINFO lpPreviewInfo, fRealDataCallBack_V30 fRealDataCallBack_V30, 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;
}
private void RealPlay_Hikvision(int channel)
{
videoPanel.Refresh();
real_play = handle;
LPNET_DVR_PREVIEWINFO PrevInfo = new LPNET_DVR_PREVIEWINFO();
if (play_video == true)
{
VideoHelper.NET_DVR_StopRealPlay(realPlayId);
videoPanel.Refresh();
play_video = false;
}
try
{
PrevInfo.hPlayWnd = real_play;
PrevInfo.lChannel = channel; //Preview channel NO.
PrevInfo.dwStreamType = 0; //0-main stream, 1-sub stream, 2-stream3, 3-stream4.
PrevInfo.dwLinkMode = 0; //0-TCP mode, 1-UDP mode, 2-Multi-play mode, 3-RTP mode, 4-RTP/RTSP, 5-RTSP over HTTP
PrevInfo.bBlocked = 0;
PrevInfo.byPreviewMode = 0;
IntPtr pUser = new IntPtr(Hik_PrevInfo.lChannel);
realPlayId = NET_DVR_RealPlay_V40(Hik_LoginID, ref PrevInfo, null, IntPtr.Zero);
if (realPlayId < 0)
{
string message = string.Format("RealPlay_v40 failed, error code: {0}", VideoHelper.NET_DVR_GetLastError());
frmObject.frmMain.to_log(message);
var bmp = new Bitmap(videoPanel.ClientSize.Width, videoPanel.ClientSize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawString(message, new Font("Verdana", (float)12, FontStyle.Bold, GraphicsUnit.Pixel), new SolidBrush(Color.Red), (float)20, (float)20);
g.Dispose();
}
videoPanel.Image = bmp;
VideoHelper.NET_DVR_StopRealPlay(realPlayId);
play_video = false;
}
else
{
play_video = true;
}
}
catch (Exception ex)
{
play_video = false;
frmObject.frmMain.to_log("Video player, Hikvision connect: " + camera_name + " - " + camera_ip + ":" + camera_port + " - " + ex.Message);
var bmp = new Bitmap(videoPanel.ClientSize.Width, videoPanel.ClientSize.Height);
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawString("Video player, Hikvision connect: " + camera_name + " - " + camera_ip + ":" + camera_port + " - " + ex.Message, new Font("Verdana", (float)12, FontStyle.Bold, GraphicsUnit.Pixel), new SolidBrush(Color.Red), (float)20, (float)20);
g.Dispose();
}
videoPanel.Image = bmp;
VideoHelper.NET_DVR_StopRealPlay(realPlayId);
VideoHelper.NET_DVR_Logout(Hik_LoginID);
VideoHelper.NET_DVR_Cleanup();
}
}