Platforms to show: All Mac Windows Linux Cross-Platform

Back to DeclareFunctionMBS class.

DeclareFunctionMBS.ClearParameters

Type Topic Plugin Version macOS Windows Linux iOS Targets
method DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Clears all parameters.

Sets them all to zero/nil.

DeclareFunctionMBS.Constructor(Signature as String, FunctionPtr as Ptr)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The constructor.
Example
// change path if you like to try this on Windows or Linux
Dim d As New DeclareLibraryMBS("/usr/lib/libz.1.dylib")

// 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

Pass the signature and function pointer from a C function.
If signature is incorrect, the application will probably crash with stack corruption.

DeclareFunctionMBS.Invoke as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Invokes the function.
Example
// change path if you like to try this on Windows or Linux
Dim d As New DeclareLibraryMBS("/usr/lib/libz.1.dylib")

// 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

See also:

DeclareFunctionMBS.Invoke(Parameters() as Variant) as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
method DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Invokes the function.

First calls SetParameters with the given array to set parameters.
Then invokes the function.

See also:

DeclareFunctionMBS.ParameterBoolean(Index as Integer) as Boolean

Type Topic Plugin Version macOS Windows Linux iOS Targets
property DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The parameter as boolean.

You can get and set value here.
(Read and Write computed property)

DeclareFunctionMBS.ParameterDouble(Index as Integer) as Double

Type Topic Plugin Version macOS Windows Linux iOS Targets
property DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The parameter value as double.

You can get and set value here.
(Read and Write computed property)

DeclareFunctionMBS.ParameterInteger(Index as Integer) as Int64

Type Topic Plugin Version macOS Windows Linux iOS Targets
property DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The parameter as integer value.

You can get and set value here.
(Read and Write computed property)

Some examples using this property:

DeclareFunctionMBS.ParameterPointer(Index as Integer) as Ptr

Type Topic Plugin Version macOS Windows Linux iOS Targets
property DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The parameter as pointer value.

You can get and set value here.
(Read and Write computed property)

DeclareFunctionMBS.ParameterSingle(Index as Integer) as Single

Type Topic Plugin Version macOS Windows Linux iOS Targets
property DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The parameter as single value.

You can get and set value here.
(Read and Write computed property)

DeclareFunctionMBS.ParameterString(Index as Integer) as String

Type Topic Plugin Version macOS Windows Linux iOS Targets
property DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
The parameter as string value.
Example
// We try to run strstr on C Library on macOS to find string in string with strstr as example:

Dim d As New DeclareLibraryMBS("/usr/lib/libc.dylib")

// char *strstr(Const char *__big, Const char *__little);

Dim p As ptr = d.Symbol("strstr")
Dim f As New DeclareFunctionMBS("(ZZ)Z", p)

f.ParameterString(0) = "Hello World!"
f.ParameterString(1) = "World"

Dim n1 As String = f.Invoke
// gives back World! as that was found

f.ParameterString(0) = "Hello World!"
f.ParameterString(1) = "xxx"

Dim n2 As String = f.Invoke
// gives empty string as not found

Break // see in debugger

To avoid crashes, we only read strings from parameters with type string or pointer.
But you can assign a string value to all types, which may not make sense for numbers.
You can get and set value here.
(Read and Write computed property)

DeclareFunctionMBS.ParameterValue(Index as Integer) as Variant

Type Topic Plugin Version macOS Windows Linux iOS Targets
property DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Get or set the value as variant.

We automatically convert values based on the parameter type.
The class stores reference to the variant when setting to make sure strings and MemoryBlocks are not freed too early.
You can get and set value here.

Please pass C Strings as Xojo strings, not CString data type.
(Read and Write computed property)

DeclareFunctionMBS.SetParameters(paramArray Parameters as Variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Set all parameters together with passing arguments to this function.
Example
// We try to run sysctlbyname on macOS

Dim d As New DeclareLibraryMBS("/usr/lib/libc.dylib")

// int sysctlbyname(const char *name, void *oldp, size_t *oldlenp, void *newp, size_t newlen);

Dim p As ptr = d.Symbol("sysctlbyname")

'const char* -> CString for name -> Z
'void* -> pointer to data -> p
'size_t * -> pointer to 32/64 bit integer for length -> p
'void* -> pointer to data -> p
'size_t -> 32/64bit unsigned int -> L in 64-bit

Dim f As New DeclareFunctionMBS("(ZpppL)i", p)

Dim mData As New MemoryBlock(8)
Dim mLen As New MemoryBlock(8)
Dim pData As ptr = mData
dim pLen as ptr = mLen

mLen.UInt64Value(0) = mData.Size

f.SetParameters "hw.ncpu", pData, pLen, Nil, 0
Dim n As Integer = f.Invoke
// result is zero on result or -1 on failure

Dim CPUCount As Integer = mData.UInt32Value(0)

If n = 0 Then
MsgBox "CPU count: "+Str(CPUCount)
Else
MsgBox "Failed."
End If

Break // see in debugger

We automatically convert values based on the parameter type.
The class stores reference to the variant when setting to make sure strings and MemoryBlocks are not freed too early.

Please pass C Strings as Xojo strings, not CString data type.

See also:

DeclareFunctionMBS.SetParameters(Parameters() as Variant)

Type Topic Plugin Version macOS Windows Linux iOS Targets
method DynamicDeclares MBS Util Plugin 20.3 ✅ Yes ✅ Yes ✅ Yes ✅ Yes All
Sets all arguments by passing an array of values.

We automatically convert values based on the parameter type.
The class stores reference to the variant when setting to make sure strings and MemoryBlocks are not freed too early.

Please pass C Strings as Xojo strings, not CString data type.

See also:

The items on this page are in the following plugins: MBS Util Plugin.


The biggest plugin in space...