CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2014
    Posts
    1

    Problems with Callbacks

    Hi, this is my first post here. First of all, sorry for my English.

    I'm having problems with the code below, is an SDK for a DVR security camera, the example that comes with SDK are in VB6 and I am trying to use in VB.Net.
    That's the original code:

    Code:
    'API initialization statement
    Public Declare Function CLIENT_Init Lib "dhnetsdk.dll" (ByVal fDisConnect As Long, _
    ByVal wDVRPort As Long) As Long
    
    'Login API
    Public Declare Function CLIENT_Login Lib "dhnetsdk.dll" _
    (ByVal pchDVRIP As String, _
    ByVal wDVRPort As Long, _
    ByVal pchUserName As String, _
    ByVal pchPassword As String, _
    lpDeviceInfo As NET_DEVICEINFO, _
    ByRef error As NET_RET_ERROR) As Long
    
    'Logout device
    Public Declare Function CLIENT_Logout Lib "dhnetsdk.dll" (ByVal lLoginID As Long) As Long
    
    'Realplay API
    Public Declare Function CLIENT_RealPlay Lib "dhnetsdk.dll" _
    (ByVal lLoginID As Long, ByVal nChannelID As Long, ByVal hwnd As Long) As Long
    
    Public Declare Function CLIENT_RealPlayEx Lib "dhnetsdk.dll" _
    (ByVal lLoginID As Long, ByVal nChannelID As Long, ByVal hwnd As Long, _
    ByVal rType As Long) As Long
    
    'Stop Realplay
    Public Declare Function CLIENT_StopRealPlayEx Lib "dhnetsdk.dll" (ByVal lLoginID As Long) As Long
    
    Public Declare Sub CLIENT_Cleanup Lib "dhnetsdk.dll" ()
    
    'Alarm Liten API
    Public Declare Function CLIENT_StartListen Lib "dhnetsdk.dll" _
    (ByVal lLoginID As Long) As Long
    
    'Mess callback API
    Public Declare Function CLIENT_SetDVRMessCallBack Lib "dhnetsdk.dll" (ByVal fMessCallBack As Long, _
    ByVal dwUser As Long) As Long
    
    Public Type NET_DEVICEINFO      'É豸µÄ½á¹¹Ìå
        sSerialNumber As String * 48
        byAlarmInPortNum As Byte
        byAlarmOutPortNum As Byte
        byDiskNum As Byte
        byDVRType As Byte
        byChanNum As Byte
    End Type
    
    Public Type NET_RET_ERROR
        errcode As Integer
    End Type
    
    '³õʼ»¯»Øµ÷º¯Êý
    Public Sub IniFunc(ByVal lLoginID As Long, ByVal pchDVRIP As Long, ByVal nDVRPort As Long, ByVal dwUser As Long)
    Form1.Text1.Text = lLoginID
    End Sub
    
    'ÏûÏ¢»Øµ÷º¯Êý
    
    Public Sub MessFunc(ByVal lCommand As Long, _
      ByVal lLoginID As Long, ByVal pBuf As Long, _
      ByVal dwBufLen As Long, ByVal pchDVRIP As Long, _
      ByVal nDVRPort As Long, ByVal dwUser As Long)
    
    
        Form1.Text1.Text = lLoginID
        Form1.Text2.Text = pBuf
        Form1.Text3.Text = pchDVRIP
    
    End Sub
    
    
    '»Øµ÷º¯ÊýÖ¸Õë´«Èë
    Public Function PtrToLong(ByVal func As Long) As Long
        PtrToLong = func
    End Function
    I have "converted" to this one:

    Code:
    Public Class Form1
        Public Declare Function CLIENT_Init Lib "dhnetsdk.dll" (ByVal fDisConnect As Integer, _
    ByVal wDVRPort As Long) As Long
    
    
    
        'Login API
        Public Declare Function CLIENT_Login Lib "dhnetsdk.dll" (ByVal pchDVRIP As String, ByVal wDVRPort As Long, ByVal pchUserName As String, ByVal pchPassword As String, lpDeviceInfo As NET_DEVICEINFO, ByRef err As NET_RET_ERROR) As Long
    
        'Logout device
        Public Declare Function CLIENT_Logout Lib "dhnetsdk.dll" (ByVal lLoginID As Long) As Long
    
        'Realplay API
        Public Declare Function CLIENT_RealPlay Lib "dhnetsdk.dll" _
        (ByVal lLoginID As Long, ByVal nChannelID As Long, ByVal hwnd As Long) As Long
    
        Public Declare Function CLIENT_RealPlayEx Lib "dhnetsdk.dll" _
        (ByVal lLoginID As Long, ByVal nChannelID As Long, ByVal hwnd As Long, _
        ByVal rType As Long) As Long
    
        'Stop Realplay
        Public Declare Function CLIENT_StopRealPlayEx Lib "dhnetsdk.dll" (ByVal lLoginID As Long) As Long
    
        Public Declare Sub CLIENT_Cleanup Lib "dhnetsdk.dll" ()
    
        'Alarm Liten API
        Public Declare Function CLIENT_StartListen Lib "dhnetsdk.dll" (ByVal lLoginID As Long) As Long
    
        'Mess callback API
        Public Declare Function CLIENT_SetDVRMessCallBack Lib "dhnetsdk.dll" (ByVal fMessCallBack As Long, _
        ByVal dwUser As Long) As Long
    
        Public Structure NET_DEVICEINFO
            Public sSerialNumber As String
            Public byAlarmInPortNum As Byte
            Public byAlarmOutPortNum As Byte
            Public byDiskNum As Byte
            Public byDVRType As Byte
            Public byChanNum As Byte
        End Structure
    
        Public Structure NET_RET_ERROR
            Public errcode As Integer
        End Structure
    
    
        Public Sub IniFunc(ByVal lLoginID As Long, ByVal pchDVRIP As Long, ByVal nDVRPort As Long, ByVal dwUser As Long)
            MessageBox.Show(lLoginID)
        End Sub
    
    
    
        Public Sub MessFunc(ByVal lCommand As Long, _
          ByVal lLoginID As Long, ByVal pBuf As Long, _
          ByVal dwBufLen As Long, ByVal pchDVRIP As Long, _
          ByVal nDVRPort As Long, ByVal dwUser As Long)
    
    
            MessageBox.Show(lLoginID)
            MessageBox.Show(pBuf)
            MessageBox.Show(pchDVRIP)
    
        End Sub
    
    
        Public Function PtrToLong(ByVal func As Long) As Long
            PtrToLong = func
        End Function
    
        Private bInited, bout As Boolean
        Private hLoginId, hMonitorId As Long
        Private err As NET_RET_ERROR
        Private devInfo As NET_DEVICEINFO
        Private dd, longinid As Long
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            bInited = CLIENT_Init(PtrToLong(AddressOf IniFunc), 0)
        End Sub
    End Class
    But I'm having a problem with this line> "bInited = CLIENT_Init (PtrToLong (AddressOf IniFunc), 0)".

    An error appears: "Address of expression can not be converted to 'Long' because 'Long' is not a delegate type".

    I did a search, I have not found a solution, or at least I did not understand the answer.

    I saw that maybe this is a difference between VB6 and VB.Net or something.

    Could someone help me?

  2. #2
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892

    Resolved Re: Problems with Callbacks

    You're likely going to have to use either Integer or IntPtr in the place of any Long data types.

    This can essentially be a guessing game without full documentation. Based on the names of the properties and how they're being used, I've come up with this:

    Code:
    Public Class Form1
        Public Declare Function CLIENT_Init Lib "dhnetsdk.dll" (ByVal fDisConnect As Integer, _
    ByVal wDVRPort As Integer) As Integer
    
    
    
        'Login API
        Public Declare Function CLIENT_Login Lib "dhnetsdk.dll" (ByVal pchDVRIP As String, ByVal wDVRPort As Integer, ByVal pchUserName As String, ByVal pchPassword As String, lpDeviceInfo As NET_DEVICEINFO, ByRef err As NET_RET_ERROR) As Integer
    
        'Logout device
        Public Declare Function CLIENT_Logout Lib "dhnetsdk.dll" (ByVal lLoginID As Integer) As Integer
    
        'Realplay API
        Public Declare Function CLIENT_RealPlay Lib "dhnetsdk.dll" _
        (ByVal lLoginID As Integer, ByVal nChannelID As Integer, ByVal hwnd As IntPtr) As Integer
    
        Public Declare Function CLIENT_RealPlayEx Lib "dhnetsdk.dll" _
        (ByVal lLoginID As Integer, ByVal nChannelID As Integer, ByVal hwnd As IntPtr, _
        ByVal rType As Integer) As Integer
    
        'Stop Realplay
        Public Declare Function CLIENT_StopRealPlayEx Lib "dhnetsdk.dll" (ByVal lLoginID As Integer) As Integer
    
        Public Declare Sub CLIENT_Cleanup Lib "dhnetsdk.dll" ()
    
        'Alarm Liten API
        Public Declare Function CLIENT_StartListen Lib "dhnetsdk.dll" (ByVal lLoginID As Integer) As Integer
    
        'Mess callback API
        Public Declare Function CLIENT_SetDVRMessCallBack Lib "dhnetsdk.dll" (ByVal fMessCallBack As IntPtr, _
        ByVal dwUser As Integer) As Integer
    
        Public Structure NET_DEVICEINFO
            Public sSerialNumber As String
            Public byAlarmInPortNum As Byte
            Public byAlarmOutPortNum As Byte
            Public byDiskNum As Byte
            Public byDVRType As Byte
            Public byChanNum As Byte
        End Structure
    
        Public Structure NET_RET_ERROR
            Public errcode As Integer
        End Structure
    
    
        Public Sub IniFunc(ByVal lLoginID As Integer, ByVal pchDVRIP As Integer, ByVal nDVRPort As Integer, ByVal dwUser As Integer)
            MessageBox.Show(lLoginID)
        End Sub
    
    
    
        Public Sub MessFunc(ByVal lCommand As Integer, _
          ByVal lLoginID As Integer, ByVal pBuf As IntPtr, _
          ByVal dwBufLen As Integer, ByVal pchDVRIP As Integer, _
          ByVal nDVRPort As Integer, ByVal dwUser As Integer)
    
    
            MessageBox.Show(lLoginID)
            MessageBox.Show(pBuf)
            MessageBox.Show(pchDVRIP)
    
        End Sub
    
    
        Public Function PtrToLong(ByVal func As IntPtr) As Integer
            PtrToLong = func
        End Function
    
        Private bInited, bout As Boolean
        Private hLoginId, hMonitorId As Integer
        Private err As NET_RET_ERROR
        Private devInfo As NET_DEVICEINFO
        Private dd, longinid As Integer
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            bInited = CLIENT_Init(PtrToLong(AddressOf IniFunc), 0)
        End Sub
    End Class
    Keep in mind that this is a total guess. If you have full API documentation or even if you have full working sourcecode from a VB6 app that referenced this API, we can give you a better answer.

    I simply changed the datatypes. Some of the references to these methods may now be broken, which you'll also have to fix.
    Last edited by Craig Gemmill; May 6th, 2014 at 10:49 AM.
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

  3. #3
    Join Date
    Apr 2012
    Posts
    43

    Re: Problems with Callbacks

    You're supposed to pass delegates I believe. Not Longs or IntPrts. Define a delegate that corresponds with the callback expected and declare the API's parameter to use this delegate as its type. When calling, you pass the address of a method with the same signature as the callback.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured