How to call a member of a class in another DLL.

Posted on 2011-10-06
Last Modified: 2012-05-12

I have a class with a member (function)

class A
 void b();

A *a=new A;

now I pass the a as a parameter to a DLL function.

So now in the DLL there is a defenition:
A *a;

Now I try to call be from the DLL:  a->b();

The problem is that I get unresolved error when I compile the DLL.


Question by:VapiSoft
    LVL 32

    Expert Comment

    that doesn't work that way.

    you would need to have an own dll where the class A was implemented. then you could export the class by using the __declspec(dllexport) specifier. if done so far you could add the import library which was built with the dll to both your application project and the dll project and both can use the class A. you can use a common header file for class A which uses __declspec(dllimport) as specifier when it was used for import.



    Author Comment

    I did solve the problem by adding "virtual" infront of the void b();

     virtual vid b();

    and it works OK.
    LVL 32

    Expert Comment

    the 'unresolved external' error was a linker error which could not be solved using the virtual keyword. the virtual was a compiler directive that tells the function b might get virtually overloaded by a class derived from A. the compiler then would add code to support that functionality. the linker nevertheless would need the implemented and compiled object code of function A::b() and it would complain if missing.

    I assume you added the a.cpp to the dll project what is a workaround.


    Author Comment

    It is very interesting.
    I don't have the b() function in the DLL.
    before I added the virtual I received the unresolved, but now I don't.

    But in the caller DLL I recieved the unresolved error even after I added the virtual, and it was resolved only after I defined the A:b(){} function.

    LVL 32

    Accepted Solution

    that is what i told you.

    i didn't know that you reported a linker error of your application project (and if you look at your initial q. you will see that you only told of a dll error)..

    you don't need the A::b() in the app project if you don't call the b function from there. here the virtual keyword could cause a changed behavior though the error was not really justified.

    as told a workaround to use the A::b() in the dll could be to add the a.cpp to the project. but you shouldn't use a copy of the source but should use a common folder where the a.cpp resides. a 3rd aletrnative is to create a static library out of class A and add the lib to both projects.


    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
    In Easy String Encryption Using CryptoAPI in C++ ( I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now