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. Dynamically loading dll and calling functions
Qt 6.11 is out! See what's new in the release blog

Dynamically loading dll and calling functions

Scheduled Pinned Locked Moved General and Desktop
7 Posts 5 Posters 22.5k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    MonalisaKalla
    wrote on last edited by
    #1

    Hi,

    I am dyamically loadning a dll and calling some functions from the dll in QT and VC++ both.
    The VC++ code works fine but QT code is not giving me the out put as expected.

    Following is the VC++ code:

    @
    typedef CApiInt* (__cdecl *NewApiConnection1)(); //definig a function pointer

    NewApiConnection1 con = (NewApiConnection1) ::GetProcAddress(hInstance, "NewApiConnection") //getting the handle of the function

    CApiInt* m_srv = con();//make pointer of the class and it will point to the same where pCtor is pointing

    DWORD m_clientNbr;

    m_srv->Connect(NULL, &m_clientNbr, NULL);// callig functions of the class.
    @

    The above code work as expected "NewApiConnection will return CApiInt* , then m_srv will point to the returned CApiInt* and the connect will connect to the

    server and increase the client number every time its connect.

    Following is the QT code:

    @
    typedef CApiInt* (__cdecl *NewApiConnection1)();

    QLibrary lib( "C:\Program Files\Plus\Oper\bin\Apiud.dll" );
    lib.load();

    bool bLoaded = lib.isLoaded();
    
    CApiInt* m_srv;
    if(bLoaded)
    {
        NewApiConnection1 con = (NewApiConnection1) lib.resolve("NewApiConnection");
        if(con)
        {
            m_srv = con();//pointer of the class and it will point to the same where con is pointing
        }
    
    
        DWORD m_clientNbr = 0;
        m_srv->Connect(NULL, &m_clientNbr, NULL);
    }
    

    @

    In the above code m_srv=con() is not behaving the same as in VC++, in VC++ all the data member of objects get intialized and when we call function on top it

    behaves correctly, but in QT m_srv is not holding the initialized data members also when i try to see the m_srv contents while debuging it crashes the

    application and gives thie message "gdb-i686-pc-minw32.exe has stopped working" closes the program.
    And connect does not connect to server clientnumber (m_clientNbr) remains 0.

    Please suggest how to resolve this issue w.r.t Qt code.

    Thanks in advance.
    Monalisa

    EDIT: please mark code blocks by @-tags, thanks. Gerolf
    EDIT: Move to desktop. Tobias

    1 Reply Last reply
    0
    • G Offline
      G Offline
      giesbert
      wrote on last edited by
      #2

      Hi,

      first of all, why is this in the forum language binding?

      Now to yourt problem:

      If I interpret you error description correctly, you are using Qt with mingw. Mingw and MSVC and not 100% compatible. If you only export C-style functions, it could work, but that also depends, how the functions are exported from the library. Can you please show this to us?

      Some things I would change in your code:

      @
      typedef CApiInt* (__cdecl *NewApiConnection1)();

      QLibrary lib( "C:\\Program Files\\Plus\\Oper\\bin\\Apiud.dll" );
      lib.load();
      
      bool bLoaded = lib.isLoaded();
      
      CApiInt* m_srv = 0; // ensure an initialised pointer
      if(bLoaded)
      {
          NewApiConnection1 con = (NewApiConnection1) lib.resolve("NewApiConnection");
          if(con)
          {
              m_srv = con();//pointer of the class and it will point to the same where con is pointing
          }
      
          if(0 != m_srv) // check if it was correctly initialised
          {
              DWORD m_clientNbr = 0;
              m_srv->Connect(NULL, &m_clientNbr, NULL);
          }
      }
      

      @

      Nokia Certified Qt Specialist.
      Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

      V 1 Reply Last reply
      1
      • M Offline
        M Offline
        MonalisaKalla
        wrote on last edited by
        #3

        I m sorry for posting on the wrong forum.

        I incorporated the code you suggested.

        @
        if(0 != m_srv) // check if it was correctly initialised
        {
        DWORD m_clientNbr = 0;
        m_srv->Connect(NULL, &m_clientNbr, NULL);
        }
        @

        m_srv is holding a valid address and it is entering into the loop.

        When i called the Connect function , it is not connecting and m_clientNbr is remained 0.

        Please suggest.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          giesbert
          wrote on last edited by
          #4

          then you should debug you library, you are calling

          Nokia Certified Qt Specialist.
          Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

          1 Reply Last reply
          0
          • C Offline
            C Offline
            cincirin
            wrote on last edited by
            #5

            Also, be sure your CApiInt exported class follow the rules "here":http://chadaustin.me/cppinterface.html

            1 Reply Last reply
            0
            • G giesbert

              Hi,

              first of all, why is this in the forum language binding?

              Now to yourt problem:

              If I interpret you error description correctly, you are using Qt with mingw. Mingw and MSVC and not 100% compatible. If you only export C-style functions, it could work, but that also depends, how the functions are exported from the library. Can you please show this to us?

              Some things I would change in your code:

              @
              typedef CApiInt* (__cdecl *NewApiConnection1)();

              QLibrary lib( "C:\\Program Files\\Plus\\Oper\\bin\\Apiud.dll" );
              lib.load();
              
              bool bLoaded = lib.isLoaded();
              
              CApiInt* m_srv = 0; // ensure an initialised pointer
              if(bLoaded)
              {
                  NewApiConnection1 con = (NewApiConnection1) lib.resolve("NewApiConnection");
                  if(con)
                  {
                      m_srv = con();//pointer of the class and it will point to the same where con is pointing
                  }
              
                  if(0 != m_srv) // check if it was correctly initialised
                  {
                      DWORD m_clientNbr = 0;
                      m_srv->Connect(NULL, &m_clientNbr, NULL);
                  }
              }
              

              @

              V Offline
              V Offline
              vikas dhumal
              wrote on last edited by
              #6

              @giesbert How to access external dll in qt (only dll file available dont have header file)
              i have vs2013 .net dll named as System.dll without header file. i want to add it in my qt creator application and access all function of dll .please reply

              aha_1980A 1 Reply Last reply
              0
              • V vikas dhumal

                @giesbert How to access external dll in qt (only dll file available dont have header file)
                i have vs2013 .net dll named as System.dll without header file. i want to add it in my qt creator application and access all function of dll .please reply

                aha_1980A Offline
                aha_1980A Offline
                aha_1980
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @vikas-dhumal

                Please have a look at the QLibrary documentation.

                Regards

                Qt has to stay free or it will die.

                1 Reply Last reply
                1

                • Login

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