Results 1 to 3 of 3

Thread: Dynamically load DLL

  1. #1
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Dynamically load DLL

    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)

    Qt Code:
    1. short GPIB_Dashboard::Load_Drivers_Vds_1(QString *error_msg)
    2. {
    3. QString drivername;
    4. QString error;
    5. QStringList message;
    6. {
    7. if(My_Vds1_Lib != NULL)
    8. {
    9. My_Vds1_Lib->unload();
    10. delete My_Vds1_Lib;
    11. My_Vds1_Lib = new QLibrary();
    12. }
    13. else
    14. My_Vds1_Lib = new QLibrary();
    15.  
    16.  
    17. //reserve DLL copy
    18. drivername = QCoreApplication::applicationDirPath()+"//Driver_Vds1.dll";
    19. if(QFile::exists(drivername))
    20. if(!QFile::remove(drivername))
    21. {
    22. QTextStream(error_msg) << "error: could not image driver Vds1";
    23. return 1;
    24. }
    25. if(!QFile::copy(Vds1.name,drivername ))
    26. {
    27. QTextStream(error_msg) <<"error: could not copy driver Vds1";
    28. return 1;
    29. }
    30.  
    31. My_Vds1_Lib->setFileName(drivername);
    32. My_Vds1_Lib->load();
    33.  
    34.  
    35. DCsource_Vds1_class = (My_DCsource_Prototype) My_Vds1_Lib->resolve("CreateDCSourceClass");
    36. if(!DCsource_Vds1_class){
    37. // DCsource_1_online = 0;
    38. QTextStream(error_msg) <<"Vds1 driver: class unresolved";
    39. return 1;
    40. }
    41. DCsource_Vds1 = DCsource_Vds1_class();
    42. DCsource_Vds1->Instance_name = "Vds1";
    43. DCsource_Vds1->LoadConfig();
    44. DCsource_Vds1->Read_GPIB_config(&message, &error);
    45.  
    46. My_Dlg_Vds1_Class= NULL;
    47. Dlg_DCsource_Vds1 = NULL;
    48. My_Dlg_Vds1_Class = (Dlg_DCSource_Prototype) My_Vds1_Lib->resolve("CreateDCSourceDialogClass");
    49. if(!My_Dlg_Vds1_Class){
    50. // DCsource_1_online = 0;
    51. QTextStream(error_msg) <<"Vds1 driver: class unresolved";
    52. return 1;
    53. }
    54. // My_Vds1_Lib->unload();
    55.  
    56. Dlg_DCsource_Vds1 = My_Dlg_Vds1_Class(DCsource_Vds1);
    57.  
    58. QTextStream(error_msg) <<"Vds1 driver: successfully loaded";
    59. }
    60.  
    61. return 0;
    62. }
    To copy to clipboard, switch view to plain text mode 


    am l missing something ?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Dynamically load DLL

    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.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  3. #3
    Join Date
    Sep 2009
    Location
    phoenix, AZ
    Posts
    41
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically load DLL

    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

Similar Threads

  1. how to add tab dynamically
    By asish_cse in forum Newbie
    Replies: 2
    Last Post: 14th September 2010, 16:27
  2. problem about load resource file dynamically
    By Raul in forum Qt Programming
    Replies: 1
    Last Post: 27th August 2010, 05:56
  3. How to load library on linux dynamically?
    By zhehongwang in forum Qt Programming
    Replies: 3
    Last Post: 5th February 2009, 08:35
  4. Dynamically Add QCheckBox
    By ToddAtWSU in forum Qt Programming
    Replies: 0
    Last Post: 22nd January 2007, 19:00
  5. Dynamically resizing in QT 3
    By kroenecker in forum Qt Programming
    Replies: 3
    Last Post: 9th January 2006, 18:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.