c++ - 如何使回调全局化以便我可以将它用于其他函数?

标签 c++ dll stdcall

我声明了这样一个函数:

int __stdcall DoSomething(int &inputSize, int &outputSize, void(* __stdcall progress)(int) )
{
}

如何使 progress() 回调成为一个全局变量,以便在同一个 DLL 中的其他函数中使用它? 我是 C++ 的新手。

最佳答案

创建一个具有匹配签名的函数(即 void (*)(int))。

#include <iostream>

//void (      *      )(     int    ) - same signature as the function callback
  void progressHandler(int progress)
{
    std::cout << "received progress: " << progress << std::endl;
}

int DoSomething(int &inputSize, int &outputSize, void (*progress)(int))
{
    progress(100);
    return 0;
}

int main()
{
    int inputSize = 3;
    int outputSize = 3;
    DoSomething(inputSize, outputSize, progressHandler);

    return 0;
}

输出:

received progress: 100

即使我删除了它(因为我使用了 g++),您也可以保留 __stdcall

关于c++ - 如何使回调全局化以便我可以将它用于其他函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33046755/

相关文章:

.net - 进程被终止后,DLL 仍处于锁定状态

python - 如何使用 ctypes 将 python 列表传递给 C 函数 (dll)

c++ - 使用 PInvoke 时,为什么要使用 __stdcall?

assembly - 结构体如何在汇编中作为参数传递

c++ - 使用多重继承时的编译错误

C++继承虚函数

java - 无法在 AMD 64 位平台 LWJGL 上加载 IA 32 位 .dll

c# - 创建与 Windows 服务通信的非托管 DLL(在 C++ 中)?

c++ - 0xC0000005 : Access violation reading location 0x0000000000000008

c++ - 优化稀疏下三角线性系统的反向求解