How to define the interface to a polymorphic interface DLL

Explains how to use a polymorphic interface DLL.

The interface is defined by an abstract API which can be implemented by a DLL. The following code fragment defines the API in terms of a pure virtual abstract C++ class; this is an example class called CMessenger.

class CMessenger : public CBase
    {
public:
    virtual void ConstructL(CConsoleBase* aConsole, const TDesC& aName)=0;
    virtual void ShowMessage()=0;
private:
    CConsoleBase* iConsole;
    HBufC*        iName;
    };

// The UID for Messenger DLLs.
// The client imposes this on DLLs which satisfy the protocol.

const TInt KMessengerUidValue=0x10004262;
const TUid KMessengerUid={KMessengerUidValue};

The purpose of the example is to be able to issue a simple greeting message. Any number of DLLs can be created, each containing a different implementation of the class.

Notes:

Because the API can be implemented differently in different DLLs, note the following when declaring the class:

  • The constructor is not declared; the default C++ constructor is used, and is not exported from the DLL. Instead, a function is exported from the DLL which constructs an object of the concrete type using new (ELeave) semantics.

  • All functions are pure virtual; a DLL must provide an implementation for all such functions.

  • All functions are public since the published API is all there to be used, there is no need for private specifications.

  • It is possible to use polymorphic interface DLLs in a more advanced way that allows some of these rules to be changed.