C++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE
-
2 Errors:
a) Lambda does not not with static var, so I must to work wwith bind
b) I try to invoke functions of class from DLL, saved in unordered_map object and fall.If to invoke functions of class not from DLL it is work properly.
How can this problem be solved?**file UnorderedMapFunctions.cpp**
#include
#include
#include
#include#include "..\\Geometry\\Geometry.h"
using namespace std;namespace NS
{
class Math
{
public:
int Sum(int x, int y) { return x + y; }
int Sub(int x, int y) { return x - y; }
int Mul(int x, int y) { return x * y; }
int Div(int x, int y) { return x / y; }
};
}
using namespace NS;int main()
{
unordered_map>* mydata = new unordered_map>;
Math* m = new Math;// Using lambda functions to bind member functions
(*mydata)["sum"] = [m](int x, int y) { return m->Sum(x, y); };
(*mydata)["sub"] = [m](int x, int y) { return m->Sub(x, y); };
(*mydata)["mul"] = [m](int x, int y) { return m->Mul(x, y); };
(*mydata)["div"] = [m](int x, int y) { return m->Div(x, y); };
/*
//Geometry* g = new Geometry;
(*mydata)["circle"] = [g](int x, int y) { return g->Circle(x, y); };
(*mydata)["rectangle"] = [g](int x, int y) { return g->Rectangle(x, y); };
*/
//==============================================================
string fullname_dll = "Geometry";
HINSTANCE hInst = LoadLibraryA(fullname_dll.c_str());
if (hInst == nullptr)
{
cout << "Error: [FuncImport] wrong name " << fullname_dll.c_str() << endl;
return false;
}
typedef void (CALLBACK* CreateInstanceFunc)(unordered_map>* mydata);
string CreateFuncName = "CreateInstance";
CreateInstanceFunc CreateInstance = (CreateInstanceFunc)GetProcAddress(hInst, CreateFuncName.c_str());
if (CreateInstance == nullptr)
{
cout << "Error: wrong name " << fullname_dll << endl;
FreeLibrary(hInst);
return false;
}
CreateInstance(mydata);
cout << "Imported " << fullname_dll.c_str();
FreeLibrary(hInst);
//==============================================================
// Invoke by 'key' functions, saved in 'mydata'
// it is right!
for (const auto& [k, v] : *mydata)
std::cout << "Key: " << k << " Value: " << v(3, 5) << std::endl;
//===================================
auto it = mydata->find("circle");
if (it != mydat -
2 Errors:
a) Lambda does not not with static var, so I must to work wwith bind
b) I try to invoke functions of class from DLL, saved in unordered_map object and fall.If to invoke functions of class not from DLL it is work properly.
How can this problem be solved?**file UnorderedMapFunctions.cpp**
#include
#include
#include
#include#include "..\\Geometry\\Geometry.h"
using namespace std;namespace NS
{
class Math
{
public:
int Sum(int x, int y) { return x + y; }
int Sub(int x, int y) { return x - y; }
int Mul(int x, int y) { return x * y; }
int Div(int x, int y) { return x / y; }
};
}
using namespace NS;int main()
{
unordered_map>* mydata = new unordered_map>;
Math* m = new Math;// Using lambda functions to bind member functions
(*mydata)["sum"] = [m](int x, int y) { return m->Sum(x, y); };
(*mydata)["sub"] = [m](int x, int y) { return m->Sub(x, y); };
(*mydata)["mul"] = [m](int x, int y) { return m->Mul(x, y); };
(*mydata)["div"] = [m](int x, int y) { return m->Div(x, y); };
/*
//Geometry* g = new Geometry;
(*mydata)["circle"] = [g](int x, int y) { return g->Circle(x, y); };
(*mydata)["rectangle"] = [g](int x, int y) { return g->Rectangle(x, y); };
*/
//==============================================================
string fullname_dll = "Geometry";
HINSTANCE hInst = LoadLibraryA(fullname_dll.c_str());
if (hInst == nullptr)
{
cout << "Error: [FuncImport] wrong name " << fullname_dll.c_str() << endl;
return false;
}
typedef void (CALLBACK* CreateInstanceFunc)(unordered_map>* mydata);
string CreateFuncName = "CreateInstance";
CreateInstanceFunc CreateInstance = (CreateInstanceFunc)GetProcAddress(hInst, CreateFuncName.c_str());
if (CreateInstance == nullptr)
{
cout << "Error: wrong name " << fullname_dll << endl;
FreeLibrary(hInst);
return false;
}
CreateInstance(mydata);
cout << "Imported " << fullname_dll.c_str();
FreeLibrary(hInst);
//==============================================================
// Invoke by 'key' functions, saved in 'mydata'
// it is right!
for (const auto& [k, v] : *mydata)
std::cout << "Key: " << k << " Value: " << v(3, 5) << std::endl;
//===================================
auto it = mydata->find("circle");
if (it != mydatI find your code somewhat difficult top understand. However as far as far as I can guess, the problem occurs in the following lines:
FreeLibrary(hInst);
//==============================================================
// Invoke by 'key' functions, saved in 'mydata'
// it is right!
for (const auto& [k, v] : *mydata)
std::cout << "Key: " << k << " Value: " << v(3, 5) << std::endl;
//===================================You call
FreeLibrary
and then (I think) try to invoke the functions that are served by that library. -
I find your code somewhat difficult top understand. However as far as far as I can guess, the problem occurs in the following lines:
FreeLibrary(hInst);
//==============================================================
// Invoke by 'key' functions, saved in 'mydata'
// it is right!
for (const auto& [k, v] : *mydata)
std::cout << "Key: " << k << " Value: " << v(3, 5) << std::endl;
//===================================You call
FreeLibrary
and then (I think) try to invoke the functions that are served by that library.My very stupid mistake! You right Richard, thank you very much. I convert my project(GitHub - okogosov/PPL: Parenthesis Programming Language (PPL)[^]) from C# to C++ and in C# I do not use FreeLibrary. But I use AI MONICA a little for convertation and call "FreeLibrary" will be generated and added by AI. I express my gratitude to you (and MONICA also) and to all my colleaques for help with the project that I am working alone.
-
My very stupid mistake! You right Richard, thank you very much. I convert my project(GitHub - okogosov/PPL: Parenthesis Programming Language (PPL)[^]) from C# to C++ and in C# I do not use FreeLibrary. But I use AI MONICA a little for convertation and call "FreeLibrary" will be generated and added by AI. I express my gratitude to you (and MONICA also) and to all my colleaques for help with the project that I am working alone.
-
Exactly. Apropo - your profile is very similar to mine - also retired, started o Mainframe (Asm, Cobol, PL/1) and continue to learn new soft.
-
Exactly. Apropo - your profile is very similar to mine - also retired, started o Mainframe (Asm, Cobol, PL/1) and continue to learn new soft.