FindResource function

Expand
7 out of 14 rated this helpful Rate this topic

FindResource function

Determines the location of a resource with the specified type and name in the specified module.

To specify a language, use the FindResourceEx function.

Syntax

HRSRC WINAPI FindResource(
  __in_opt  HMODULE hModule,
  __in      LPCTSTR lpName,
  __in      LPCTSTR lpType
);

Parameters

hModule [in, optional]

Type: HMODULE

A handle to the module whose portable executable file or an accompanying MUI file contains the resource. If this parameter is NULL, the function searches the module used to create the current process.

lpName [in]

Type: LPCTSTR

The name of the resource. Alternately, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is the integer identifier of the resource. For more information, see the Remarks section below.

lpType [in]

Type: LPCTSTR

The resource type. Alternately, rather than a pointer, this parameter can be MAKEINTRESOURCE(ID), where ID is the integer identifier of the given resource type. For standard resource types, see Resource Types. For more information, see the Remarks section below.

Return value

Type: HRSRC

If the function succeeds, the return value is a handle to the specified resource's information block. To obtain a handle to the resource, pass this handle to the LoadResource function.

If the function fails, the return value is NULL. To get extended error information, call GetLastError.

Remarks

If IS_INTRESOURCE is TRUE for x = lpName or lpType, x specifies the integer identifier of the name or type of the given resource. Otherwise, those parameters are long pointers to null-terminated strings. If the first character of the string is a pound sign (#), the remaining characters represent a decimal number that specifies the integer identifier of the resource's name or type. For example, the string "#258" represents the integer identifier 258.

To reduce the amount of memory required for a resource, an application should refer to it by integer identifier instead of by name.

An application can use FindResource to find any type of resource, but this function should be used only if the application must access the binary resource data by making subsequent calls to LoadResource and then to LockResource.

To use a resource immediately, an application should use one of the following resource-specific functions to find the resource and convert the data into a more usable form.

FunctionAction
FormatMessage Loads and formats a message-table entry.
LoadAccelerators Loads an accelerator table.
LoadBitmap Loads a bitmap resource.
LoadCursor Loads a cursor resource.
LoadIcon Loads an icon resource.
LoadMenu Loads a menu resource.
LoadString Loads a string-table entry.

 

For example, an application can use the LoadIcon function to load an icon for display on the screen. However, the application should use FindResource and LoadResource if it is loading the icon to copy its data to another application.

String resources are stored in sections of up to 16 strings per section. The strings in each section are stored as a sequence of counted (not necessarily null-terminated) Unicode strings. The LoadString function will extract the string resource from its corresponding section.

Examples

For an example, see Updating Resources.

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winbase.h (include Windows.h)

Library

Kernel32.lib

DLL

Kernel32.dll

Unicode and ANSI names

FindResourceW (Unicode) and FindResourceA (ANSI)

See also

Reference
FindResourceEx
IS_INTRESOURCE
LoadAccelerators
LoadCursor
LoadIcon
LoadMenu
LoadResource
LoadString
LockResource
SizeofResource
Conceptual
Resources
Other Resources
FormatMessage
LoadBitmap

 

 

Send comments about this topic to Microsoft

Build date: 9/7/2011

Did you find this helpful?
(1500 characters remaining)
Community Additions ADD
C++ Class

A sample C++ class for resource manipulation can be found here:
Header:
http://www.asyncop.com/MTnPDirEnum.aspx?treeviewPath=%5bo%5d+Open-Source%5cWinModules%5cInfrastructure%5cSystemAPI.h
Source:
http://www.asyncop.com/MTnPDirEnum.aspx?treeviewPath=%5bo%5d+Open-Source%5cWinModules%5cInfrastructure%5cSystemAPI.cpp
This includes loading a library, reading and writing resources, and saving objects. The class allows an exe file to self extract required dlls from its resources.

The following is a self-installer C++ class:
http://www.asyncop.com/MTnPDirEnum.aspx?treeviewPath=%5bo%5d+Open-Source%5cWinModules%5cInfrastructure%5cAppServices.cpp

12/30/2009
Caution with PNG images

If you try to include PNG images in a resource file, be aware that PNG seems to be a standard resource type although the MSDN does NOT list it as such (neither http://msdn.microsoft.com/en-us/library/ms648009(VS.85).aspx nor http://msdn.microsoft.com/en-us/library/aa381043(VS.85).aspx list PNG as standard type). Using the resource compiler RC.exe with a .rc file containing entries like

PNGFILE PNG PNGFILE.PNG



where a new custom resource type named "PNG" is defined will result in unexpected behaviour when trying to retrieve a handle to such a resource using FindResource() and passing the type as name instead of ID. It will cause an error code 1813 (The specified resource type cannot be found in the image file.). Even more, the resource will be treated as a bitmap and not as custom binary data (RCDATA).

Having trouble loading resources by name?
Unfortunately, the official documentation does not mention that loading a resource by name does not work unless you specify the name in all capital letters. The resource itself must be named with all capital letters, and the argument to FindResource[Ex] must be in all capital letters. Otherwise, FindResource[Ex] will return NULL with error code 1814.
9/5/2007