How to Ensure My Bitmap Can Load from Resource

JWG 146 Reputation points
2023-05-21T15:16:45.13+00:00

Locating a resource file fails. I don't know the reason.

Is this correct?

project.rc

// Bitmap

IN_BITMAP BITMAP DISCARDABLE "input.bmp"

resource.h

#define IN_BITMAP 201

project.cpp

PCWSTR inputBitmapName = L"IN_BITMAP";
PCWSTR inputBitmapType = L"BITMAP";
PCWSTR inputBitmapFile = L"input.bmp";

// Loading Resource Bitmap
	/* Locate the resource */
	imageResHandle = FindResourceW(HINST_THISCOMPONENT, resourceName, resourceType);
	hr = imageResHandle ? S_OK : E_FAIL;
	if (SUCCEEDED(hr))
	{
		//FAILED
	}

debug console :

		hr	E_FAIL	HRESULT
+		imageResHandle	0x0000000000000000 <NULL>	HRSRC__ *
+		resourceName	0x00007ff6d979acc0 L"IN_BITMAP"	const wchar_t *
+		resourceType	0x00007ff6d979acd8 L"BITMAP"	const wchar_t *

		hr	E_FAIL	HRESULT

Windows development Windows API - Win32
Windows for business Windows Client for IT Pros User experience Other
0 comments No comments
{count} votes

Accepted answer
  1. RLWA32 49,536 Reputation points
    2023-05-21T16:36:34.6633333+00:00

    There are a number of reasons why the call to FindResourceW can fail.

    The first thing to do is to call GetLastError() when the call fails. Tell us what error code was returned.

    If the resource.h file contains a #define statement for IN_BITMAP then passing L"IN_BITMAP" is an error. Instead, pass MAKEINTRESOURCE(IN_BITMAP).

    Instead of passing L"BITMAP" as the resource type pass RT_BITMAP.

    If the resource resides in an .exe file's resources you can pass NULL as the module handle. If it resides in a DLL then you need to pass the HMODULE for that DLL to the function.

    The DISCARDABLE bitmap attribute is obsolete and can be removed.

    There's not enough information provided in your question to identify the actual error.

    Finally, the code should use LoadImageW or LoadBitmapW if it will actually load a bitmap. Its not clear what purpose is served by calling FindResourceW since obtaining an HRSRC handle does not make that resource immediately available for use.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.