Reputation:
How to make globalObject
accessible by callbackFunction
?
I am using a third party library library.h
which has a method libraryFunction
. The output of libraryFunction
is a libraryFunctionOutput
which is passed into the callback function callbackFunction
.
How to pass another object (e.g. globalObject
) for use inside the callback function callbackFunction
?
Unfortunately the third party library is compiled so I cannot make any changes to it.
#include <stdio.h>
#include "library.h"
int callbackFunction(int libraryFunctionOutput):
printf("%s, %d", libraryFunctionOutput, globalObject);
return 1;
int main(int argc, char* argv[])
{
int globalObject = 0;
libraryFunction(callbackFunction);
}
In the documentation, the function is shown as:
int __stdcall libraryFunction(const char* filename,
unsigned int flags,
int userID,
MathrelCallback callbackFunction);
The MathrelCallback
struct is defined as the following:
struct MathrelCallback {MathrelHeader header;
MathrelInfo data;
unsigned int Type;
};
Upvotes: 0
Views: 77
Reputation: 2151
If the library really takes a function pointer (not a generalized Callable like std::function) and does not offer the ability to pass a context or user pointer to your callback, you have to make your global object global (with all its drawbacks):
#include <stdio.h>
#include "library.h"
static int globalObject = 0;
int callbackFunction(int libraryFunctionOutput)
{
printf("%s, %d", libraryFunctionOutput, globalObject);
return 1;
}
int main(int argc, char* argv[])
{
libraryFunction(callbackFunction);
}
Upvotes: 1