Y
Y
yeputons2011-02-05 13:26:29
API
yeputons, 2011-02-05 13:26:29

Is it possible to make an object-oriented API through a DLL (GCC, C++)?

Good afternoon.
The question is the following - let there be an abstract application to which you want to add some API for plugins. Plugins will most likely be DLLs that are dynamically loaded by the main code.
In Java, you can make an IPlugin interface, and extend it in a plugin: public class MySuperPlugin implements IPlugin , after which you can easily load thousands of classes by name. Is it possible to do something similar in C++? I know that you can safely load regular functions (not class members) from a DLL. So far, only such crutches have been invented: we make many functions of “adapters”, each of which calls the corresponding member of the class. But this is horror.
How to decide? After all, it's probably possible.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SlaX, 2011-02-05
@yeputons

Can. To do this, you need to write a regular function like:


extern "C" IPlugin * getPluginInterface()
{
    return new Plugin();
}

If the code is for Windows, you also need to add __declspec (dllexport) after extern "C".
And that's it. Now you can load the library dynamically and ask the address of the function by name using dlsym (for Linux), I don’t remember for Windows.

A
Alexey Sidorov, 2011-02-05
@Gortauer87

Yes, that's exactly how it's done. We make an abstract class, we make a factory of these very classes, and she already creates them as she wants and from any libs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question