Results 1 to 4 of 4

Thread: Loading Qt DLL from MFC

  1. #1
    Join Date
    Aug 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Loading Qt DLL from MFC

    I load my qt-dll by button clicking:
    Qt Code:
    1. void CMFCTestDlg::OnBnClickedButton1()
    2. {
    3. HINSTANCE hinstLib;
    4. MYPROC ProcAdd;
    5. BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
    6.  
    7. // Get a handle to the DLL module.
    8.  
    9. hinstLib = LoadLibrary(TEXT("qttest.dll"));
    10.  
    11. // If the handle is valid, try to get the function address.
    12.  
    13. if (hinstLib != NULL)
    14. {
    15. ProcAdd = (MYPROC) GetProcAddress(hinstLib, "createWindow");
    16.  
    17. // If the function address is valid, call the function.
    18.  
    19. if (NULL != ProcAdd)
    20. {
    21. fRunTimeLinkSuccess = TRUE;
    22. char * params[] = {"1"};
    23. (ProcAdd) (1, params);
    24. }
    25.  
    26. // Free the DLL module.
    27.  
    28. fFreeResult = FreeLibrary(hinstLib);
    29. }
    30.  
    31. // If unable to call the DLL function, use an alternative.
    32.  
    33. if (! fRunTimeLinkSuccess)
    34. MessageBox(L"Message printed from executable\n");
    35. }
    To copy to clipboard, switch view to plain text mode 

    Function createWindow() is as follows:
    Qt Code:
    1. extern "C" __declspec(dllexport) int createWindow(int argc, char *argv[])
    2. {
    3. new QApplication(argc, argv);
    4. Q_CHECK_PTR( qApp );
    5.  
    6. MainWindow* w = MainWindow::getInstance();
    7. qApp->setMainWidget(w);
    8. w->show();
    9.  
    10. return qApp->exec();
    11. }
    To copy to clipboard, switch view to plain text mode 

    When I click button then MainWindow appears, I close that window and then I click button again -- raising up assert: qcoreapplication: there should be only one application object...

    Then I wrote the following code:
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent * event)
    2. {
    3. qApp->exit();
    4. delete qApp;
    5. }
    To copy to clipboard, switch view to plain text mode 
    But now i'm getting error in _CrtIsValidHeapPointer method after second closing of MainWindow.

    How to unload qt-dll correctly?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Loading Qt DLL from MFC

    I'd simply check if qApp is a valid pointer before creating a new application object.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Aug 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Loading Qt DLL from MFC

    Don't work.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Loading Qt DLL from MFC

    First a comment:
    Not sure what the effect of this code is:
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent * event)
    2. {
    3. qApp->exit();
    4. delete qApp;
    5. }
    To copy to clipboard, switch view to plain text mode 
    because your window is the main application window, the application will destroy it as part of its exit process.
    But you are deleting the application before you are deleting the window, so I am not sure if it ends in a defined state, and if so what that state is.

    You can however try:
    Qt Code:
    1. void MainWindow::closeEvent(QCloseEvent * event)
    2. {
    3. qApp->exit();
    4. delete qApp; //this leaves garbage in the pointer.
    5. qApp = NULL;
    6. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Model loading
    By iksarp in forum Newbie
    Replies: 5
    Last Post: 2nd June 2010, 18:38
  2. Delay DLL loading ?
    By black_coder in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2009, 00:00
  3. Loading a DLL using Qt
    By srohit24 in forum Qt Programming
    Replies: 6
    Last Post: 25th February 2009, 00:27
  4. Loading SVG icons
    By user_mail07 in forum Qt Programming
    Replies: 2
    Last Post: 28th June 2007, 04:12
  5. Problem Loading GIF
    By ScoOteR in forum Newbie
    Replies: 3
    Last Post: 6th June 2007, 10:09

Tags for this Thread

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.