Search code examples
c++dllstdcall

How to make a callback global so I can use it to other functions?


I declared a function like this:

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

How can I make progress() callback a global variable to use it in other functions in the same DLL? I am a newbie to C++.


Solution

  • Create a function with a matching signature (i.e., 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;
    }
    

    Output:

    received progress: 100
    

    Even though I removed it (because I used g++) you can keep the __stdcall.