【问题标题】:Passing Class object of one dll to another dll c++将一个dll的类对象传递给另一个dll c++
【发布时间】:2014-09-05 12:11:31
【问题描述】:

我正在尝试以下情况

我的 A.dll 正在加载 B.dll 并使用 A.dll 中存在的类对象的指针作为加载函数的参数调用它的函数

使用该对象引用我可以从 B.dll 调用 A.dll 的函数吗??

我的B.dll函数如下,

bool LogManagerThread::StartLogThread(void* AObj)
{
    A* pobj;
    pobj = (A*)AObj;
    pobj->PrintTestMsg();
    return true;
}

'A'是A.dll中的类

如果我这样调用,我会收到链接错误为“未解析的外部符号”.. 其中 PrintTestMsg() 是 A.dll 的“A 类”中的方法

Error 11 error LNK2001: unresolved external symbol "public: void __thiscall A::PrintTestMsg(void)" (?PrintTestMsg@A@@QAEXXZ) D:\ilrewrite2\ConsoleApplication1\LogManager.obj LogMa‌​nager

【问题讨论】:

  • 你为什么使用void *?这也不是“参考”而是一个指针。但我也认为您可能需要向我们展示准确且完整的错误消息。
  • 错误 11 错误 LNK2001: 无法解析的外部符号 "public: void __thiscall A::PrintTestMsg(void)" (?PrintTestMsg@A@@QAEXXZ) D:\write2\ConsoleApplication1\LogManager.obj LogManager
  • 我的困惑是 A.dll 中的方法是否可以像我所做的那样在 B.dll 中调用? (通过从 A.dll 传递对象指针)
  • 您应该链接到为A.dll 生成的.lib 文件并从那里使用那些东西(而不是void*)。

标签: c++ dll void-pointers dllexport


【解决方案1】:

根据您的描述:“我的 A.dll 正在加载 B.dll 并通过引用调用它的函数”,所以 A 依赖于 B,现在您想在 B DLL 中使用 A dll 的类,这意味着您有让B依赖A,所以它创建了一个循环,你不能这样实现dll。

这样做的一种方法是:在 B DLL 中实现一组接口,而在 A DLL 中,A 实现这些接口,所以看起来像这样:

//in B DLL

class BInterface
{
    public:        
        virtual void PrintTestMsg() = 0;
};

   //in A DLL,

   class AChild : public BInterface
{
    public:
        virtual void PrintTestMsg()
        {
            //do stuff
        }
};

作为 B DLL 中的函数:

bool LogManagerThread::StartLogThread(BInterface* AObj)
{
   if (!AObj)
     return false;

    AObj->PrintTestMsg();
    return true;
}

这些类型应该通过设计来解决,而不是依赖于类,你应该让类依赖于接口来打破依赖关系。 inversion of control 就是解决这些问题的模式。

【讨论】:

  • 谢谢马特.. 所以,我是否需要在加载 B.dll 时传递 AChild 的对象指针?
猜你喜欢
  • 2015-09-15
  • 2017-03-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多