PDA

View Full Version : Dynamically load DLL



OverTheOCean
24th October 2012, 12:41
Hi,

l want to load dynamically DLL ( instrument driver) so l can change on the fly which driver to use for my instruments.

The DLL contains a QDialog with a specific class containing all driver's commands. Overall works very well except when l change the DLL, can not open the Qdialog ( .exec() ), it crash.

the driver/ dll are tested individually and works fine.

I suspect something on class allocation below, but can not figure it out.


here is the code below:
- first copy the .dll to a specific name so can use same the driver multiple times in the application.
- then load teh class containing the GPIB commands
- then load the class for the Qdialog ( UI)



short GPIB_Dashboard::Load_Drivers_Vds_1(QString *error_msg)
{
QString drivername;
QString error;
QStringList message;
{
if(My_Vds1_Lib != NULL)
{
My_Vds1_Lib->unload();
delete My_Vds1_Lib;
My_Vds1_Lib = new QLibrary();
}
else
My_Vds1_Lib = new QLibrary();


//reserve DLL copy
drivername = QCoreApplication::applicationDirPath()+"//Driver_Vds1.dll";
if(QFile::exists(drivername))
if(!QFile::remove(drivername))
{
QTextStream(error_msg) << "error: could not image driver Vds1";
return 1;
}
if(!QFile::copy(Vds1.name,drivername ))
{
QTextStream(error_msg) <<"error: could not copy driver Vds1";
return 1;
}

My_Vds1_Lib->setFileName(drivername);
My_Vds1_Lib->load();


DCsource_Vds1_class = (My_DCsource_Prototype) My_Vds1_Lib->resolve("CreateDCSourceClass");
if(!DCsource_Vds1_class){
// DCsource_1_online = 0;
QTextStream(error_msg) <<"Vds1 driver: class unresolved";
return 1;
}
DCsource_Vds1 = DCsource_Vds1_class();
DCsource_Vds1->Instance_name = "Vds1";
DCsource_Vds1->LoadConfig();
DCsource_Vds1->Read_GPIB_config(&message, &error);

My_Dlg_Vds1_Class= NULL;
Dlg_DCsource_Vds1 = NULL;
My_Dlg_Vds1_Class = (Dlg_DCSource_Prototype) My_Vds1_Lib->resolve("CreateDCSourceDialogClass");
if(!My_Dlg_Vds1_Class){
// DCsource_1_online = 0;
QTextStream(error_msg) <<"Vds1 driver: class unresolved";
return 1;
}
// My_Vds1_Lib->unload();

Dlg_DCsource_Vds1 = My_Dlg_Vds1_Class(DCsource_Vds1);

QTextStream(error_msg) <<"Vds1 driver: successfully loaded";
}

return 0;
}



am l missing something ?

ChrisW67
25th October 2012, 00:02
I have no idea why you are trying to copy DLLs around at run time. This will fail on any modern Windows unless the application is permanently running with admin rights. Just load the specified DLL from is naturally location with QLibrary.

If your program is crashing check that pointers you think are valid are, in fact, valid at the time you use them. We cannot see your debugger so we cannot do this for you.

The whole exercise looks like a good fit for a plugin.

OverTheOCean
25th October 2012, 15:07
thanks for your feedback !!

l'm copying the DLL as it will be used for different instruments, therefore can not be accessed simultaneously for different instruments.

I'm not sure to agree with your comment, can copy and access dll at runtime...this is the whole idea of DLL, isn't it ?

could check Plugin if easier.

rgds,

Michael