如何在SYMBIAN60中编写DLL,比较经典!

Prerequisites

  • Knowledge of C++ and COM.
  • Knowledge of Symbian Programming idioms

Introduction

Writing a polymorphic DLL involves the following steps:

  1. Design and write interfaces.
  2. 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();

//function would be exposed through Function table
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.

 
微信扫码订阅
UP更新不错过~
关注
  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值