Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Who can explain the FindResource/LoadResource/LockResource?

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 ?

like image 998
wenxibo Avatar asked Sep 06 '25 03:09

wenxibo


1 Answers

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.

like image 71
Jonathan Potter Avatar answered Sep 07 '25 20:09

Jonathan Potter