开发者

2 way communication with main exec module and dynamic lib

When I want to call a function from a dynamic linked library, I'm using dlopen then dlsym from <dlfcn.h>.

But what is the right way to call functions from the main executable module from within dll functions? Is the only correc开发者_StackOverflowt way to pass function-pointers into dll module and calling them (for progress reporting purposes, special logging, etc.)?


Libraries that you link at run-time should really not be too central to your program flow; rather, they should be at the "leaf ends", if you will. Especially general purpose libraries usually exhibit only functions that accomplish one particular task (e.g. encrypt something).

Think about it, all you can import from a library at runtime is a single function. The library cannot possibly know your entire program state and interact with it meaningfully.

It all boils down to design. If you design the library yourself, and you want integral parts of your program to be loaded at runtime, then you have to supply a thorough amount of callback interfaces. But if the library is that tightly coupled, you might consider linking it into your main program directly.

Finally, since the dlopen-style interface only handles C-style function pointers, you're essentially limited to a C-style interface. You could pass function pointers through, if you like, but see above on considerations whether this leads to good design. Having a C++ interface (e.g. exporting functions that take classes by references) is a lot trickier because you'll have to ensure that the library (and all future incarnations of 3rd-party supplied libraries) conform to the same ABI, and the C++ ABI is a lot more complex and subject to change than the C ABI.

Think about it like this: Use run-time loading if you want a plugin architecture for which third parties can at later times write code that can be used by your program. If that doesn't make sense, use normal linking at compile time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜