如何从调用程序访问 DLL 的变量?

How to access a DLL's variable from the calling program?

本文关键字:DLL 变量 访问 程序 调用      更新时间:2023-10-16

我有一个.dll,我正在尝试使用Unity3D。我需要的是两者之间的实时通信,因此在 DLL 中我有一个循环,它执行一些计算,然后使用参数调用以下函数 myvalue, 0

__declspec(dllexport) double __cdecl MyMathFuncs::Divide(double value, double b)
{
    static int numfound = 0;            
    if(value == -1 && b == -1) //for args -1, -1, this is a...
        return numfound;   // ...GETTER
    else numfound = value ;  // if args are other then -1,-1, this is a SETTER
    return -1;
}

问题是,每次从 Unity 的 Update(( 函数中调用此函数作为 getter 时,它都会返回零,无论我在另一个函数的循环中将其设置为什么。我在 Visual Studio 中对其进行了测试,并在那里获得了预期的输出 - 所以我想变量的范围或生存期存在问题。我还尝试声明一个全局变量并访问它,但它的值也是零。我该如何解决这个问题?

您的方法确实返回

  • 0如果valueb相等-1
  • -1否则。

你确定最后一个语句不应该return numfound(或return value(吗?

设置 numfound = value 对输出没有任何影响,因为您使用每个方法调用重新初始化numfound。如果你想在方法之外使用它,你是对的,你需要把它作为一个全局变量。