Finding and Loading Resources

Before using a resource, an application must load it into memory. The FindResource and FindResourceEx functions find a resource in a module and return a handle to the binary resource data. FindResource locates a resource by type and name. FindResourceEx locates the resource by type, name, and language. Information about FindResource in this topic also applies to FindResourceEx.

The LoadResource function uses the resource handle returned by FindResource to load the resource into memory. After an application loads a resource by using LoadResource, the system will unload the associated memory only when all references to its module are freed through FreeLibrary. Applications which need to repeatedly access the same or many resources within a particular module may incur performance penalties due to the memory mapping taking place in repeated LoadLibrary and FreeLibrary calls. Applications should store a single module handle until resources are no longer needed, and then call FreeLibrary. After a module is unloaded from memory, resource handles become invalid.

An application can use FindResource and LoadResource to find and load any type of resource, but these functions should be used only in one of these situations:

  • When the application cannot access the resource by using an existing resource-specific function.
  • When the application must access the resource as binary data for subsequent function calls.

Whenever possible, an application should instead use one of the following resource-specific functions to find and load resources in one call:

Function Action To remove resource
FormatMessage Loads and formats a message-table entry. No action needed.
LoadAccelerators Loads an accelerator table. DestroyAcceleratorTable
LoadBitmap Loads a bitmap resource. DeleteObject
LoadCursor Loads a cursor resource. DestroyCursor
LoadIcon Loads an icon resource. DestroyIcon
LoadImage Loads an icon, cursor, or bitmap. DestroyIcon, DestroyCursor, DeleteObject
LoadMenu Loads a menu resource. DestroyMenu
LoadString Loads a string-table entry. No action needed.

 

Note the release functions in the table above. Before terminating, an application should release the memory occupied by accelerator tables, bitmaps, cursors, icons, and menus by using the appropriate functions.

Memory associated with resources loaded through FindResource and LoadResource will be released once the module has been unloaded by a call to FreeLibrary. Any resources which remain unloaded at application termination will be automatically released by the system.