Prerequisites
- Knowledge of C++ and COM.
- Knowledge of Symbian Programming idioms
Introduction
Writing a polymorphic DLL involves the following steps:
- Design and write interfaces.
- Implement the interface.
Lets explore these in more detail:
Design and write interface consisting of method declarations.
An interface is essentially an abstract class. An example would clarify it:
class ICalculator {
public:
virtual TInt Sum(TInt, TInt) = 0;
};
Above snippet declares a class ICalculator
with one method Sum
which would take 2 TInt
parameters and would return a TInt
value. Take a look at the name given to the class. This is done to follow a convention i.e. an interface name should start with an 'I'. Now if one wishes to implement interface ICalculator
, he/she has to implement the method Sum
.
COM programmers would be wondering that how it can be an interface without a UUID or something like that. Yes! in Symbian too, an interface requires a UUID. While writing an interface this is how UUID is given to it.
static const TUid KCalculator_DLLUID = {0x100039CE};
static const TUid KCalculator_DLLUID_Ver1 = {0x0352D96B};
class ICalculator {
public:
virtual TInt Sum(TInt, TInt) = 0;
};
Your polymorphic interface is ready. You have to write it in a .h file so that it can be implemented in other .cpp files.
Implement the interface.
Writing a polymorphic DLL is half done when interface declaration is done. Now one would require to implement it. Implementing an interface is as simple as inheriting abstract classes. You have to inherit from an interface and implement pure virtual methods of the interface.
Again an example would help us understand this:
class CImpCalculator: public ICalculator {
public:
EXPORT_C CImpCalculator* NewImpClass();
TInt Sum(TInt num1,TInt num2);
};
GLDEF_C TInt E32Dll(TDllReason) {
return KErrNone;
}
EXPORT_C CImpCalculator* CImpCalculator::NewImpClass() {
return new (ELeave) CImpCalculator;
}
TInt CImpCalculator::Sum(TInt num1,TInt num2) {
return num1 + num2;
}
The example above does something more than just implementing the interface. The class CImpCalculator
has a method of its own, NewImpClass
. This method has a qualifier EXPORT_C
. Clearly this is the method which is exposed from the implementation class. In Symbian it is not required to export or expose all the methods from an interface. Only method which needs to be exposed is which returns the object of the interface (or should I say the implementor class?). Rest of the methods can be accessed from function table. The NewImpClass
method returns a pointer to the newly created object.
E32Dll
method has to be written in every Polymorohic DLL as it is required by the Symbian run time. You would get linking errors in case you forget to write it. This method is responsible to allocate thread local store for the DLL instance.
Now you are ready to compile and build your own DLL. Build it and start using it.
Lastly these DLLs are called polymorphic because one can write many implementations of the same interface. Further all the Symbian GUI applications are Polymorphic DLLs. Check the GUI SDK examples, you will find E32Dll
method implemented.
That's all you would require to write a DLL on Symbian!! Note that I have tested the code on NOKIA Series 60 emulator.