i'm having trouble here. don't know how link , define methods in c++ dll inside header file. how link code::blocks? also, how define method inside header file? know seems stupid question can't find anywhere online.
assuming mean windows because of mingw, it's not bad. example, function in "thedll.dll" named "somedllfunction" takes double
, returns int
might have excerpt:
//dllfunctions.h int somedllfoo(double d) { auto dll = loadlibraryw(l"thedll"); if (!dll) { //error } using footype = int(*)(double); auto func = (footype)getprocaddress(dll, "somedllfunction"); if (!func) { //error } int result = func(d); if (!freelibrary(dll)) { //error } return result; } //whatever.cpp #include "dllfunctions.h" int main() { int result = somedllfoo(3.2); }
you take further writing more generic caller. following not work void return type, since assigns result, that's addable:
template<typename ret, typename... args> ret calldllfunction(const std::wstring &dllname, const std::string &funcname, args... args) { auto dll = loadlibraryw(dllname.c_str()); if (!dll) { //error } using functype = ret(*)(args...); auto func = (functype)getprocaddress(dll, funcname.c_str()); if (!func) { //error } ret result = func(args...); if (!freelibrary(dll)) { //error } return result; } int main() { //tested example calldllfunction<int>(l"user32", "messageboxw", nullptr, nullptr, nullptr, 0); }
that should work, or @ least on right track. of course, said, doesn't work void return types until add version of it. extend __stdcall functions. well, idea set error mechanism function dll's errors can handled. if it's uses setlasterror
, might overridden freelibrary
call. depends on need do. might consider putting dll
smart pointer calls freelibrary
when function ends.
Comments
Post a Comment