Static Interface vs Polymorphic Interface
有两种重要类型的DLL,它们是:共享库Dll(也叫静态Dll)和多态Dll。
- 静态接口DLL在客户程序使用它时会自动装载进内存(一个例外是DLL在ROM),当没有如何客户程序使用它时也会自动卸载。这些DLLs具有.dll的扩展名,通常用它们实现应用程序引擎(Engine)
- 多态DLL通过RLibrary::Load()显式的装载,RLibrary::Close()卸载。多态DLL通常具有单一的入口点,它们有不同的扩展名。最重要的有.app(applications), .ldd(logical device drivers), .tsy 和 .csy(telephony and communication server modules),....
通常我们都在创建静态DLL,下面的文章将主要关注此类DLL。
一个Static Interface DLL
从使用者(不是终端用户)的角度来说,一个DLL由三部分组成:
- 头文件(header file)
- 库文件(import library file)
- Dll文件
从DLL开发者的角度来说,和完整的Symbian工程一样,编写一个DLL需要下列文件:
- 项目定义文件(MMP file),组件定义文件(bld.inf)
- 一个头文件指定接口
- 包含实现的源文件
头文件
DLL头文件和其它类的头文件类似。最大的区别是:在DLL头文件中,要在导出的函数原形前加宏IMPORT_C。示例代码如下:
// engine.h
#ifndef __ENGINE_H
#define __ENGINE_H
#include // include defines for CBase
#include
class CEngine : public CBase
{
public:
IMPORT_C static CEngine* NewL();
IMPORT_C static CEngine* NewLC();
// method for test only
IMPORT_C void TestMethod(CConsoleBase* aConsole);
CEngine();
~CEngine();
private:
void ConstructL();
// private method used in TestMethod()
void SomePrivateMethod();
};
#endif
DLL实现文件
编写DLL实现文件要注意的两点:
- 实现E32Dll() 函数
- 要导出的函数前加宏EXPORT_C
// engine.cpp
#include "engine.h"
// this function is mandatory for all DLLs
EXPORT_C TInt E32Dll(TDllReason)
{
return KErrNone;
}
EXPORT_C CEngine* CEngine::NewL()
{
CEngine* self = NewLC();
CleanupStack::Pop(self);
return self;
}
EXPORT_C CEngine* CEngine::NewLC()
{
CEngine* self = new(ELeave) CEngine();
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
EXPORT_C void CEngine::TestMethod(CConsoleBase* aConsole)
{
aConsole->Printf(_L("Hello Text "));
}
CEngine::CEngine()
{
}
CEngine::~CEngine()
{
}
void CEngine::ConstructL()
{
}
void CEngine::SomePrivateMethod()
{
}
项目定义文件(MMP file)
- 定义TARGETTYPE 为dll
- UID2 的值固定为0x1000008d
- 在开发阶段通过使用EXPORTUNFROZEN通知编译环境the DLL Interface还没有确定
TARGET engine.dll
TARGETTYPE dll
UID 0x1000008d 0x01000000
....
EXPORTUNFROZEN
Freezing the DLL interface
发布release 版本的DLL时,应该freeze the interface,so as to ensure the backward compatibility of future releases of a library。从MMP文件中删掉EXPORTUNFROZEN关键字,然后用通常的方法编译。
如果项目已经build,使用abld freeze命令freeze the interface,重新生成makefiles,这样import library 将直接从.def文件生成。
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10294527/viewspace-126356/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/10294527/viewspace-126356/