【问题标题】:How to retrieve array variable from DLL? (Visual C++)如何从 DLL 中检索数组变量? (视觉 C++)
【发布时间】: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


【解决方案1】:

在第一个版本中,调用如下:

RiBasis(*RiCatmullRomBasis);

您需要获取变量的地址(这是 GetProcAddress 可以返回的),但该函数需要一个实例而不是指针,因此您必须取消引用返回的指针。

【讨论】:

    猜你喜欢
    • 2021-12-27
    • 1970-01-01
    • 2021-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 1970-01-01
    相关资源
    最近更新 更多