views:

433

answers:

5

Hello, I´m having some problem to convert my VB6 project to VB.NET

I don't understand how this "AddressOf" function should be in VB.NET

My VB6 code:

Declare Function MP4_ClientStart Lib "hikclient.dll" _
  (pClientinfo As CLIENT_VIDEOINFO, ByVal abab As Long) As Long

Public Sub ReadDataCallBack(ByVal nPort As Long, pPacketBuffer As Byte, _
  ByVal nPacketSize As Long)

  If Not bSaved_DVS Then
    bSaved_DVS = True
    HW_OpenStream hChannelHandle, pPacketBuffer, nPacketSize
  End If
    HW_InputData hChannelHandle, pPacketBuffer, nPacketSize

End Sub

nn1 = MP4_ClientStart(clientinfo, AddressOf ReadDataCallBack)
+2  A: 

Regarding callbacks in unmanaged code see if this similar post helps you.

Regarding your question - I don't think you need callback functions or the example you posted is not correct/complet - see the post indicated above and clarify your code sample.

Ando
yes , its not the complete code, just everything that has with the addressof function to do,and I need the callback function
johan
+1  A: 

I assume that the second parameter to MP4_ClientStart is supposed to be the address of a callback function. Likely the problem is that you've defined it here as a Long, which in VB6 is a 32-bit value, but in VB.NET is a 64-bit value. You'll probably have some success by changing your declaration to:

Declare Function MP4_ClientStart Lib "hikclient.dll" _
    (pClientinfo As CLIENT_VIDEOINFO, ByVal abab As Integer) As Integer
Jim Mischel
+2  A: 

You are probably seeing this error:

'AddressOf' expression cannot be converted to 'Long' because 'Long' is not a delegate type.

What you probably want to do is create a delegate then change the type of adab to that delegate type. Add this to the class:

Public Delegate Sub ReadDataCallBackDelegate(ByVal nPort As Long, _
  ByVal pPacketBuffer As Byte, ByVal nPacketSize As Long)

Then change your P/Invoke declaration to:

Declare Function MP4_ClientStart Lib "hikclient.dll" (ByVal pClientinfo As _
  CLIENT_VIDEOINFO, ByVal abab As ReadDataCallBackDelegate) As Long

Do not delete/change your ReadDataCallBack Sub, you still need that.

At that point he compiler should be happy. However, the point made by others is important. The length of Integers and Longs is different in VB6 than in VB.NET. So in .NET you need to use Integer anytime you used a Long in VB6.

magnifico
+1, this is the correct answer.
Hans Passant
A: 

Very nice, thanks!

I did it like this

VB.NET code:

Declare Function MP4_ClientStart Lib "hikclient.dll" (ByRef pClientinfo As _
  CLIENT_VIDEOINFO, ByVal abab As ReadDataCallBackDelegate) As Integer

Public Delegate Sub ReadDataCallBackDelegate(ByVal nPort As Long, _
  ByRef pPacketBuffer As Byte, ByVal nPacketSize As Long)

Public Sub ReadDataCallBack(ByVal nPort As Integer, ByRef pPacketBuffer As _
  Byte, ByVal nPacketSize As Integer)

  If Not bSaved_DVS Then
    bSaved_DVS = True
    HW_OpenStream(hChannelHandle, pPacketBuffer, nPacketSize)
  End If
  HW_InputData(hChannelHandle, pPacketBuffer, nPacketSize)

End Sub

MP4_ClientStart(clientinfo, AddressOf ReadDataCallBack)
johan
A: 

你好,能解释一下为什么要这么做吗?我测试的好像有问题哦。

Dodu