Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

235
Views
Howto pass funcptr from C++ Class to a C API?

How can I pass a dynamic generated C++ function as *funcprt to a C API?

The API exports this:

DllImport void api_register_func(char *func_name, void (*funcptr)(char *, char *));

I have to create the function while runtime because I don't know about it before. So I used a class:

class JsFunc
{
    public:
        char * JsFuncName;
        char * JsParameter;

    void RunFunc(char * val1, char * val2)
    {
        printf("\nJsFunc.runFunc executed, JsParameter=%s passed\n",JsParameter);
    }
};

And call it like this:

JsFunc * jsm = new JsFunc ();
jsm->JsFuncName = external_parameter1; 
jsm->JsParameter = external_parameter2; 
api_register_func(external_parameter1, jsm->RunFunc);

But VisualStudio 2015 tells me:

Error C3867 'JsFunc::runFunc': non-standard syntax; use '&' to create a pointer to member VJCS C:\Users\astrauss\Source\Repos\VCJS\VCJS\VCJS.cpp 54

Sorry if the code is bad, I'm not a C/C++ programmer but need to get this running for my daily work. Thanks!

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Unfortunately, you don't have very many options here. The API:

DllImport void api_register_func(char *func_name, 
                                 void (*funcptr)(char *, char *));

only lets you provide a function pointer, without any context. So you can pass in a free function or a static member function, but you have no way of providing a member function. If you had a context:

DllImport void better_api_register_func(char *func_name, 
                                        void *context,
                                        void (*funcptr)(void *, char *, char *));

Then you could write a simple lambda:

api_register_func(external_parameter1,
                  jsm, //context
                  [](void *ctxt, char* arg1, char* arg2) {
                      static_cast<JsFunc*>(ctxt)->runFunc(arg1, arg2);
                  });

and you're effectively using a member function as the callback. However, this isn't an option. Instead, you're stuck with using a global variable:

std::unique_ptr<JsFunc> the_global_func;

Which you can allocate at runtime:

the_global_func.reset(new JsFunc);
// set stuff

And then use the global in a lambda. Since it's global, we don't need to (in fact, can't) capture it - which means our lambda can be convertible to a function pointer:

api_register_func(external_paramter1,
                  [](char* arg1, char* arg2) {
                      the_global_func->runFunc(arg1, arg2);
                  });
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!