Sel_va
Sel_va

Reputation: 608

Passing Class object of one dll to another dll c++

I m trying to for a situation as below

My A.dll is loading B.dll and call it's function with pointer of the class object that present in A.dll as parameter to the loading function

Using that object reference can i call a function of A.dll from B.dll??

My B.dll function is as follows,

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

'A' is the class in A.dll

If i call this way I am getting linking error as "unresolved external symbol".. where PrintTestMsg() is the method in "class A" of A.dll

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

Upvotes: 1

Views: 1666

Answers (1)

Matt
Matt

Reputation: 6050

Based on your description: "My A.dll is loading B.dll and call it's function with reference", so A depends on B, now you want to use classes of A dll in the B DLL, this means you have let B depends on A, so it creates a loop, you can't implement the dlls in this way.

One way to do this is: implement a set of interfaces in B DLL, while in A DLL, A implements these interfaces, so it look this:

//in B DLL

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

   //in A DLL,

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

as the function in the B DLL:

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

    AObj->PrintTestMsg();
    return true;
}

These kinds should be resolved via design, instead of depending on class, you should let classes depend on the interfaces to break the dependencies. inversion of control is the pattern to solve these problems.

Upvotes: 1

Related Questions