PDA

View Full Version : QLibrary resolving functions



td
7th April 2010, 11:06
Hi guys,

I have a widget that uses QLibrary to load a DLL and resolve its functions into widget specified functions. Here's what I mean


myWidget::myWidget(QWidget *parent)
: QWidget(parent)
{
myLib = new QLibrary; // library to hold DLL
}

void myWidget::openDLL(QString DLL_File_Name)
{
bool operation;

typedef bool (*MyPrototypeOne)();
MyPrototypeOne Open = (MyPrototypeOne) myLib->resolve("OD");

operation = Open();

emit Success(operation);

}

void myWidget::run(QString DLL_File_Name)
{
typedef int (*MyPrototypeTwo)();
MyPrototypeTwo RunFunc = (MyPrototypeTwo) myLib->resolve("RF");

int i = RunFunc();
}

What I want to know is, is it possible to have these resolved functions accessible throughout the widget? It would be nice not to have to continually pass the file name and prototype the function but rather resolve it once and have it accessible everywhere.

Any help appreciated.

borisbn
7th April 2010, 12:13
your example wouldn't work, because you didn't give DLL_File_Name to QLibrary object
call


myLib->setFileName( DLL_File_Name );

in your openDLL and run functions.
but there's a better idea to give dll name in constructor, like this


class myWidget : public QWidget
{
public:
myWidget(QWidget *parent, const char * const dllFileName )
: QWidget(parent)
, m_dll( dllFileName )
{
}
protected:
QLibrary m_dll;

and the next. Check, that your function pointers are not zero


MyPrototypeOne Open = (MyPrototypeOne) m_dll.resolve("OD");
if ( Open )
{
... // do something with Open
}

td
7th April 2010, 12:18
Thanks but maybe I wasn't being clear. I have a widget working perfectly (the above was just a basic demonstration of what I'm on about).

What I'm wondering is if there's a way to call the resolved function "Open" in the run function and vice versa.

borisbn
7th April 2010, 12:33
What I'm wondering is if there's a way to call the resolved function "Open" in the run function and vice versa
Of course you can. Just move declaration

MyPrototypeOne Open
into class declaraion (.h-file) as a member of your class, resolve them in constructor and use in any functions within your class

td
7th April 2010, 12:56
Ahh! Thanks! My brain must be on go-slow today.