我正在尝试如下情况
我的 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 LogManager
最佳答案
根据你的描述:“我的 A.dll 正在加载 B.dll 并通过引用调用它的函数”,所以 A 依赖于 B,现在你想在 B DLL 中使用 A dll 的类,这意味着你有让 B 依赖于 A,所以它创建了一个循环,你不能以这种方式实现 dll。
一种方法是:在B的DLL中实现一组接口(interface),而在A的DLL中,A实现了这些接口(interface),所以看起来是这样的:
//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;
}
这些类型应该通过设计来解决,而不是依赖于类,你应该让类依赖于接口(interface)来打破依赖。 inversion of control是解决这些问题的模式。
关于c++ - 将一个dll的Class对象传递给另一个dll c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25685615/