Re: Dynamic linking with symbols



Arnaud Debaene schrieb:

Not at all. I've got several singeltons and stuff like that. My plugin
simply registers some callbacks inside these singeltons. Thus I need
to either pass all the singeltons pointers or pass function pointers
to functions hooking the callbacks into the singeltons. Not very
elegant imo.

On the contrary, it's very simple and elegant IMHO : just pass the singletons pointers to the plugin when initializing it! By the way, it's the usual, "standard", way of doing this on Windows.

So, I've got the pointer, and what now?
I dont have the .text to do anything with unless I either export it in the exe or also link it into the dll. The latter will result in a bigger dll and also a duplication of code.
Also I'd basically have two singeltons but with the same ptr (since one static ptr to the instance is in my exe and one is in the dll).
Very ugly imo. If you want to for instance lock access to the singleton in a MySingelton::getInstance Method you'd have to write another wrapper or the dll and the exe could both lock it at once (since the static ..data where a lock is stored is twice in the address space).



Since I'm working with OOP quite a lot I definitly would not want a
plugin writer to use a c style interface (which you would have to use
in your example). Plugins will have to work with objects created in my
executable for instance. Writing c wrappers for these would be a pain.

What's wrong with passing the singletons pointers (or references) to the plugin then?

See above, it breaks the design idea, imho.

Btw, DLLs were not designed for OOP, and they work best with a "C" style interface.

Now this for sure is not true. Almost all modern libs (at least the ones I work with) use c++. Although I admin I only know of one other programm importing the .text into the lib and not the other way around.


The usual idiom is to have the plugin export an "Init" C-style function which takes an initialization structure containing all the "callbacks" the plugin may need (in your case, the singletons' pointers) and returns all entry points in the plugin the main app may need. Those entry points can be either an array of functions pointers, either an interface (pointer) that is implemented by the plugin. The interface (base class) is defined by the main app and is implemented in derived classes by the various plugins. The second way is of course much more OOP-ish.

Well, and how is a plugin supposed to work with objects then? Unless your either put all required methods into the dll or export them from the exe there is imo no way to do it.


Imho exporting my core functionality either from the exe or an
additional dll is the more elegant solution in my case.

In C, it might be, but not in C++ because of name-mangling : this would render the app and plugins incompatible if they are compiled with different compilers (or different versions of the same compiler). The COM approach to this problem (a v-table like array of function pointers) aims precisely at solving this problem.

I do not care about supporting different compiler versions. Whoever writes a plugin either has to share the source or provide it with the same C++ ABI version as the exe was compiled with. On linux for instance the C++ ABI is quite stable from g++ >=3.4 (iirc). On Windows it's quite stable too. Most users either use VC6 oder 2003.


kind regards Philip
.



Relevant Pages

  • Re: How are C++ objects laid out in memory ?
    ... If a C++ class or struct contains no virtual methods, ... There are no function pointers. ... >pointers which I can locate and then invoke the function pointers? ...
    (comp.lang.asm.x86)
  • How are C++ objects laid out in memory ?
    ... Optimizing Compiler v. 13.10.3052". ... But the function pointers declared in above MsgStruct structure have to ... pointers which I can locate and then invoke the function pointers? ...
    (comp.lang.asm.x86)
  • Re: Dynamic linking with symbols
    ... >> do this when you first initialize the DLL. ... > to either pass all the singeltons pointers or pass function pointers ... singletons pointers to the plugin when initializing it! ...
    (microsoft.public.vc.language)
  • Re: virtual functions vs speed
    ... > I have been thinking about using function pointers but that seems as ... Virtual functions work via pointers (just to cover my bases here: ... ever played the game Mortal Kombat? ... there's a character in it called Shang Tsung who can morph into every other ...
    (comp.lang.cpp)
  • Re: working with addresses
    ... >> function pointers). ... Besides the obvious difference between pointers ... of precision as a pointer to char. ... (and you probably also meant to declare 'str' with a few more ...
    (alt.comp.lang.learn.c-cpp)