0

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;
};
  • 4
    What is the signature of `libraryFunction` ? If it accepts lambda the answer will be trivial. Don't know if it is possible if it only accepts function pointers except by copying `globalObject` in a global variable. – Franck Oct 08 '16 at 13:00
  • Most C-style APIs also pass a context pointer. You should use that for context. – krzaq Oct 08 '16 at 13:06
  • @Franck I've added how the library looks like in the documentation. I don't think it does accept lambda. I am happy with a globalObject in a global variable as it is a relatively small application. @krzaq Unfortunately no other contextual arguments can be passed inside `libraryFunction` other then which callback function to use. –  Oct 08 '16 at 13:26
  • 1
    Can you post the definition of MathrelCallback? I guess it's just a plain function pointer, but who knows... – Horstling Oct 08 '16 at 13:44
  • @Horstling Updated the post to add `MathrelCallback` definition. –  Oct 08 '16 at 14:16
  • Ok thats definitely not a plain callback. Hard to say what you can do with that without documentation. – Horstling Oct 08 '16 at 14:23
  • I'll speak to the provider on Monday, your global variable/object solution worked great! It was a bit tricky as I had to use another third party's custom object. Thanks for your help! –  Oct 08 '16 at 14:54

1 Answers1

1

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);
}
Horstling
  • 2,131
  • 12
  • 14
  • Would this compile? I'm trying to make a small example work and I must be missing something. The `:` looks out of place? – wally Oct 08 '16 at 13:33
  • Try changing `int main(int argcm char* argv[])` to `int main(int argc, char* argv[])`. –  Oct 08 '16 at 13:38
  • Oh sry, just copied and edited the original code, didn't even notice the errors. – Horstling Oct 08 '16 at 13:44