Click here to Skip to main content
15,946,316 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
PinnedHOW TO ANSWER A QUESTION PinPopular
Chris Maunder12-Jul-09 22:37
cofounderChris Maunder12-Jul-09 22:37 
PinnedHOW TO ASK A QUESTION PinPopular
Chris Maunder12-Feb-09 17:19
cofounderChris Maunder12-Feb-09 17:19 
QuestionC++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE Pin
Oscar Kogosov 202322-Jul-24 20:37
Oscar Kogosov 202322-Jul-24 20:37 
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
<pre lang="C++">#include <iostream>
#include <unordered_map>
#include <functional>
#include <windows.h>

#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<string, function<int(int, int)>>* mydata = new unordered_map<string, function<int(int, int)>>;
  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<string, function<int(int, int)>>* 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 != mydata->end())
   // Exception thrown: read access violation.
   // fault in functional.h line 920
   // it->second.vfptr = addr(??? ??? ??? ??? ???)
    cout << "\nKey: " << it->first << " Value: " << it->second(4, 2) << std::endl;
  else
    cout << "Error\n";
  //===================================

  delete m;
  delete mydata;
  return 0;
}
//=============================================================
2 Dll files: Geometry.h, Geometry.cpp
file Geometry.h
C++
#pragma once
#include <iostream>
#include <unordered_map>
#include <functional>
#include <windows.h>

#ifdef GEOMETRY_EXPORTS
#define GEOMETRY_API __declspec(dllexport)
#else
#define GEOMETRY_API __declspec(dllimport)
#endif

using namespace std;
namespace NS
{
  class GEOMETRY_API Geometry
  {
  public:
    Geometry();
    int Circle(int x, int y);
    int Rectangle(int x, int y);
  };
  extern "C" void  GEOMETRY_API  CreateInstance(unordered_map<string, function<int(int, int)>>* mydata);
  extern "C" void  GEOMETRY_API  Cleanup();
}

file Geometry.cpp
C++
#include "pch.h"
#include "Geometry.h"

namespace NS
{
   Geometry* geometryInstance;
  void CreateInstance(unordered_map<string, function<int(int, int)>>* mydata)
  {
    geometryInstance = new Geometry;
    // Can't capture a static variable in a lambda
    //(*mydata)["circle"] = [geometryInstance](int x, int y) { return geometryInstance->Circle(x, y); };
    //(*mydata)["rectangle"] = [geometryInstance](int x, int y) { return geometryInstance->Rectangle(x, y); };

    (*mydata)["circle"] = bind(&Geometry::Circle, geometryInstance, placeholders::_1, placeholders::_2);
    (*mydata)["rectangle"] = bind(&Geometry::Rectangle, geometryInstance, placeholders::_1, placeholders::_2);
  }
  Geometry::Geometry() {}
  int Geometry::Circle(int x, int y) { return 3.14 * x * x; }
  int Geometry::Rectangle(int x, int y) { return x * y; }
  void Cleanup()
  {
        delete geometryInstance;
        geometryInstance = nullptr;
  }
}

AnswerRe: C++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE Pin
Richard MacCutchan23-Jul-24 0:48
mveRichard MacCutchan23-Jul-24 0:48 
GeneralRe: C++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE Pin
Oscar Kogosov 202320hrs 21mins ago
Oscar Kogosov 202320hrs 21mins ago 
GeneralRe: C++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE Pin
Richard MacCutchan20hrs 11mins ago
mveRichard MacCutchan20hrs 11mins ago 
GeneralRe: C++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE Pin
Oscar Kogosov 202320hrs ago
Oscar Kogosov 202320hrs ago 
GeneralRe: C++20 UNORDERED_MAP OBJECT WITH CLASS FUNCTIONS FROM DLL AS VALUE Pin
Richard MacCutchan19hrs 48mins ago
mveRichard MacCutchan19hrs 48mins ago 
QuestionglReadPixels wrong values Pin
etechX221-Jul-24 16:30
etechX221-Jul-24 16:30 
AnswerRe: glReadPixels wrong values Pin
Victor Nijegorodov21-Jul-24 19:53
Victor Nijegorodov21-Jul-24 19:53 
GeneralRe: glReadPixels wrong values Pin
etechX221-Jul-24 20:35
etechX221-Jul-24 20:35 
QuestionC program(homework) Pin
Member 1631107418-Jul-24 20:36
Member 1631107418-Jul-24 20:36 
AnswerRe: C program(homework) Pin
OriginalGriff18-Jul-24 20:37
mveOriginalGriff18-Jul-24 20:37 
GeneralRe: C program(homework) Pin
Member 1631107418-Jul-24 20:50
Member 1631107418-Jul-24 20:50 
GeneralRe: C program(homework) Pin
OriginalGriff18-Jul-24 20:59
mveOriginalGriff18-Jul-24 20:59 
GeneralRe: C program(homework) Pin
Member 1631107418-Jul-24 21:23
Member 1631107418-Jul-24 21:23 
GeneralRe: C program(homework) Pin
OriginalGriff18-Jul-24 21:38
mveOriginalGriff18-Jul-24 21:38 
GeneralRe: C program(homework) Pin
Member 1631107418-Jul-24 21:48
Member 1631107418-Jul-24 21:48 
GeneralRe: C program(homework) Pin
OriginalGriff19-Jul-24 1:17
mveOriginalGriff19-Jul-24 1:17 
GeneralRe: C program(homework) Pin
OriginalGriff19-Jul-24 1:21
mveOriginalGriff19-Jul-24 1:21 
SuggestionRe: C program(homework) Pin
David Crow19-Jul-24 4:56
David Crow19-Jul-24 4:56 
GeneralRe: C program(homework) Pin
RedDk19-Jul-24 7:16
RedDk19-Jul-24 7:16 
GeneralRe: C program(homework) Pin
David Crow19-Jul-24 3:00
David Crow19-Jul-24 3:00 
QuestionHow to use QUERY_CHANGES_VIRTUAL_DISK_RANGE when HyperV backup Pin
DingHuaming17-Jul-24 18:39
DingHuaming17-Jul-24 18:39 
AnswerRe: How to use QUERY_CHANGES_VIRTUAL_DISK_RANGE when HyperV backup Pin
jschell18-Jul-24 14:24
jschell18-Jul-24 14:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.