加载PE文件的资源 [英] When Resources of a PE file are loaded

查看:109
本文介绍了加载PE文件的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中使用PE文件中包含的资源(例如二进制资源)时。我们必须先

When using a resource included in a PE file (for example a binary resource) in C++ . we have to first call

1)FindResource,然后

1 )FindResource and then

2)LoadResource


访问资源。

2 )LoadResource
to access the resource .

关于函数名称 LoadResource的准确性,我想知道 Windows Loader是否加载加载其他部分(例如代码或数据段)时,应用程序的所有资源是否在内存中,还是仅在需要时才延迟加载?


是否可以在使用完这些资源后释放这些资源以释放分配的内存?

Being accurate about the function name "LoadResource" i wonder if the "Windows Loader" does load all resource of an application in memory just when loading other parts (like code or data section) or they are delay loaded only when we need them ?

If so can we unload these resources after we have used them in order to free allocated memory?

推荐答案

这些功能很旧,可以追溯到尚不支持虚拟内存的Windows版本。回到过去,他们实际上实际上是将资源加载到RAM中。

These functions are old, they date back to Windows versions that did not yet support virtual memory. Back in the olden days they would actually physically load a resource into RAM.

这些日子已经一去不复返了,OS加载程序创建了一个内存映射文件来映射可执行文件。进入记忆。当程序取消引用指针时,文件中的所有内容(代码和资源)都将映射到RAM中。您只需为使用的商品付费。

Those days are long gone, the OS loader creates a memory-mapped file to map the executable file into memory. And anything from the file (code and resources) are only mapped into RAM when the program dereferences a pointer. You only pay for what you use.

所以LoadResource()所做的很少,它只是返回一个伪装成HGLOBAL句柄的指针。 LockResource()并没有做任何有趣的事情,它只是将HGLOBAL强制转换回指针。实际开始使用它时,您将遇到页面错误,内核将读取文件,并将其加载到RAM中。 UnlockResource()和FreeResource()什么也不做。如果操作系统需要RAM用于其他进程,则可以取消映射该资源的RAM。由于文件支持内存,因此无需保留任何内容,因此可以简单地丢弃该页面。如果需要再次使用该资源,则在必要时返回页面。

So LoadResource() does very little, it simply returns a pointer, disguised as a HGLOBAL handle. LockResource() does nothing interesting, it simply casts the HGLOBAL back to a pointer. When you actually start using it then you'll trip a page fault and the kernel reads the file, loading it into RAM. UnlockResource() and FreeResource() do nothing. If the OS needs RAM for another process then it can unmap the RAM for the resource. Nothing needs to be preserved since the memory is backed by the file, the page can simply be discarded. Paged back in when necessary if you use the resource again.

这篇关于加载PE文件的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆