Function Pointer From String (static class member)



  • Hi,

    What I am trying to do, is create a main() function with custom input arguments in a dll, and wrap it in a thread using QtConcurrent.

    I could do this easily if I just made a class, but I want to dynamically call the main function without having to create a class object to pass into QtConcurrent...

    So for example, I want a function in the hello.dll called main_ride_bike(BikerInfo *info), where the BikerInfo class contains a couple attributes about the biker, like how far he wants to go (integer), the bikers name (QString), etc.
    @
    // create function name
    QString processName = "main_ride_bike"; // function in dll

    // create biker info
    BikerInfo *info = new BikerInfo(this);
    info->bikerName = "mike";
    info->distanceToGo = 100;

    // start thread using 'main' function in dll
    QFuture <void> rideBike = QtConcurrent::run(processName, info);
    @

    How could I construct the Qt Dll to do so?



  • Hi,

    in order to create correctly a shared library read "here":http://qt-project.org/doc/qt-5.1/qtdoc/sharedlibrary.html



  • I figured out most of what I was trying to do with the dll.

    My only issue now is trying to convert a string to a function pointer. Unfortunately, I don't believe I will be able to use a 'reflection' like technique in c++.

    Thank you again.



  • The question is: "Why do you need use a string"?
    If you really needs it, Read about QLibrary class in order to load the library at runtime.



  • Hi,
    Converting a string to a function pointer is not possible directly, what is possible is to use "QMetaObject::invokeMethod()":http://qt-project.org/doc/qt-5.1/qtcore/qmetaobject.html#invokeMethod to call a method using its name in a string. But unfortunately this does not return a pointer to the function, so I'm not sure that this will match with you requirements.

    Maybe this post could help : "Call a function or methods dynamically":http://qt-project.org/forums/viewthread/17773

    edit: There is certainly a way to run QMetaObject::invokeMethod in a different thread.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.