Thought this feature was also worth flagging for those who don't follow tB GitHub or Discord.
twinBASIC now supports Call By Pointer, by way of Delegates, which are function pointer types.
You can of course also use a LongPtr variable to assign to the delegate type.Code:Private Delegate Function Delegate1 (ByVal A As Long, ByVal B As Long) As Long Private Sub Command1_Click() Dim myDelegate As Delegate1 = AddressOf Addition MsgBox "Answer: " & myDelegate(5, 6) End Sub Public Function Addition(ByVal A As Long, ByVal B As Long) As Long Return A + B End Function
The delegate type can also be used in interface/API declarations and as members of a User-defined type, for example, the ChooseColor API:
If you already have code assigning a Long/LongPtr to the lpfnHook member, it will continue to work normally, but now you can also have the type safety benefits of setting it to a method matching the Delegate:Code:Public Delegate Function CCHookProc (ByVal hwnd As LongPtr, ByVal uMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr Public Type CHOOSECOLOR lStructSize As Long hwndOwner As LongPtr hInstance As LongPtr rgbResult As Long lpCustColors As LongPtr Flags As ChooseColorFlags lCustData As LongPtr lpfnHook As CCHookProc 'Delegate function pointer type instead of LongPtr lpTemplateName As LongPtr End Type
A much, much better way of implementing call by pointer compared to the standard ways of DispCallFunc or asm thunks!Code:Dim tCC As CHOOSECOLOR tCC.lpfnHook = AddressOf ChooseColorHookProc ... Public Function ChooseColorHookProc(ByVal hwnd As LongPtr, ByVal uMsg As Long, ByVal wParam As LongPtr, ByVal lParam As LongPtr) As LongPtr End Function




Reply With Quote
