Interface Design in C/C ++ project DLL

Source: Internet
Author: User

Interface Design in C/C ++ project DLL

From:
Http://www.codeproject.com/Tips/441838/Designing-the-Interface-of-DLLs-in-C-Cplusplus-P

Introduction
This tip assumes that you are familiar with DLL usage and related terms like static/dynamic linking. if you have no idea how to use DLLs, then read this tip after learning the DLL related basics here (cross-platform wikipedia page), here (windows) or here (unix: linux, bsd, osx, etc .).

My advice is to never use DLLs in your project if you don't have to because they are trouble. if you still have to fall back on putting some C/C ++ code to DLLs (or you have to create a plug-in interface) and it's up to you to design the DLL interface (that is used only by your C/C ++ code), then read along. there is a minimalistic zipped project file attached with a Visual C ++ 2010 solution and a unix build script to demonstrate plugin (DLL/shared library) loading on Windows and unix (linux/bsd/osx) platforms.

How shoshould a DLL Interface Look Like In My Opinion
First, declare an interface (a class that has only pure virtual methods) in the public header file of your DLL. It's quite important to use only pure virtual methods! After this, put the only exported function to your DLL that returns a pointer to this interface. after this, in the private source files of your DLL, you can derive a class from this interface and implement its methods. now the users of the DLL just call the interface getter method of the DLL and then continue using it in a nice C ++ ish way by calling the methods of the retrieved interface pointer.

Don't forget to read the attached example source code. It is very simple and short.

Why Is This Good?
It simply looks nicer.
As a result of the thin linking interface between your DLL and the module that uses it-just one exported DLL function-it's very easy to make this cross-platform because the ugly ifdefs and platform dependent code is minimal. the thin interface makes it easy to implement platform independent delay loading.
It's very easy to merge this DLL code into your application later as part of a refactorization if you decide to do so-it's a breeze.
Let's say you are writing a plug-in interface for your program-it's very easy to put internal plug-ins to your executable by implementing the DLL interface inside your program. it becomes very easy to switch between an built in functionality and one that is implemented in a DLL, for your executable both kinds are visible just as a simple interface pointer.

Versioning Plug-in DLLs (My Style)
There are a lot of ways to detect the version of plug-ins. for example, by coding the version into the versioninfo resource of the DLL, coding the version to the filename, getting the version by calling a version getter exported function of the DLL... I use a totally different approach that has several advantages over the previusly listed methods.

Let's say I have a media player program and a codec plug-in for it with a specific interface. after some time, I release a new version of the media player that has a modified interface but I decide to release the plug-in for older versions of the media player as well. I do this by writing a single DLL that supports both the old and the new version of the plugin interface. the public interface header of my plug-in DLL will contain the declaration of both the old and the new version of the interface and it contains two exported functions: one of them returns the old, and the other returns the new interface pointer. the exported methods have different names of course: GetMediaPlayerInterface () and GetMediaPlayerInterface2 (). the DLL implements both interfaces and inside it can use a lot of shared code between the two implementations and I have to maintain only one project for that.

I have a piece of code that can detect if a DLL exports some functions without actually loading the DLL. this piece of code is useful for your reasons: If you have to load a DLL to find out whether it is your plugin or not, then during your LoadLibrary () call the DllMain () of the library may be executed inside your process even if it is not your plugin. another advantage is faster execution that may count a lot if your app has lots of plugins. here is another tip that contains the code that detects whether a DLL exports your desired function (s) without loading it with LoadLibrary (): checking for exported symbols/functions in a DLL without loading it.

Advices to Design the Interface of DLLs Written in C
I myself never use C in user applications and I'm generally against the use of C when development isn' t very low/system-level and a reasonably good C ++ compiler is available on the specified platform. if you have the sources of a DLL that is written in C, then you can do the following:

If you want to keep the C interface, then do the following:
Declare a struct in the public header of your DLL. The members of this struct will be function pointers. This struct will be the equivalent of the C ++ interface class.
Somewhere in your DLL, define a (static) instance of this struct and fill it with pointers to your function implementations.
Export a single function from the DLL that returns a const pointer to the previusly defined (static) struct instance.
Alternatively you can create a C ++ "wrapper" interface from which you call the original C functions.

========================================================== ========================
My suggestions:
1 division has to be done, otherwise try not to use DLL

2 It is best to declare a member function of a class in DLL as pure virtual methods

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.