ForeignPointerLookup[lib,fun]
返回库 lib 中函数 fun 的指针.
ForeignPointerLookup
ForeignPointerLookup[lib,fun]
返回库 lib 中函数 fun 的指针.
更多信息
- ForeignPointerLookup 返回一个 OpaqueRawPointer.
- 用 FindLibrary 解析 lib.
- lib 必须是与 C 兼容的动态库.
- ForeignPointerLookup 与 Unix 类系统上的 dlsym 和 Windows 上的 GetProcAddress 相似.
范例
打开所有单元 关闭所有单元基本范例 (1)
addonePtr = ForeignPointerLookup["compilerDemoBase", "addone"]指定适当的类型,加载 ForeignFunction:
addone = ForeignFunctionLoad[addonePtr, {"CInt"} -> "CInt"]addone[41]范围 (1)
ForeignPointerLookup 使用 FindLibrary 查找库:
ForeignPointerLookup["compilerDemoBase", "addone"]或者用 FindLibrary 找到库然后传递给 ForeignPointerLookup:
lib = FindLibrary["compilerDemoBase"];
ForeignPointerLookup[lib, "addone"]应用 (1)
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"]]addone = ForeignPointerLookup["compilerDemoBase", "addone"]ptr = arrayMaker[addone]RawMemoryImport[ptr, {"List", 10}]属性和关系 (1)
addonePtr = ForeignPointerLookup["compilerDemoBase", "addone"]指定适当的类型,加载 ForeignFunction:
ForeignFunctionLoad[addonePtr, {"CInt"} -> "CInt"]也可以通过 ForeignFunctionLoad 调用完成:
ForeignFunctionLoad["compilerDemoBase", "addone", {"CInt"} -> "CInt"]可能存在的问题 (2)
如果找不到库,ForeignPointerLookup 将返回 Failure:
ForeignPointerLookup["compilerDemoBase2", "addone"]如果在库中找不到函数指针,ForeignPointerLookup 将返回 Failure:
ForeignPointerLookup["compilerDemoBase", "addtwo"]技术笔记
-
▪
- 外部函数
相关指南
-
▪
- 外部函数接口
文本
Wolfram Research (2023),ForeignPointerLookup,Wolfram 语言函数,https://reference.wolfram.com/language/ref/ForeignPointerLookup.html.
CMS
Wolfram 语言. 2023. "ForeignPointerLookup." Wolfram 语言与系统参考资料中心. Wolfram Research. https://reference.wolfram.com/language/ref/ForeignPointerLookup.html.
APA
Wolfram 语言. (2023). ForeignPointerLookup. Wolfram 语言与系统参考资料中心. 追溯自 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: 05-October-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: 05-October-2026]}