I followed an excellent article by Rudy Velthuis about using C++ classes in DLL’s. Everything was golden, except that I need access to some classes that do not have corresponding factories in the C++ DLL. How can I construct an instance of a class in the DLL? The classes in question are defined as
class __declspec(dllexport) exampleClass
{
public:
void foo();
};
Now without a factory, I have no clear way of instantiating the class, but I know it can be done, as I have seen SWIG scripts (.i files) that make these classes available to Python. If Python&SWIG can do it, then I presume/hope there is some way to make it happen in Delphi too.
Now I don’t know much about SWIG, but it seems like it generates some sort of map for C++ mangled names? Is that anywhere near right? Looking at the exports from the DLL, I suppose I could access functions & constructor/destructor by index or the mangled name directly, but that would be nasty; and would it even work? Even if I can call the constructor, how can I do the equivalent of “new CClass();” in Delphi?
The correct way to do it is to write a wrapper DLL that exposes a factory for the class you need.
I am not sure hiw SWIG works, but anything that relies on reverse-engineering the name mangling seems a dubious approach.
Besides a C++ object should be created only in C++ code. You ought to leave the object creation semantics to the C++ runtime.
There is a reason why COM exists. Precisely to make this cross language object metaphor work neatly.
I’ve written scores of COM objects that are called from Delphi, python and C#