Wolfram Computation Meets Knowledge

Wolfram Language & System Documentation Center Wolfram Language Home Page »

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

Examples

open allclose all

Basic Examples  (1)

Load a function from a library:

Call the function:

Scope  (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:

Read the output:

Package into a function:

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:

Execute the hash:

Read the results from the ciphertext buffer:

Compare with the built-in function Hash:

Package into a function:

The packaged function is very efficient:

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:

Wolfram Research (2023), ForeignFunctionLoad, Wolfram Language function, https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html.

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

BibTeX

@misc{reference.wolfram_2024_foreignfunctionload, author="Wolfram Research", title="{ForeignFunctionLoad}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html}", note=[Accessed: 18-May-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_foreignfunctionload, organization={Wolfram Research}, title={ForeignFunctionLoad}, year={2023}, url={https://reference.wolfram.com/language/ref/ForeignFunctionLoad.html}, note=[Accessed: 18-May-2024 ]}

Top