编写DLL(转)

一个DLL就是一段可以在程序运行而不是编译期间链接的代码。它可以被多个客户程序共享而不需要分别复制给每个客户程序。

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由三部分组成:

  1. 头文件(header file)
  2. 库文件(import library file)
  3. Dll文件

从DLL开发者的角度来说,和完整的Symbian工程一样,编写一个DLL需要下列文件:

  1. 项目定义文件(MMP file),组件定义文件(bld.inf)
  2. 一个头文件指定接口
  3. 包含实现的源文件

头文件

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实现文件要注意的两点:

  1. 实现E32Dll() 函数
  2. 要导出的函数前加宏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)

  1. 定义TARGETTYPE 为dll
  2. UID2 的值固定为0x1000008d
  3. 在开发阶段通过使用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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值