Dynamically loading dll and calling functions



  • 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



  • 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);
        }
    }
    

    @



  • 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.



  • then you should debug you library, you are calling



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.