how can i use C++ dll for my vb.net Project ?
i have this c++ dll function that i want to call from vb.net :
typedef void(CALLBACK *fRemoteConfigCallback)(DWORD dwType, void* lpBuffer, DWORD dwBufLen, void* pUserData);
NET_DVR_API LONG __stdcall NET_DVR_StartRemoteConfig(LONG lUserID, DWORD dwCommand, LPVOID lpInBuffer, DWORD dwInBufferLen, fRemoteConfigCallback cbStateCallback, LPVOID pUserData);
NET_DVR_API LONG __stdcall NET_DVR_GetNextRemoteConfig(LONG lHandle, void* lpOutBuff, DWORD dwOutBuffSize);
Here's the code that i made on my C# Wrapper to call the PInvoke :
public delegate void fRemoteConfigCallback(uint lCommand, IntPtr lpBuffer, uint dwBufLen, IntPtr pUserData);
[DllImport("HCNetSDK.dll", CallingConvention = CallingConvention.StdCall)]
public static extern long NET_DVR_StartRemoteConfig(long lUserID, uint dwCommand, IntPtr lpInBuffer, uint dwInBufferLen, fRemoteConfigCallback cbStateCallback, IntPtr pUserData);
[DllImport("HCNetSDK.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int NET_DVR_GetNextRemoteConfig(long lHandle, IntPtr lpOutBuff, uint dwOutBuffSize);
and i call the wrapper with this code :
Dim m_struAttendanceRecordCfg As NET_DVR_ATTENDANCE_RECORD_CFG = New NET_DVR_ATTENDANCE_RECORD_CFG()
a = Marshal.AllocHGlobal(Marshal.SizeOf(m_struAttendanceRecordCfg))
'error on this line
iret = NET_DVR_GetNextRemoteConfig(mHandle, a, Marshal.SizeOf(m_struAttendanceRecordCfg))
Marshal.StructureToPtr(m_struAttendanceRecordCfg, a, False)
Marshal.FreeHGlobal(a)
Based on what i search on google, it's because some data type of the variables doesn't match with the c++ dll ? what should i change so this code can works ? Thanks in advance.
EDIT : Here's my edited c# wrapper :
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_StartRemoteConfig(int lUserID, uint dwCommand, IntPtr lpInBuffer, uint dwInBufferLen, fRemoteConfigCallback cbStateCallback, IntPtr pUserData);
[DllImport("HCNetSDK.dll")]
public static extern int NET_DVR_GetNextRemoteConfig(int lHandle, IntPtr lpOutBuff, uint dwOutBuffSize);
vb.net code
Dim m_struSearchInfoCond As NET_DVR_SEARCH_INFO_COND = New NET_DVR_SEARCH_INFO_COND()
Dim m_struAttendanceRecordCfg As NET_DVR_ATTENDANCE_RECORD_CFG = New NET_DVR_ATTENDANCE_RECORD_CFG()
Dim a As IntPtr
a = Marshal.AllocHGlobal(Marshal.SizeOf(m_struSearchInfoCond))
Dim sizeOfA As UInt16 = Marshal.SizeOf(a)
'if i change mHandle as Uint32, it throw error Overflow
Dim mHandle As Long = NET_DVR_StartRemoteConfig(lUser, NET_DVR_GET_ATTENDANCE_RECORD_INFO,a, sizeOfA, Nothing, Nothing)
If mHandle < 0 Then
sizeOfA = NET_DVR_GetLastError()
End If
Marshal.StructureToPtr(m_struSearchInfoCond, a, False)
Marshal.FreeHGlobal(a)
Dim iret As Long = 0
While mHandle >= 0
a = Marshal.AllocHGlobal(Marshal.SizeOf(m_struAttendanceRecordCfg))
'if i change mHandle to long and pass it to this API (NET_DVR_GetNextRemoteConfig), it throw error "has unbalanced stack"
lUser = NET_DVR_GetNextRemoteConfig(mHandle, a, Marshal.SizeOf(m_struAttendanceRecordCfg))
Marshal.StructureToPtr(m_struAttendanceRecordCfg, a, False)
Marshal.FreeHGlobal(a)
fid = m_struAttendanceRecordCfg.dwEmployeeNo
End While