none
Importing Derived Classes from DLLs RRS feed

  • Question

  • Im trying to export a derived class from a DLL, so the dll code looks like following:

    class __declspec(dllexport) DerivedClass: BaseClass

    {

    public:

      int classmember;

    }

    Im trying to import this class from a Test winapplication, but it seems to be that I need to include a header file where this class is described. I have included one writing just the same code as I wrote before, but I get an error saying that it does not recognize BaseClass.

    I have the BaseClass in another headerfile, which also include other classes that are in other headerfiles. However, I do not want to include 100 headerfiles in my project. The purpose of writing the dll is to pack all that in just one dll file and be ready for use and to be able to make instances of my derivedclass.

    Is there any solution to that problem? Is it possible to export classes in dlls without the need of a headerfile? or is it posible to export derived classes with a headerfile but without the headerfile where its corresponding baseclass is? Is there any other method for compacting a library other than dll with classes just ready to instantenate?

    I know that in C# I do not get this sort of problems. I just include my dll and it is ready for use. I cann call every function, every class and everything I want.

    Thursday, May 18, 2006 3:16 PM

Answers

  • No this is not possible in C++. When you apply dllexport to a class what you really export is just the entry point to the methods (and any static data members) of the class in question: you do not "export" a definition of the class. So in order to be able to use the exported methods on the other side of the DLL boundary the compiler needs the full definition of the class - this means a header file. When it comes down to it dllexport/dllimport have not progressed much beyond the original C functionality. Note: I usually discourage (or at least require some careful thought) the usage of dllexport/dllimport on classes as it does leave you exposed to possible changes in of the name mangling scheme that the compiler uses. These changes don't happen that often but when they do they can cause problems.

    C# (and C++/CLI) is different because they run on the CLR they have meta-data that fully describes the class so in this case what goes into the assembly is a full description of the the class from which the compiler can create the necessary internal representation.

    Also what C#, C++/CLI, and VB produce may have the *.dll suffix but it is very different to the DLL you create using either COM or __declspec(dllexport).

    Thursday, May 18, 2006 4:21 PM
    Moderator

All replies

  • No this is not possible in C++. When you apply dllexport to a class what you really export is just the entry point to the methods (and any static data members) of the class in question: you do not "export" a definition of the class. So in order to be able to use the exported methods on the other side of the DLL boundary the compiler needs the full definition of the class - this means a header file. When it comes down to it dllexport/dllimport have not progressed much beyond the original C functionality. Note: I usually discourage (or at least require some careful thought) the usage of dllexport/dllimport on classes as it does leave you exposed to possible changes in of the name mangling scheme that the compiler uses. These changes don't happen that often but when they do they can cause problems.

    C# (and C++/CLI) is different because they run on the CLR they have meta-data that fully describes the class so in this case what goes into the assembly is a full description of the the class from which the compiler can create the necessary internal representation.

    Also what C#, C++/CLI, and VB produce may have the *.dll suffix but it is very different to the DLL you create using either COM or __declspec(dllexport).

    Thursday, May 18, 2006 4:21 PM
    Moderator
  • Thanks for the explanation. I just want to ask you some things that you just said.

     What does name mangling scheme means? What is the difference between a common DLL and a DLL created with the Component Object Model? What is the COM exactly about?

    Friday, May 19, 2006 5:37 AM
  • Name mangling is the way the compiler creates pseudo names that represents the function in the object files. Functions can have the same way, but different arguments, so there is a difference in the way the name is mangled so that the linker can see the difference.

    Whats COM? Please read a book about this.

    A COM DLL is surely a standard DLL with a specific functionality that uses the COM API! So in a COM DLL there a functions that registers the interfaces inside the DLL in the COM subsystem, and via this way, other code can create such objects via the COM subsystem.

    Friday, May 19, 2006 11:22 AM
    Moderator