【发布时间】:2017-12-24 05:58:02
【问题描述】:
第 3 方 C 库包含以下全局变量和函数:
typedef RtBasis float[4][4];
RI_EXPORT RtBasis RiCatmullRomBasis;
RI_EXPORT void RiBasis(RtBasis u);
我想从 DLL 中检索变量和函数并在我的 C++ 代码中使用它们。当我执行以下操作时:
// Get the function.
void (*RiBasis)(RtBasis);
RiBasis = (void (*)(RtBasis))GetProcAddress(h, "RiBasis");
// Get the variable.
RtBasis *RiCatmullRomBasis;
RiCatmullRomBasis = (RtBasis*)GetProcAddress(h, "RiCatmullRomBasis");
// Call the function, passing it the variable.
RiBasis(RiCatmullRomBasis);
Visual C++ 在调用 RiBasis 时出现此编译错误:
error C2664: 'void (float [][4])': cannot convert argument 1
from 'RtBasis (*)' to 'float [][4]'
我尝试从 RiCatmullRomBasis 变量中删除一级间接:
// Get the variable.
RtBasis RiCatmullRomBasis;
RiCatmullRomBasis = (RtBasis)GetProcAddress(h, "RiCatmullRomBasis");
// Call the function, passing it the variable.
RiBasis(RiCatmullRomBasis);
但这在 GetProcAddress 调用中给了我以下信息:
error C2440: 'type cast': cannot convert from 'FARPROC' to 'RtBasis'
note: There are no conversions to array types, although there are
conversions to references or pointers to arrays
在我的 C++ 代码中声明类型以使其工作的正确方法是什么?
【问题讨论】:
-
See this。为什么要将函数指针的地址转换为
float的二维数组? -
一旦你 LoadLibray,全局变量应该可以在你的代码中访问,但是你对变量执行 GetProcAddress,即使它不是一个函数。您是否尝试过在代码中声明
extern "C" RtBasis RiCatmullRomBasis;然后尝试访问它? -
根据文档,GetProcAddress() 也用于返回导出变量的地址。
标签: c++ visual-c++ dll