How To Load (C++) Library in QT and Call function ?
-
wrote on 22 Jun 2017, 11:03 last edited by
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; }
-
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; }
wrote on 22 Jun 2017, 11:17 last edited by m.sueHi @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.
-
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.
-
wrote on 22 Jun 2017, 11:24 last edited by m.sue
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.
-
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.
wrote on 22 Jun 2017, 12:45 last edited by Taz742bool 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.
1/5