3

I load a module(exe/dll) by LoadLibrary and get a pointer of a binary resource in it .

Microsoft note that should use three steps :

  1. use FindResource return HRSRC
  2. use LoadResource with that HRSRC and return HGLOBAL
  3. use LockResource lock the HGLOBAL to return a pointer which you want finally .

I don't understand why MS design this process so strangely ?

if you want to detect the length of resource , you must use SizeofResource with the pointer returned from first step, but cannot input the pointer returned from step2 and step3 .

If check the pointer address outputted from these steps , I got the result :

  1. All of pointer address in the address range of module with loaded by LoadLibrary .
  2. The addresses by step2 and step3 are same .

Who can explain what these functions do exactly ?

wenxibo
  • 171
  • 2
  • 6
  • Stack Overflow is for when you have a problem. You know how to use these functions. You have read the documentation and understand it fine. Follow the rules and move on. – David Heffernan Sep 03 '12 at 08:58
  • 3
    @DavidHeffernan, SO is when you have a question. A place where we can learn from each other so that we can become better at what we do. Sometimes the rules don't make sense and it helps to understand what is happening. – Adrian Dec 21 '14 at 07:24

1 Answers1

5

These functions date from the Windows 3.x days when memory was scarce, and resources were kept on disk until they were needed. FindResource finds them in the resource table of the disk file and LoadResource loads them into memory. The memory is allocated as "movable", which means the memory manager can move it around as needed to free up space to make larger contiguous chunks. The memory therefore needed to be locked using LockResource before it could be accessed.

Since Windows 2000/XP a lot of these steps are redundant but the functions remain for backwards compatibility.

Jonathan Potter
  • 36,172
  • 4
  • 64
  • 79
  • 1
    Just to necro add to this for future travelers, this is exactly what c# and most other systems still do as well whether you see it or not. In this case you are just seeing a lower level unwrapped API as one of the available APIs. This is probably one of the most common idioms ever. C# has "fixed" which pins down the memory so it can actually be accessed, which can be used after load, which can be used after getting a file handle through opening a stream. Is just the way it is. – Beeeaaar May 02 '17 at 18:43