ForeignFunctionLoad
ForeignFunctionLoad[lib,fun,{argtype1,argtype2,…}rettype]
loads the function fun with the specified argument and output types from the library lib.
ForeignFunctionLoad[ptr,{argtype1,argtype2,…}rettype]
creates a foreign function from the function pointer ptr.
Details
- ForeignFunctionLoad returns the ForeignFunction object.
- lib is resolved with FindLibrary.
- lib must be a C-compatible dynamic library.
- Supported types generally align with those supported by the Wolfram Compiler.
- Possible argument and return types and their corresponding C types include:
-
"UnsignedInteger8" uint8_t unsigned 8-bit integer "Integer8" int8_t signed 8-bit integer "UnsignedInteger16" uint16_t unsigned 16-bit integer "Integer16" int16_t signed 16-bit integer "UnsignedInteger32" uint32_t unsigned 32-bit integer "Integer32" int32_t signed 32-bit integer "UnsignedInteger64" uint64_t unsigned 64-bit integer "Integer64" int64_t signed 64-bit integer "CUnsignedChar" unsigned char C-compatible unsigned char "CSignedChar" signed char C-compatible signed char "CUnsignedShort" unsigned short C-compatible unsigned short "CShort" short C-compatible short "CUnsignedInt" unsigned int C-compatible unsigned int "CInt" int C-compatible int "CUnsignedLong" unsigned long C-compatible unsigned long "CLong" long C-compatible long "CSizeT" size_t C-compatible size_t "CFloat" float C-compatible float "CDouble" double C-compatible double "OpaqueRawPointer" void* opaque pointer "RawPointer"::[t] t* typed pointer {ty1,ty2,…} struct {ty1 f1; ty2 f2; …} struct or product type "Void" void no output (only in output types) - Struct or product types can also be written as "ListTuple"::[ty1,ty2, …].
- In ForeignFunctionLoad[ptr,…], ptr must be an OpaqueRawPointer pointing to a valid function in a library.
Examples
open allclose allScope (2)
ForeignFunctionLoad uses FindLibrary to locate libraries:
Alternatively, find the library once with FindLibrary and pass it to ForeignFunctionLoad:
Get the pointer to a function in a library:
Load the foreign function by using the function pointer and specifying its type:
Applications (2)
Create a foreign function for the RAND_bytes function from OpenSSL:
Create a buffer into which the random bytes can be written:
Generate the random bytes by calling RAND_bytes:
The foreign function is very efficient:
Implement SHA256 using OpenSSL:
Create a buffer containing the plaintext to hash:
Create a buffer into which to write the hash:
Read the results from the ciphertext buffer:
Compare with the built-in function Hash:
Properties & Relations (1)
ForeignFunctionLoad can generally create callable links to libraries faster than custom links can be compiled:
However, the custom compiled versions can have less overhead:
Possible Issues (3)
If the library does not exist, a Failure is returned:
If the function does not exist, a Failure is returned:
ForeignFunctionLoad will return $Failed if a type is not supported:
Text
Wolfram Research (2023), ForeignFunctionLoad, Wolfram Language function, https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html.
CMS
Wolfram Language. 2023. "ForeignFunctionLoad." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html.
APA
Wolfram Language. (2023). ForeignFunctionLoad. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html