Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically importing a C++ class from a DLL

What is the correct way to import a C++ class from a DLL? We're using Visual C++.

There's the dllexport/exports.def+LoadLibrary+GetProcAddress trifecta, but it doesn't work on C++ classes, only C functions. Is this due to C++ name-mangling? How do I make this work?

like image 734
Shachar Avatar asked Sep 21 '08 11:09

Shachar


1 Answers

You need to add the following:

extern "C"
{
...
}

to avoid function mangling.

you might consider writing two simple C functions:

SomeClass* CreateObjectInstace()
{
    return new SomeClass();
}

void ReleaseObject(SomeClass* someClass)
{
   delete someClass;
}

By only using those functions you can afterward add/change functionality of your object creation/deletion. This is sometimes called a Factory.

like image 180
Dror Helper Avatar answered Sep 28 '22 09:09

Dror Helper