dynamic - Get the address of a callback function to call dynamically in C++ -


i trying build application can dynamically call win32 api function according user input.

i wondering how can have behavior of function registercallback in c++, because seems useful , can address callback function.

how can achieve same behavior function in c++?

i implemented function can call dynamic library, stuck in such dynamic callbacks.

for example can call enumwindows api function below:

calllibfunction(getprocaddress(loadlibrary(l"user32.dll"), "enumwindows"), paramarray, 2, exepinfo); 

thanks in advance.

edit: explain more.

assume have following code:

callback function:

bool callback enumwindowsproc(__in hwnd hwnd, __in lparam lparam) {     return true; } 

main function:

enumwindows(enumwindowsproc, null); 

above usual way can use api function. want called this:

longlong callbackaddress = <some function address>&enumwindowsproc  paramarray[1].type = vt_i8; paramarray[1].llval = callbackaddress; // address of `enumwindowsproc`. 

and call dynamically like:

calllibfunction(getprocaddress(loadlibrary(l"user32.dll"), "enumwindows"), paramarray, 2, exepinfo); 

first, need declare pointer type hold address of callback. basic definition of function bit odd in c++. there further complication in c++ have functions, function-objects, , template types.

the standard provides basic function template type:std::function. type holds not function pointer, callable object.

#include <functional> 

to declare specific function type, pass signature std::function template parameter.

typedef std::function<int(const char*)> stdfreefunc;  // can define types member functions, , define  // member function pointers way struct a{}; typedef std::function<int a::*(const char*)> stdmemberfunc;  // member callable objects called syntax: // (obj.*callable)(args); or (obj->*callable)(args);  // parenthesis required keep compiler happy  // callable object: struct callme {     int xy;     int operator()(const char*) { /*...*/ return xy; }  }; 

std::function compatible function objects, lambdas , regular function pointers (see below). works best c++ stuff.

struct astruct {    stdfreefunc callback_;   // hold value, not pointer     void setcallback(stdfreefunc&& cb)  // pass value, reference, const ref,     {                                   // move... object type         callback_ = cb;     };     int callcallback(const char* str)     {         if (callback_) // callable object has bool() operator check if valid !!            return (callback_)(str);          // parenthesis , optional, same:        if (callback_)            return callback_(str):    } };  // example callable object: astruct as, ar; as.setcallback(callme{42});   // can pass object data ar.setcallback(callme{84});   // obtain different effects  as.callcallback("prints fourty two"); ar.callcallback("prints eighty four"); 

c-style function pointers

before c++, there c. how it's done in c, , compile. disadvantage c-style function pointers not compatible function objects. on other hand compatible c, , many other languages such pascal, vb, etc..

for example, type function taking const char* parameter , returning int written as:

typedef int (callback)(const char*); 

the usual form declare pointer, since that's stored. in:

typedef int (*callback)(const char*);  // can specify call specifications  typedef int (__stdcall * callback)(const char*);  // uses pascal calling   // exmample:  struct yourstruct {     //...    callback callback_{nullptr};  // pointer initialize nullptr    //...    void setcallback(callback cb)    {         // call ys.setcallback(afunction)        // or      ys.setcallback(&afunction)        callback_ = cb;     };     int callcallback(const char* str)     {         if (callback_)   // check if valid !!            return (*callback_)(str);          // parenthesis , * optional, same:        if (callback_)            return callback_(str):    } };  int userfunction(const char*) { /*...*/ return 0; }  yourstruct ys; ys.setcallback(&userfunction); ys.callcallback("hello"); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -