Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Callback function

Callback function

Scheduled Pinned Locked Moved Visual Basic
c++data-structureshelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tomaz Rotovnik
    wrote on last edited by
    #1

    Hello I have some problems with integrating dll (written in VC++) into VB application. I looked for some advices through google. I create three functions packed in dll: int_4 __stdcall Konec_RecToR(); //Stop int_4 __stdcall Start_RecToR(); //Start int_4 __stdcall RecToR_Init_Cap(void (*tmp)(unsigned char* array, int size, unsigned char first_frame, unsigned char last_frame, void* ex_param), void* ex_param); First two functions work well in VB application. The problem is the third function which includes two parameters: callback function with five parameters and extra parameter. The syntax in VB is: Private Declare Function Start_RecToR Lib "RecToR.dll" () As Long Private Declare Function Konec_RecToR Lib "RecToR.dll" () As Long Private Declare Function RecToR_Init_Cap Lib "RecToR.dll" (ByVal CalBckFunc As Long, ByRef param As Long) As Long Dim Success As Long Dim param As Long Private Sub Command1_Click() Text1.Text = "ASCII" & vbCrLf & "FG" param = 10 Success = RecToR_Init_Cap(AddressOf CallBackFunc, param) Success = Start_RecToR End Sub Private Sub Command2_Click() Success = Konec_RecToR End Sub //this is in separate module.bas file Public Sub CallBackFunc(ByRef inarray As Byte, ByVal size As Long, ByVal flag_start As Byte, ByVal flag_stop As Byte, ByRef param As Long) Text1.Text = Text1.Text & "TEST" End Sub Application crashes when my dll calls calback function. Did I miss something? Tomaz Rotovnik

    O 1 Reply Last reply
    0
    • T Tomaz Rotovnik

      Hello I have some problems with integrating dll (written in VC++) into VB application. I looked for some advices through google. I create three functions packed in dll: int_4 __stdcall Konec_RecToR(); //Stop int_4 __stdcall Start_RecToR(); //Start int_4 __stdcall RecToR_Init_Cap(void (*tmp)(unsigned char* array, int size, unsigned char first_frame, unsigned char last_frame, void* ex_param), void* ex_param); First two functions work well in VB application. The problem is the third function which includes two parameters: callback function with five parameters and extra parameter. The syntax in VB is: Private Declare Function Start_RecToR Lib "RecToR.dll" () As Long Private Declare Function Konec_RecToR Lib "RecToR.dll" () As Long Private Declare Function RecToR_Init_Cap Lib "RecToR.dll" (ByVal CalBckFunc As Long, ByRef param As Long) As Long Dim Success As Long Dim param As Long Private Sub Command1_Click() Text1.Text = "ASCII" & vbCrLf & "FG" param = 10 Success = RecToR_Init_Cap(AddressOf CallBackFunc, param) Success = Start_RecToR End Sub Private Sub Command2_Click() Success = Konec_RecToR End Sub //this is in separate module.bas file Public Sub CallBackFunc(ByRef inarray As Byte, ByVal size As Long, ByVal flag_start As Byte, ByVal flag_stop As Byte, ByRef param As Long) Text1.Text = Text1.Text & "TEST" End Sub Application crashes when my dll calls calback function. Did I miss something? Tomaz Rotovnik

      O Offline
      O Offline
      OICU812
      wrote on last edited by
      #2

      The problem is that you need to declare a "delegate" callback function. Here is some example code for enumerating thru windows using callbacks just like you need to implement in your code. You shouldn't have any problems figuring out what you need to do from here.

      Module EnumWindows
      Delegate Function EnumWindows_Callback(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer

      Public Declare Function EnumWindows Lib "user32" \_
          (ByVal lpEnumFunc As EnumWindows\_Callback, \_
              ByVal lParam As Integer) As Integer
      
      Public Declare Function EnumChildWindows Lib "user32" \_
          (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumWindows\_Callback, \_
              ByVal lParam As Integer) As Integer
      
      Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" \_
          (ByVal hWnd As Integer, ByVal lpClassName As System.Text.StringBuilder, \_
              ByVal nMaxCount As Integer) As Integer
      
      Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" \_
          (ByVal hWnd As Integer, ByVal lpString As System.Text.StringBuilder, \_
              ByVal cch As Integer) As Integer
      
      ' The callback routine, common to both EnumWindows and EnumChildWindows.
      ' the argument passed in lParam is the indent level.
      
      Function EnumWindows\_CBK(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer
          ' display information on this window, with correct indentation
          Console.WriteLine(New String(" "c, lParam \* 3) & WindowDescription(hWnd))
          ' then display all child windows, but indent them to the right
          EnumChildWindows(hWnd, AddressOf EnumWindows\_CBK, lParam + 1)
          ' Return 1 to continue enumeration.
          Return 1
      End Function
      
      ' return a windows description given its hWnd
      
      Function WindowDescription(ByVal hWnd As Integer) As String
          Dim text As String
          text = WindowText(hWnd)
      
          WindowDescription = "\[" & Right$("0000000" & Hex$(hWnd), 8) & "\] " \_
              & WindowClassName(hWnd)
          If Len(text) > 0 Then
              WindowDescription &= " - """ & text & """"
          End If
      End Function
      
      ' Return the caption/text of a window.
      
      Function WindowText(ByVal hWnd As Integer) As String
          Dim buffer As New System.Text.StringBuilder(256)
          Dim length As Integer
          length = GetWindowText(hWnd, buffer, buffer.Capacity)
          WindowText = buffer.ToString.Substring(0, length)
      End Function
      
      Function
      
      T 1 Reply Last reply
      0
      • O OICU812

        The problem is that you need to declare a "delegate" callback function. Here is some example code for enumerating thru windows using callbacks just like you need to implement in your code. You shouldn't have any problems figuring out what you need to do from here.

        Module EnumWindows
        Delegate Function EnumWindows_Callback(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer

        Public Declare Function EnumWindows Lib "user32" \_
            (ByVal lpEnumFunc As EnumWindows\_Callback, \_
                ByVal lParam As Integer) As Integer
        
        Public Declare Function EnumChildWindows Lib "user32" \_
            (ByVal hWndParent As Integer, ByVal lpEnumFunc As EnumWindows\_Callback, \_
                ByVal lParam As Integer) As Integer
        
        Public Declare Function GetClassName Lib "user32" Alias "GetClassNameA" \_
            (ByVal hWnd As Integer, ByVal lpClassName As System.Text.StringBuilder, \_
                ByVal nMaxCount As Integer) As Integer
        
        Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" \_
            (ByVal hWnd As Integer, ByVal lpString As System.Text.StringBuilder, \_
                ByVal cch As Integer) As Integer
        
        ' The callback routine, common to both EnumWindows and EnumChildWindows.
        ' the argument passed in lParam is the indent level.
        
        Function EnumWindows\_CBK(ByVal hWnd As Integer, ByVal lParam As Integer) As Integer
            ' display information on this window, with correct indentation
            Console.WriteLine(New String(" "c, lParam \* 3) & WindowDescription(hWnd))
            ' then display all child windows, but indent them to the right
            EnumChildWindows(hWnd, AddressOf EnumWindows\_CBK, lParam + 1)
            ' Return 1 to continue enumeration.
            Return 1
        End Function
        
        ' return a windows description given its hWnd
        
        Function WindowDescription(ByVal hWnd As Integer) As String
            Dim text As String
            text = WindowText(hWnd)
        
            WindowDescription = "\[" & Right$("0000000" & Hex$(hWnd), 8) & "\] " \_
                & WindowClassName(hWnd)
            If Len(text) > 0 Then
                WindowDescription &= " - """ & text & """"
            End If
        End Function
        
        ' Return the caption/text of a window.
        
        Function WindowText(ByVal hWnd As Integer) As String
            Dim buffer As New System.Text.StringBuilder(256)
            Dim length As Integer
            length = GetWindowText(hWnd, buffer, buffer.Capacity)
            WindowText = buffer.ToString.Substring(0, length)
        End Function
        
        Function
        
        T Offline
        T Offline
        Tomaz Rotovnik
        wrote on last edited by
        #3

        Thank you for your help. I will try with VB.NET framework (as I can see from example), because "Delegate" is not supported in VB 6. I will give feedback as soon as possible. Tomaz Tomaz Rotovnik

        O 1 Reply Last reply
        0
        • T Tomaz Rotovnik

          Thank you for your help. I will try with VB.NET framework (as I can see from example), because "Delegate" is not supported in VB 6. I will give feedback as soon as possible. Tomaz Tomaz Rotovnik

          O Offline
          O Offline
          OICU812
          wrote on last edited by
          #4

          Im so far removed from VB6 that I didn't even consider that you maybe using it.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups