开发者

" addressof " VB6 to VB.NET

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)


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.


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.


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


Here is the VB.NET implementation:

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜