Results 1 to 11 of 11

Thread: QPluginLoader instance always returns null

  1. #1
    Join Date
    May 2016
    Posts
    12
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default QPluginLoader instance always returns null

    Greeting

    I created plugin with Qt (5.6.2) and trying to load it but it returns null all the time. I checked several question and also tried the solutions but it didn't work for me.

    Can you take a look of the following code and see whats wrong?

    DeviceManager.hpp

    Qt Code:
    1. #ifndef DEVICE_MANAGER_HPP
    2. #define DEVICE_MANAGER_HPP
    3.  
    4. #include <QtCore>
    5. #include <string>
    6.  
    7. using namespace std;
    8.  
    9. class DeviceManager
    10. {
    11.  
    12. public:
    13. virtual ~DeviceManager() {}
    14.  
    15. virtual bool initialize() = 0;
    16. virtual string getBrandName() = 0;
    17.  
    18. };
    19.  
    20. QT_BEGIN_NAMESPACE
    21. Q_DECLARE_INTERFACE(DeviceManager, "com.some.address/1.0")
    22. QT_END_NAMESPACE
    23.  
    24. #endif //DEVICE_MANAGER_HPP
    To copy to clipboard, switch view to plain text mode 

    DeviceManagerImpl.hpp

    Qt Code:
    1. #ifndef DEVICE_MANAGER_IMPL_HPP
    2. #define DEVICE_MANAGER_IMPL_HPP
    3.  
    4. #include "DeviceManager.hpp"
    5.  
    6. #include <string>
    7. using namespace std;
    8.  
    9. class DeviceManagerImpl : public QObject, public DeviceManager
    10. {
    11. Q_OBJECT
    12. Q_PLUGIN_METADATA(IID "com.some.address/1.0")
    13. Q_INTERFACES(DeviceManager)
    14.  
    15. public:
    16. DeviceManagerImpl();
    17.  
    18. //Override Method
    19. bool initialize(); //Have implementation in cpp file
    20. string getBrandName(); //Have implementation in cpp file
    21.  
    22. private:
    23. ...
    24.  
    25. };
    26.  
    27. #endif //DEVICE_MANAGER_IMPL_HPP
    To copy to clipboard, switch view to plain text mode 

    Pro File

    Qt Code:
    1. QT += core gui sql
    2.  
    3. TARGET = Device-Manager
    4. #TARGET = $$qtLibraryTarget(Device-Manager)
    5. TEMPLATE = lib
    6. CONFIG += plugin
    7.  
    8.  
    9. SOURCES += \
    10. ...
    11.  
    12. HEADERS += \
    13. ...
    14.  
    15. DISTFILES += Device-Manager.json
    16.  
    17. unix {
    18. target.path = /usr/lib
    19. INSTALLS += target
    20. }
    To copy to clipboard, switch view to plain text mode 

    And this is how i try to load the plugin in my main process.

    Qt Code:
    1. QPluginLoader * pluginLoader = new QPluginLoader(pluginPath.c_str());
    2. QObject * plugin = pluginLoader->instance();
    3.  
    4. if (plugin)
    5. {
    6. deviceManager = qobject_cast<DeviceMAnager *>(plugin);
    7. return true;
    8. }
    9. else
    10. {
    11. delete pluginLoader;
    12. return false;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Im using QT 5.6.2 and QT Creator and MinGW 32bit.

    Thanks in advance.

  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: QPluginLoader instance always returns null

    QT_BEGIN_NAMESPACE and QT_END_NAMESPACE should not be needed in the header.

    When loading, is plugin or deviceManager NULL? If plugin is NULL then the plugin DLL is either not found at all, or unable to load. If deviceManager is NULL (and plugin is not) then the loaded plugin does not contain a QObject handling the interface.
    Are you certain the Device-Manager.dll is in the location identified pluginPath?
    Are all of the external runtime dependencies of the plugin DLL met in the environment in which it is running? That is, if Device-Manager.dll depends on foolib.dll is that in the PATH?

  3. #3
    Join Date
    May 2016
    Posts
    12
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPluginLoader instance always returns null

    Quote Originally Posted by ChrisW67 View Post
    QT_BEGIN_NAMESPACE and QT_END_NAMESPACE should not be needed in the header.

    When loading, is plugin or deviceManager NULL? If plugin is NULL then the plugin DLL is either not found at all, or unable to load. If deviceManager is NULL (and plugin is not) then the loaded plugin does not contain a QObject handling the interface.
    Are you certain the Device-Manager.dll is in the location identified pluginPath?
    Are all of the external runtime dependencies of the plugin DLL met in the environment in which it is running? That is, if Device-Manager.dll depends on foolib.dll is that in the PATH?
    Thanks for reply. The plugin is in correct path and pluginLoader is not null but instance() return null. I think that one of run-time dependencies is missing or something. I'm breaking the code to several part and testing it to find out what exactly is wrong!

  4. #4
    Join Date
    May 2016
    Posts
    12
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPluginLoader instance always returns null

    I found the reason but i have no idea why it cause the problem, Im using library that is linked to project but when i use its function, The instance returns null.

    Qt Code:
    1. LIBS += $$(STANDARD_XFS_DIRECTORY)/LIB/MSXFS.lib
    2. INCLUDEPATH += $$(STANDARD_XFS_DIRECTORY)/INCLUDE
    To copy to clipboard, switch view to plain text mode 

    Can anyone tell me why it happens?

  5. #5
    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: QPluginLoader instance always returns null

    mixed build mode perhaps? (release/debug)
    ==========================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.

  6. #6
    Join Date
    May 2016
    Posts
    12
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPluginLoader instance always returns null

    Quote Originally Posted by high_flyer View Post
    mixed build mode perhaps? (release/debug)
    No, Both are in debug!
    The thing is, I did something exactly like this before with CMakeList.txt and Visual Studio but its not working with MinGW and QT Creator . I'm missing something but i have no idea what it is !!!


    Added after 18 minutes:


    The following configs of library and plugin in `CMakeList.txt` work perfectly fine when i make the project with `Visual Studio`. (Made with CMake)

    Qt Code:
    1. ADD_DEFINITIONS(${QT_DEFINITIONS})
    2. ADD_DEFINITIONS(-DUNICODE -D_UNICODE)
    3. ADD_DEFINITIONS(-DQT_PLUGIN)
    4. ADD_DEFINITIONS(-DQT_SHARED)
    5. ADD_DEFINITIONS(-DQT_DLL)
    6. ADD_DEFINITIONS(-DQT_LARGEFILE_SUPPORT)
    7. ADD_DEFINITIONS(-DQT_THREAD_SUPPORT)
    8.  
    9. INCLUDE_DIRECTORIES(
    10. ${STANDARD_XFS_DIRECTORY}/INCLUDE
    11. )
    12.  
    13. LINK_DIRECTORIES(
    14. ${STANDARD_XFS_DIRECTORY}
    15. ${STANDARD_XFS_DIRECTORY}/LIB
    16. )
    17.  
    18. SET(XFS_LIBS
    19. MSXFS
    20. xfs_conf
    21. SSIDLL
    22. )
    23.  
    24. TARGET_LINK_LIBRARIES(Device-Manager
    25. ${XFS_LIBS}
    26. )
    To copy to clipboard, switch view to plain text mode 

    And this is the configs of library and plugin in `.pro` file in `Qt Creator` with `MinGW` which is not working when i use the library.

    Qt Code:
    1. QT -= gui
    2. QT += core sql
    3.  
    4. TARGET = Device-Manager
    5. TEMPLATE = lib
    6. CONFIG += plugin
    7. CONFIG += c++11
    8.  
    9. DEFINES += DEVICEMANAGER_LIBRARY
    10.  
    11. INCLUDEPATH += $$(STANDARD_XFS_DIRECTORY)
    12. INCLUDEPATH += $$(STANDARD_XFS_DIRECTORY)/INCLUDE
    13. message(Include : $${INCLUDEPATH})
    14.  
    15. LIBS += $$(STANDARD_XFS_DIRECTORY)/SSIDLL.lib
    16. LIBS += $$(STANDARD_XFS_DIRECTORY)/LIB/MSXFS.lib
    17. LIBS += $$(STANDARD_XFS_DIRECTORY)/LIB/xfs_conf.lib
    18. message(Lib : $${LIBS})
    To copy to clipboard, switch view to plain text mode 

    So i can say the code it self is fine and i'm definitely missing some configs in `.pro` file.

    PS1: Plugin and the application that load the plugin, Both have `INCLUDEPATH` and `LIBS` of the library in their pro file.

    PS2: The application and the plugin both are in debug mode

    PS3: I dont get any compile error or linker error during compile.
    Last edited by meysam_hashemi; 12th December 2017 at 14:30.

  7. #7
    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: QPluginLoader instance always returns null

    No, Both are in debug!
    do you have any other 3rd party dependencies which might be in release?

    I'd try to build your project in release and try to see if it works, its an easy test worth trying.
    ==========================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.

  8. #8
    Join Date
    May 2016
    Posts
    12
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPluginLoader instance always returns null

    Quote Originally Posted by high_flyer View Post
    do you have any other 3rd party dependencies which might be in release?

    I'd try to build your project in release and try to see if it works, its an easy test worth trying.
    No man i dont

    I made simple console application and included my library header and lib file and called the function and this is the error i get when i run the application. So it must be it. But seriously why!!!

    The Code:

    Qt Code:
    1. LPWFSRESULT result = new WFSRESULT();
    2. WFSFreeResult(result)
    To copy to clipboard, switch view to plain text mode 

    mingw.jpg

  9. #9
    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: QPluginLoader instance always returns null

    Is this typo 'DeviceMAnager' really in the code or only in the code posted here?:
    Qt Code:
    1. if (plugin)
    2. {
    3. deviceManager = qobject_cast<DeviceMAnager *>(plugin);
    4. return true;
    5. }
    To copy to clipboard, switch view to plain text mode 

    I'd add some defencive code here:
    Qt Code:
    1. QPluginLoader * pluginLoader = new QPluginLoader(pluginPath.c_str());
    2. QObject * plugin = pluginLoader->instance();
    3.  
    4. if (plugin)
    5. {
    6. deviceManager = qobject_cast<DeviceMAnager *>(plugin);
    7. if(deviceManager) return true;
    8. return false;;
    9. }
    10. else
    11. {
    12. delete pluginLoader;
    13. return false;
    14. }
    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.

  10. #10
    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: QPluginLoader instance always returns null

    Your screenshot clearly shows error 0xc0000135, which means STATUS_DLL_NOT_FOUND. This goes back to:
    Quote Originally Posted by me
    Are all of the external runtime dependencies of the plugin DLL met in the environment in which it is running? That is, if Device-Manager.dll depends on foolib.dll is that in the PATH?
    Is the MSXFS.DLL in the run time PATH of the process or in the same directory as the plugin DLL? Is everything the MSXFS.DLL depends on also in one of those locations?

  11. #11
    Join Date
    May 2016
    Posts
    12
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Re: QPluginLoader instance always returns null

    Quote Originally Posted by ChrisW67 View Post
    Your screenshot clearly shows error 0xc0000135, which means STATUS_DLL_NOT_FOUND. This goes back to:

    Is the MSXFS.DLL in the run time PATH of the process or in the same directory as the plugin DLL? Is everything the MSXFS.DLL depends on also in one of those locations?
    Yes.

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    Well, I found the problem however its so silly. I run project over windows 10 32 bit and it work fine. Maybe something is not incompatible with 64 bit and that caused this mess.

    Thanks everyone for all your solution and i hope your solutions help others problem.

    Thanks alot!

Similar Threads

  1. QSslCertificate returns NULL in Windows Server 2008 R2
    By plopes21 in forum Qt Programming
    Replies: 3
    Last Post: 18th January 2013, 12:24
  2. QImage() returns Null Image
    By ustulation in forum Qt Programming
    Replies: 7
    Last Post: 24th July 2012, 14:52
  3. Replies: 33
    Last Post: 2nd December 2010, 17:47
  4. QDBusMessage returns NULL string
    By nrabara in forum Qt Programming
    Replies: 0
    Last Post: 27th November 2009, 07:09
  5. QPluginLoader instance
    By Jeff100 in forum Qt Programming
    Replies: 1
    Last Post: 22nd November 2007, 07:47

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.