Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE

C++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE

Scheduled Pinned Locked Moved C / C++ / MFC
c++helplinqfunctionalquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Oscar Kogosov 2023
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • O Oscar Kogosov 2023

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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.

      O 1 Reply Last reply
      0
      • L Lost User

        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.

        O Offline
        O Offline
        Oscar Kogosov 2023
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • O Oscar Kogosov 2023

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You are welcome. And remember the key word in AI is "artificial".

          O 1 Reply Last reply
          0
          • L Lost User

            You are welcome. And remember the key word in AI is "artificial".

            O Offline
            O Offline
            Oscar Kogosov 2023
            wrote on last edited by
            #5

            Exactly. Apropo - your profile is very similar to mine - also retired, started o Mainframe (Asm, Cobol, PL/1) and continue to learn new soft.

            L 1 Reply Last reply
            0
            • O Oscar Kogosov 2023

              Exactly. Apropo - your profile is very similar to mine - also retired, started o Mainframe (Asm, Cobol, PL/1) and continue to learn new soft.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Yes, parallel processing the two of us. :-D

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups