How To Load (C++) Library in QT and Call function ?



  • bool GlobalAll::WhatisMyIP(std::wstring& out_name )
    {
        typedef bool (* LPDLLFUNC) (std::wstring&);
    
        QLibrary lib("ELService.dll");
        lib.load();
    
        if(lib.isLoaded())
            qDebug() << "its loaded";
    
        HINSTANCE hDLL;
        LPDLLFUNC lpfnDllFunc = NULL;
    
        if(!hDLL )
            return false;
    
        lpfnDllFunc = (LPDLLFUNC)lib.resolve("WhatIsMyIP");
        if(!lpfnDllFunc )
            return false;
    
        lpfnDllFunc(out_name);
    
        qDebug() << QString::fromStdWString(out_name);
    
        return true;
    }
    


  • Hi @Taz742

    There is just one problem. This is a C interface. So, no reference, no bool, no classes, etc.

    Just pointers and C standard types. If some C++ types work anyway, please consider it as a happy co-incidence that may not work on someone else's machine.

    -Michael.



  • @m.sue
    I can not understand exactly what you want to say.
    Can you write any example?



  • Hi @Taz742

    Your code is correct, but you can only use C standard types or pointers to such types.

    typedef bool (* LPDLLFUNC) (std::wstring&);
    ->
    typedef int (* LPDLLFUNC) (char *, int len);

    You maybe lucky that a class pointer works but I would not bet on it.

    -Michael.



  • @m.sue

    bool GlobalAll::GetContactNameByID(string tin, std::wstring& out_name )
    {
        typedef bool (* LPDLLFUNC) (string, string, string, std::wstring&);
    
        HINSTANCE hDLL;
        LPDLLFUNC lpfnDllFunc = NULL;
    
        hDLL = LoadLibrary (L"C:\\Users\\tleladze\\Desktop\\build-user_-Desktop_Qt_5_7_0_MinGW_32bit-Debug\\debug\\ELService.dll");
        wstring sName;
    
        if(!hDLL )
            return false;
    
        lpfnDllFunc = (LPDLLFUNC)GetProcAddress(hDLL, "GetContactNameByID");
        if(!lpfnDllFunc )
            return false;
    
        lpfnDllFunc("arkania:206322102", "123456", tin, out_name);
    
        FreeLibrary (hDLL);
    
        return true;
    }
    

    i copy this code, and it worked fine in c++. but QT not.


Log in to reply
 

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