1

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
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Sel_va
  • 588
  • 5
  • 25
  • 2
    Why are you using `void *`? This is also not a "reference" but a pointer. But I also think you might need to show us the exact and full error message. – crashmstr Sep 05 '14 at 12:20
  • Error 11 error LNK2001: unresolved external symbol "public: void __thiscall A::PrintTestMsg(void)" (?PrintTestMsg@A@@QAEXXZ) D:\write2\ConsoleApplication1\LogManager.obj LogManager – Sel_va Sep 05 '14 at 12:27
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – πάντα ῥεῖ Sep 05 '14 at 12:29
  • The confusion that i have is whether a method in A.dll can be called in B.dll like how i have done ? (by passing the object pointer from A.dll) – Sel_va Sep 05 '14 at 12:29
  • 1
    You should link against the `.lib` file generated for `A.dll` and use that stuff from there (not `void*`). – πάντα ῥεῖ Sep 05 '14 at 12:30

1 Answers1

1

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.

Matt
  • 6,010
  • 25
  • 36
  • Thanks Matt.. so , should i need to pass the object pointer of the AChild while loading B.dll ? – Sel_va Sep 05 '14 at 12:40