Components | All | New | MacOS | Windows | Linux | iOS | ||||
Examples | Mac & Win | Server | Client | Guides | Statistic | FMM | Blog | Deprecated | Old |
Calls a C function.
Component | Version | macOS | Windows | Linux | Server | iOS SDK |
CFunction | 10.3 | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes | ✅ Yes |
Parameter | Description | Example |
---|---|---|
FunctionRef | The reference number for the function. | $func |
Parameters... | The parameters. Pass one for each value, see examples. |
Returns OK or error.
Call a function without parameters:
MBS( "CFunction.Call"; $function)
Load SQLite version function and run it:
Set Variable [ $library ; Value: MBS( "CLibrary.Load"; "/usr/lib/libsqlite3.dylib" ) ]
If [ MBS("IsError") ]
Show Custom Dialog [ "Failed to load library." ; $library ]
Else
Set Variable [ $function ; Value: MBS( "CLibrary.LoadFunction"; $library; "sqlite3_libversion"; "()Z" ) ]
If [ MBS("IsError") ]
Show Custom Dialog [ "Failed to load function." ; $function ]
Else
Set Variable [ $version ; Value: MBS( "CFunction.Call"; $function) ]
Show Custom Dialog [ "SQLite version:" ; $version ]
Set Variable [ $r ; Value: MBS( "CFunction.Release"; $function) ]
End If
Set Variable [ $r ; Value: MBS( "CLibrary.Release"; $library) ]
End If
Query user name on Windows:
# this is C declaration:
# BOOL GetUserNameW(LPWSTR lpBuffer, LPDWORD pcbBuffer);
#
# we load the library and the function
Set Variable [ $library ; Value: MBS( "CLibrary.Load"; "Advapi32.dll") ]
If [ MBS("IsError") ]
Show Custom Dialog [ "Failed to load library." ; $library ]
Else
Set Variable [ $function ; Value: MBS( "CLibrary.LoadFunction"; $library; "GetUserNameW"; "(pp)i") ]
If [ MBS("IsError") ]
Show Custom Dialog [ "Failed to load library." ; $function ]
Else
# since 1st parameter is WChar Array, we allocate space for it
Set Variable [ $r ; Value: MBS( "CFunction.AllocateArray"; $function; 0; "S"; 256 ) ]
# and for the count variable as ptr, we allocate a one value array
Set Variable [ $r ; Value: MBS( "CFunction.AllocateArray"; $function; 1; "I"; 1) ]
# put in 255 for the length of the first array (minus one character for end character)
Set Variable [ $r ; Value: MBS( "CFunction.SetArray"; $function; 1; "i"; 255) ]
# Now call function
Set Variable [ $result ; Value: MBS( "CFunction.Call"; $function) ]
If [ $result = 1 ]
# now we read how many characters we got
Set Variable [ $r ; Value: MBS( "CFunction.GetArray"; $function; 1; "I") ]
Set Variable [ $length ; Value: GetValue($r; 1) ]
# and get the characters
Set Variable [ $characters ; Value: MBS( "CFunction.GetArray"; $function; 0; "S") ]
#
# we loop over the values to build the text
Set Variable [ $text ; Value: "" ]
Set Variable [ $index ; Value: 1 ]
Loop [ Flush: Always ]
Set Variable [ $char ; Value: GetValue($characters; $index) ]
Set Variable [ $text ; Value: $text & Char($char) ]
#
Set Variable [ $index ; Value: $index + 1 ]
Exit Loop If [ $index = $length ]
End Loop
#
Show Custom Dialog [ "Windows user name" ; $text ]
#
# cleanup
Set Variable [ $r ; Value: MBS( "CFunction.FreeArray"; $function; 0) ]
Set Variable [ $r ; Value: MBS( "CFunction.FreeArray"; $function; 1) ]
End If
Set Variable [ $r ; Value: MBS( "CFunction.Release"; $function) ]
End If
Set Variable [ $r ; Value: MBS( "CLibrary.Release"; $library) ]
End If
This function checks for a license.
Created 25th April 2020, last changed 4th January 2024