Platforms to show: All Mac Windows Linux Cross-Platform
Required plugins for this example: MBS Util Plugin, MBS Compression Plugin
You find this example project in your Plugins Download as a Xojo project file within the examples folder: /Util/Dynamic Declare
This example is the version from Fri, 7th May 2020.
Project "Dynamic Declare.xojo_binary_project"
Class App Inherits Application
Const kEditClear = "&Delete"
Const kFileQuit = "&Quit"
Const kFileQuitShortcut = ""
EventHandler Sub Open()
#if TargetMacOS then
// change path if you like to try this on Windows or Linux
Dim d As New DeclareLibraryMBS("/usr/lib/libz.1.dylib")
Dim lines() As String = d.SymbolNames
Break // look in list of functions
// zlibVersion
// ZEXTERN Const char * ZEXPORT zlibVersion Of((void));
Dim p As ptr = d.Symbol("zlibVersion")
Dim f As New DeclareFunctionMBS("()Z", p)
Dim n As String = f.Invoke
MsgBox "zlibVersion: "+n
// CRC
// ZEXTERN uLong ZEXPORT crc32 Of((uLong crc, Const Bytef *buf, uInt Len));
// uLong is 32/64-bit on Mac, but may be always 32-bit on Windows, so we use long as type here
Dim s As String = "Hello World"
Dim CRCStartValue As Integer = 0
p = d.Symbol("crc32")
f = New DeclareFunctionMBS("(LpI)L", p)
f.ParameterInteger(0) = CRCStartValue
f.ParameterString(1) = s
f.ParameterInteger(2) = s.LenB
Dim crc As UInt32 = f.Invoke
Dim crcPlugin As UInt32 = CRC32StringMBS(CRCStartValue, s)
MsgBox "Declare: "+str(crc)+EndOfLine+"Plugin: "+str(crcPlugin)
#EndIf
End EventHandler
End Class
Class MainWindow Inherits Window
Control List Inherits Listbox
ControlInstance List Inherits Listbox
End Control
EventHandler Sub Open()
// one int32 parameter and return int32
Dim v1() As Variant
v1.Append 345
test "i)i", 123, v1
// various parameters
Dim m2 As New MemoryBlock(4)
Dim p2 As ptr = m2
Dim v2() As Variant
dim s2() as string
v2.Append 1
s2.Append MyCallback.kTypeBool
v2.Append -123
s2.Append MyCallback.kTypeChar
v2.Append 123
s2.Append MyCallback.kTypeUnsignedChar
v2.Append -1234
s2.Append MyCallback.kTypeShort
v2.Append 1234
s2.Append MyCallback.kTypeUnsignedShort
v2.Append -12345678
s2.Append MyCallback.kTypeInt
v2.Append 12345678
s2.Append MyCallback.kTypeUnsignedInt
v2.Append -12345678
s2.Append MyCallback.kTypeLong
v2.Append 12345678
s2.Append MyCallback.kTypeUnsignedLong
v2.Append -1234567890123
s2.Append MyCallback.kTypeInt64
v2.Append 1234567890123
s2.Append MyCallback.kTypeUnsignedInt64
v2.Append CType(12.34, Single) // avoid passing as double
s2.Append MyCallback.kTypeFloat
v2.Append 123.456
s2.Append MyCallback.kTypeDouble
v2.Append p2
s2.Append MyCallback.kTypePtr
v2.Append "Hello World"
s2.Append MyCallback.kTypeString
// return
s2.Append MyCallback.kTypeEndArg
s2.Append MyCallback.kTypeVoid
Dim sig2 As String = Join(s2,"")
test sig2, Nil, v2
Dim v3() As Variant
v3.Append 345
test "i)Z", "Hello World", v3
// test pointer return
Dim v4() As Variant
v4.Append 345
test "i)p", p2, v4
// test double
Dim v5() As Variant
test ")d", 2.5, v5
Log "done"
Break // no crash till here? Great!
End EventHandler
Sub Log(s as string)
List.AddRow s
End Sub
Sub test(signature as string, Result as Variant, TestParameters() as Variant)
Dim c As New MyCallback(signature)
Dim f As New MyFunction(signature, c.FunctionPtr)
c.Result = Result
Dim r As Variant = f.Invoke(TestParameters)
If r <> c.Result Then
Log "Result is not matching for " + signature
Break // failed
Else
// okay
End If
Dim ParametersReceived() As Variant = c.ParametersReceived
Dim u As Integer = TestParameters.Ubound
For i As Integer = 0 To u
Dim ParameterReceived As Variant = ParametersReceived(i)
Dim TestParameter As Variant = TestParameters(i)
If ParameterReceived <> TestParameter Then
Log Str(i)+"th parameter not matching for " + signature
Break // failed
End If
Next
End Sub
End Class
MenuBar MainMenuBar
MenuItem FileMenu = "&File"
MenuItem FileQuit = "#App.kFileQuit"
MenuItem EditMenu = "&Edit"
MenuItem EditUndo = "&Undo"
MenuItem EditSeparator1 = "-"
MenuItem EditCut = "Cu&t"
MenuItem EditCopy = "&Copy"
MenuItem EditPaste = "&Paste"
MenuItem EditClear = "#App.kEditClear"
MenuItem EditSeparator2 = "-"
MenuItem EditSelectAll = "Select &All"
End MenuBar
Class MyCallback Inherits DeclareCallBackMBS
EventHandler Function Callback(Parameters() as Variant) As Variant
Self.ParametersReceived = Parameters
Return result
End EventHandler
Property ParametersReceived() As Variant
Property Result As Variant
End Class
Class MyFunction Inherits declareFunctionMBS
End Class
End Project
The items on this page are in the following plugins: MBS Util Plugin.