Callback function
-
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
-
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
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 IntegerPublic 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
-
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 IntegerPublic 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
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
-
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