ForeignPointerLookup[lib,fun]
returns the pointer to the function fun in the library lib.
ForeignPointerLookup
ForeignPointerLookup[lib,fun]
returns the pointer to the function fun in the library lib.
Details
- ForeignPointerLookup returns an OpaqueRawPointer.
- lib is resolved with FindLibrary.
- lib must be a C-compatible dynamic library.
- ForeignPointerLookup is analogous to dlsym on Unix-type systems, and GetProcAddress on Windows.
Examples
open all close allBasic Examples (1)
Get the pointer to a function in a library:
addonePtr = ForeignPointerLookup["compilerDemoBase", "addone"]Load the ForeignFunction by specifying an appropriate type:
addone = ForeignFunctionLoad[addonePtr, {"CInt"} -> "CInt"]addone[41]Scope (1)
ForeignPointerLookup uses FindLibrary to locate libraries:
ForeignPointerLookup["compilerDemoBase", "addone"]Alternatively, find the library once with FindLibrary and pass it to ForeignPointerLookup:
lib = FindLibrary["compilerDemoBase"];
ForeignPointerLookup[lib, "addone"]Applications (1)
Compile a library with a function that takes a function pointer:
Needs["CCompilerDriver`"]
lib = CreateLibrary["
#include <stdlib.h>
#include \"dllexport.h\"
DLLEXPORT int* arrayMaker(int (*fun)(int)) {
int* out = (int*)malloc(sizeof(int) * 10);
for(int i = 0; i < 10; i++) {
out[i] = fun(i);
}
return out;
}
", "foreignPointerLookupTest"];arrayMaker = ForeignFunctionLoad[lib, "arrayMaker", {"OpaqueRawPointer"} -> "RawPointer"::["CInt"]]Load a pointer to a function from a different library:
addone = ForeignPointerLookup["compilerDemoBase", "addone"]Pass the function pointer to the foreign function:
ptr = arrayMaker[addone]RawMemoryImport[ptr, {"List", 10}]Properties & Relations (1)
Get the pointer to a function in a library:
addonePtr = ForeignPointerLookup["compilerDemoBase", "addone"]Load the ForeignFunction by specifying an appropriate type:
ForeignFunctionLoad[addonePtr, {"CInt"} -> "CInt"]Alternatively, this can be accomplished with a single ForeignFunctionLoad call:
ForeignFunctionLoad["compilerDemoBase", "addone", {"CInt"} -> "CInt"]Possible Issues (2)
ForeignPointerLookup will return a Failure if the library cannot be found:
ForeignPointerLookup["compilerDemoBase2", "addone"]ForeignPointerLookup will return a Failure if the function pointer cannot be found in the library:
ForeignPointerLookup["compilerDemoBase", "addtwo"]Tech Notes
Related Guides
History
Text
Wolfram Research (2023), ForeignPointerLookup, Wolfram Language function, https://reference.wolfram.com/language/ref/ForeignPointerLookup.html.
CMS
Wolfram Language. 2023. "ForeignPointerLookup." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ForeignPointerLookup.html.
APA
Wolfram Language. (2023). ForeignPointerLookup. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ForeignPointerLookup.html
BibTeX
@misc{reference.wolfram_2026_foreignpointerlookup, author="Wolfram Research", title="{ForeignPointerLookup}", year="2023", howpublished="\url{https://reference.wolfram.com/language/ref/ForeignPointerLookup.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_foreignpointerlookup, organization={Wolfram Research}, title={ForeignPointerLookup}, year={2023}, url={https://reference.wolfram.com/language/ref/ForeignPointerLookup.html}, note=[Accessed: 13-June-2026]}