Static Interface vs Polymorphic Interface
有两种重要类型的DLL,它们是:共享库Dll(也叫静态Dll)和多态Dll。
通常我们都在创建静态DLL,下面的文章将主要关注此类DLL。
一个Static Interface DLL
从使用者(不是终端用户)的角度来说,一个DLL由三部分组成:
从DLL开发者的角度来说,和完整的Symbian工程一样,编写一个DLL需要下列文件:
头文件
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实现文件要注意的两点:
// 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)
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博客 ” ,链接:https://blog.itpub.net/10294527/viewspace-126356/ ,如需转载,请注明出处,否则将追究法律责任。