millionDollahSmile
millionDollahSmile

Reputation: 43

Export a class in dll without the function decoration

I would like to know how to get rid of the decorations around a function of a class that I need to export in a dll. For example, when you have something like this :

extern "C"
{
    __declspec(dllexport) int __cdecl getWhatever();
}

And that you verify with dependencyWalker, the function's name that is exported, you will have exactly the same function Name.

But if you do something similar with a class, there will be a bunch of character decorating the function like this:

extern "C"
{

  class __declspec(dllexport) Toto
  {
    __cdecl Toto(){}
    __cdecl  ~Toto(){}

    int __cdecl getBlob(float y){return (int)y;}
   };

} 

In dependencyWalker you will see this :

??0Toto@@AAE@XZ

??1Toto@@AAE@XZ

??4Toto@@QAEAAV0@ABV0@@Z

?getBlob@Toto@@AAAHM@Z

So how to make it clean like with a procedural function?

Thanks,

Upvotes: 4

Views: 1687

Answers (2)

amanjiang
amanjiang

Reputation: 1283

Do not export a class directly, use an abstract interface, just like COM does.

Here are some nice articles:

Exporting C++ classes from a DLL

,

Binary-compatible C++ Interfaces

&

HowTo Export C++ classes from a DLL

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 613511

You cannot disable name mangling for C++ classes and you cannot export them without mangling. C++ classes support features that require mangling. For example, function overloading.

It's also worth noting that mangling is compiler specific. So if you want your class to be accessible for people using different compilers, or even different languages, then exporting C++ classes from DLLs is a bad design choice.

Upvotes: 5

Related Questions