Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Is it possible to use a dll created by Qt inside MFC ?

Is it possible to use a dll created by Qt inside MFC ?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 4 Posters 839 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    ynakane
    wrote on last edited by
    #1

    Hi.

    I created a simple dll file referring to the video below.
    https://www.youtube.com/watch?v=ZewJ4iHQvXY

    I used the LoadLibrary function in mfc, but it cannot work.

    HMODULE module = LoadLibrary(_T("MyLib.dll")); // DLL was created by qt
    

    My question is that is it possible to use a Qt created library inside mfc?
    If the above is possible, I would like an indication of which procedure is missing or please let me know which site I should refer to.

    thank you.

    JKSHJ 1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      Qt and MFC are just C++ libraries. You are not "creating a dll in Qt". You're just creating a dll. It doesn't matter if it uses Qt, MFC, stl, boost or any other library inside.
      LoadLibrary is not MFC. That's a low level WinAPI function. MFC has nothing to do with loading dlls.

      As to what's wrong with your code - how should we know? "it cannot work" is not describing what's happening. Is it crashing? Does LoadLibrary return null? Does it load but something else is not right with it? If it returns null have you called GetLastError to see what the error was?

      My blind guess is that LoadLibrary can't find your dll. Where is your dll located relative to the exe? The search order is: your executable location i.e. if the dll is placed right next to it, then a working directory (which can be different depending on how you run your app), then the directories in the PATH global system variable and then system locations (so for example System32 dir).

      JonBJ Y 2 Replies Last reply
      2
      • Chris KawaC Chris Kawa

        Qt and MFC are just C++ libraries. You are not "creating a dll in Qt". You're just creating a dll. It doesn't matter if it uses Qt, MFC, stl, boost or any other library inside.
        LoadLibrary is not MFC. That's a low level WinAPI function. MFC has nothing to do with loading dlls.

        As to what's wrong with your code - how should we know? "it cannot work" is not describing what's happening. Is it crashing? Does LoadLibrary return null? Does it load but something else is not right with it? If it returns null have you called GetLastError to see what the error was?

        My blind guess is that LoadLibrary can't find your dll. Where is your dll located relative to the exe? The search order is: your executable location i.e. if the dll is placed right next to it, then a working directory (which can be different depending on how you run your app), then the directories in the PATH global system variable and then system locations (so for example System32 dir).

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #3

        @Chris-Kawa said in Is it possible to use a dll created by Qt inside MFC ?:

        My blind guess is that LoadLibrary can't find your dll.

        @ynakane
        Probably what @Chris-Kawa is the case. Additionally, even if it can find your .dll it will not load if it cannot find all the other Qt DLLs on which that (presumably, depending on your code) depends, and we don't know where you have put them such that they can be found.

        Y 1 Reply Last reply
        1
        • Y ynakane

          Hi.

          I created a simple dll file referring to the video below.
          https://www.youtube.com/watch?v=ZewJ4iHQvXY

          I used the LoadLibrary function in mfc, but it cannot work.

          HMODULE module = LoadLibrary(_T("MyLib.dll")); // DLL was created by qt
          

          My question is that is it possible to use a Qt created library inside mfc?
          If the above is possible, I would like an indication of which procedure is missing or please let me know which site I should refer to.

          thank you.

          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @ynakane said in Is it possible to use a dll created by Qt inside MFC ?:

          My question is that is it possible to use a Qt created library inside mfc?

          That is exactly what the QtWinMigrate project does:

          • https://github.com/qtproject/qt-solutions/blob/master/qtwinmigrate/examples/qtdll/main.cpp
          • https://github.com/qtproject/qt-solutions/blob/master/qtwinmigrate/examples/mfc/step1/qtmfc.cpp#L185-L208

          Note that this project is very old and parts of it are obsolete. See the comments at https://bugreports.qt.io/browse/QTBUG-105719 -- the final comment shows how to use a Qt-based DLL in an MFC app without QtWinMigrate.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          Y 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            Qt and MFC are just C++ libraries. You are not "creating a dll in Qt". You're just creating a dll. It doesn't matter if it uses Qt, MFC, stl, boost or any other library inside.
            LoadLibrary is not MFC. That's a low level WinAPI function. MFC has nothing to do with loading dlls.

            As to what's wrong with your code - how should we know? "it cannot work" is not describing what's happening. Is it crashing? Does LoadLibrary return null? Does it load but something else is not right with it? If it returns null have you called GetLastError to see what the error was?

            My blind guess is that LoadLibrary can't find your dll. Where is your dll located relative to the exe? The search order is: your executable location i.e. if the dll is placed right next to it, then a working directory (which can be different depending on how you run your app), then the directories in the PATH global system variable and then system locations (so for example System32 dir).

            Y Offline
            Y Offline
            ynakane
            wrote on last edited by
            #5

            @Chris-Kawa
            Thank you for your reply.
            Your comments helped me understand the basics of DLLs, Qt and MFC.

            I will explain the situation in more detail below
            LoadLibrary returns NULL.
            GetLastError returns 127.
            (The specified procedure could not be found.)
            The DLL is placed in the same directory as the .exe.

            1 Reply Last reply
            0
            • JonBJ JonB

              @Chris-Kawa said in Is it possible to use a dll created by Qt inside MFC ?:

              My blind guess is that LoadLibrary can't find your dll.

              @ynakane
              Probably what @Chris-Kawa is the case. Additionally, even if it can find your .dll it will not load if it cannot find all the other Qt DLLs on which that (presumably, depending on your code) depends, and we don't know where you have put them such that they can be found.

              Y Offline
              Y Offline
              ynakane
              wrote on last edited by
              #6

              @JonB
              As you said, I'm assuming it can't find the other Qt DLLs it depends on.
              I have poor knowledge about DLLs and need to learn how to use them in general.
              Thank you.

              1 Reply Last reply
              0
              • JKSHJ JKSH

                @ynakane said in Is it possible to use a dll created by Qt inside MFC ?:

                My question is that is it possible to use a Qt created library inside mfc?

                That is exactly what the QtWinMigrate project does:

                • https://github.com/qtproject/qt-solutions/blob/master/qtwinmigrate/examples/qtdll/main.cpp
                • https://github.com/qtproject/qt-solutions/blob/master/qtwinmigrate/examples/mfc/step1/qtmfc.cpp#L185-L208

                Note that this project is very old and parts of it are obsolete. See the comments at https://bugreports.qt.io/browse/QTBUG-105719 -- the final comment shows how to use a Qt-based DLL in an MFC app without QtWinMigrate.

                Y Offline
                Y Offline
                ynakane
                wrote on last edited by
                #7

                @JKSH
                Thank you for the very useful information.
                I'll research about QtWinMigrate and https://bugreports.qt.io/browse/QTBUG-105719.

                1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #8

                  Yup, that error indicates that the dll is trying to load other dependent dlls but can't find them.
                  When you crated the dll via the wizard there was a page where you selected which Qt modules is your dll going to use. For each of these modules there's a Qt's dll. It is located in the bin directory inside your Qt installation directory. You need to copy all the Qt module dlls that you're using over to your dll directory. For example if you're using the core module you need Qt6Core.dll (for Release build) or Qt6Cored.dll (for Debug build).

                  JKSHJ 1 Reply Last reply
                  1
                  • Chris KawaC Chris Kawa

                    Yup, that error indicates that the dll is trying to load other dependent dlls but can't find them.
                    When you crated the dll via the wizard there was a page where you selected which Qt modules is your dll going to use. For each of these modules there's a Qt's dll. It is located in the bin directory inside your Qt installation directory. You need to copy all the Qt module dlls that you're using over to your dll directory. For example if you're using the core module you need Qt6Core.dll (for Release build) or Qt6Cored.dll (for Debug build).

                    JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on last edited by
                    #9

                    @Chris-Kawa said in Is it possible to use a dll created by Qt inside MFC ?:

                    You need to copy all the Qt module dlls that you're using over to your dll directory.

                    You can also do this by running the windeployqt.exe tool on your DLL: https://doc.qt.io/qt-6/windows-deployment.html#the-windows-deployment-tool

                    If you have installed multiple versions of Qt, each version has its own copy of windeployqt.exe. Make sure you run the correct one.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    1
                    • Y Offline
                      Y Offline
                      ynakane
                      wrote on last edited by
                      #10

                      @JKSH , @Chris-Kawa
                      Thank you for providing very useful information.
                      Now I am able to successfully use the qt-based dll in my mfc app.

                      By the way, my final goal is to draw the parts-dll such as pushbuttons created by qt quick in the pane of the mfc app.
                      I haven't been able to find a similar example, so if you know a way or any useful information please let me know.
                      Thank you.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved