简体   繁体   English

如何处理win32可执行资源?

[英]How are win32 executable resources handled?

I don't know why it's so difficult to find the answer to this question on Google, but I want to set something straight. 我不知道为什么在Google上找到这个问题的答案是如此困难,但我想直截了当。

Are win32 resources handled in the same way static data is where that data is kept in the RAM for the entire runtime of a process, or are they kept on disk like a regular file until loaded into memory? 是否以相同的方式处理win32资源静态数据是将数据保存在RAM中以用于整个进程运行时的位置,还是将它们保存在磁盘上,就像常规文件一样,直到加载到内存中? Functions like LoadResource / LoadString imply the latter, but I want to be absolutely sure I'm not being fooled by abstraction. 像LoadResource / LoadString这样的函数意味着后者,但我想绝对肯定我不会被抽象所欺骗。

In olden days (like Windows 3.1 and earlier), resources were copied into memory during load, and you just got a handle to them. 在过去的日子里(如Windows 3.1和更早版本),资源在加载过程中被复制到内存中,你只需要处理它们。 The memory manager could do things like move the copy around in memory to defragment space or even secretly unload the resource until you needed it again. 内存管理器可以执行诸如在内存中移动副本以对空间进行碎片整理,甚至秘密卸载资源,直到您再次需要它为止。 When you needed the resource, there was a second step to "lock" it into memory. 当您需要资源时,还有第二步将其“锁定”到内存中。 This gave you a pointer to the copy and made sure the resource manager didn't move it around until you unlocked it again. 这为您提供了指向副本的指针,并确保资源管理器在您再次解锁之前不会移动它。

In 32-bit versions of Windows, resources aren't copied. 在32位版本的Windows中,不会复制资源。 The executable (or DLL) is mapped into memory, and if you touch the resource, the virtual memory manager will make sure it's there for you. 可执行文件(或DLL)映射到内存中,如果您触摸资源,虚拟内存管理器将确保它在那里。

The APIs (FindResource, LoadResource, LockResource) reflect the old days, with handles to resources, and locking of handles, etc. But the implementations are much simpler now because the handle is just a pointer to the beginning of the resource and locking is effectively a no-op, casting the handle to a pointer type and returning it. API(FindResource,LoadResource,LockResource)反映了旧时代,资源句柄和句柄锁定等。但是现在实现起来要简单得多,因为句柄只是指向资源开头的指针而且锁定是有效的一个无操作,将句柄转换为指针类型并返回它。

You may notice that all resource APIs accept an hModule argument - this is in fact a pointer to the PE header of the module in memory and not the handle to the file on disk. 您可能会注意到所有资源API都接受hModule参数 - 这实际上是指向内存中模块的PE头的指针,而不是磁盘上文件的句柄。 Thus the resource section of the PE file ( .rsrc ) must be present in the memory space of the program for those APIs to work. 因此,PE文件的资源部分( .rsrc )必须存在于程序的存储空间中,以便这些API工作。 Of course, as with all memory-mapped files the data is probably not actually paged into the physical RAM until it's needed. 当然,与所有内存映射文件一样,在需要之前,数据可能实际上并未被分页到物理RAM中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM