Win32中加载和销毁资源

本文介绍了在Win32中管理资源的四个步骤:寻找资源、加载资源、锁定资源及释放资源。并详细解释了如何使用FindResource、LoadResource、LockResource和FreeResource等函数。

在Win32中,使用一个资源实际上分成四个步骤,在当前模块寻找资源,加载资源以及锁定资源,使用完毕后释放资源。

1. 寻找资源

HRSRC FindResource(
HMODULE hModule,
LPCTSTR lpName,
LPCTSTR lpType
);
hModule[in] :Handle to the module whose executable file contains the resource. A value of NULL specifies the module handle associated with the image file that the operating system used to create the current process.
lpName[in]: Specifies the name of the resource. For more information, see the Remarks section below.
lpType[in]: Specifies the resource type. For more information, see the Remarks section below. For standard resource types, see Resource Types.

该函数确定具有指定类型和指定名称的资源在指定模块中的位置。
如果成功,返回值是指定资源信息块的句柄;若要获取该资源的句柄,调用LoadResource 函数,并将获取的信息块的句柄传入其中。
该函数内部会使用IS_INTRESOURCE(x)宏来测试x (此时x=lpName或者lpType),当测试为真时,x为资源名称或类型的ID值;否则,这些参数是字符串指针。当字符串的首字符为’#'号时,剩余的字符代表十进制的整数ID值。比如,字符串"#258"代表ID值258.
为了减小资源所需内存数量,程序应该通过ID值而不是名称引用资源。
程序可以使用FindResource 找到任何类型资源,但是该函数应当仅在程序通过调用 LockResource 获取二进制的资源数据前使用。
需要立即使用资源时,程序应该使用下列资源函数一次性完成寻找和加载资源的操作。

FunctionAction
FormatMessageLoads and formats a message-table entry.
LoadAcceleratorsLoads an accelerator table.
LoadBitmapLoads a bitmap resource.
LoadCursorLoads a cursor resource.
LoadIconLoads an icon resource.
LoadMenuLoads a menu resource.
LoadStringLoads a string-table entry.

例如,程序应该使用LoadIcon加载一个图标。然而,程序需要将一张图标资源复制到另一个程序当中时,应当使用 FindResource 和 LoadResource 。

2. 加载资源

HGLOBAL LoadResource(
HMODULE hModule,
HRSRC hResInfo
);
hModule[in]: Handle to the module whose executable file contains the resource. If hModule is NULL, the system loads the resource from the module that was used to create the current process.
hResInfo[in] :Handle to the resource to be loaded. This handle is returned by the FindResource or FindResourceEx function.

加载资源到全局内存中。
如果该函数成功,返回与资源关联数据的句柄。当需要获取资源数据的指针时,调用LockResource函数。
当需要立即使用一个资源时,程序应该使用下列函数一次性的找到和加载资源。

FunctionActionTo remove resource
FormatMessageLoads and formats a message-table entryNo action needed
LoadAcceleratorsLoads an accelerator tableDestroyAcceleratorTable
LoadBitmapLoads a bitmap resourceDeleteObject
LoadCursorLoads a cursor resourceDestroyCursor
LoadIconLoads an icon resourceDestroyIcon
LoadMenuLoads a menu resourceDestroyMenu
LoadStringLoads a string resourceNo action needed

比如,程序使用 LoadIcon 函数加载一个图标,在使用完毕后调用DestroyIcon销毁图标。
实际上,LoadIcon、LoadCursor等函数就是调用了FindResource和LoadResource完成了资源加载。
系统在创建这些资源的进程结束时自动删除这些资源;不过,调用合适的函数可以节约内存、降低进程工作集的大小。

3. 锁定资源

在内存中锁定资源,返回指向资源数据的首字节。

LPVOID LockResource( HGLOBAL hResData);

如果加载的资源锁定了,该函数返回资源首字节的指针;否则,NULL.
只有在包含该资源的模块尚未卸载时,该函数才返回有效指针。不需要解锁资源,因为系统在进程结束时会自动删除这些资源。

4. 释放资源

在Win32编程中,FreeResource 用来释放资源。

BOOL FreeResource( HGLOBAL hglbResource);
hglbResource[in]: Handle of the resource. It is assumed that hglbResource was created by LoadResource.

该函数可使加载资源的引用数减1。当一个资源的引用数减为 0的时候,资源的内存被释放。
当程序调用LoadResource函数时,会使资源的引用数加1. FreeResource函数已经废弃了,仅仅是为了兼容16位的Windows。对32位的Windows程序,没有必要释放LoadResource加载的资源。
对用其他函数加载的资源,比如LoadIcon, LoadCursor, LoadBitmap, LoadImage, LoadMenu, or LoadAccelerators,FreeResource 已经被下列函数取代。

Resource typeFreeResource replacement
AcceleratorDestroyAcceleratorTable
BitmapDeleteObject
CursorDestroyCursor
IconDestroyIcon
MenuDestroyMenu

当创建这些资源的进程终止时,系统将会自动删除这些资源。然后,调用恰当的释放函数会节约内存。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Santiago

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值