Is it possible to use a dll created by Qt inside MFC ?
-
Hi.
I created a simple dll file referring to the video below.
https://www.youtube.com/watch?v=ZewJ4iHQvXYI 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.
-
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).
-
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).
@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. -
Hi.
I created a simple dll file referring to the video below.
https://www.youtube.com/watch?v=ZewJ4iHQvXYI 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.
@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 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).
@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. -
@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. -
@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.
-
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). -
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).@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-toolIf you have installed multiple versions of Qt, each version has its own copy of windeployqt.exe. Make sure you run the correct one.
-
@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.