poldevwro
poldevwro

Reputation: 13

Missing resource name in Code::Blocks and therefore FindResource doesn't work

I have a project in Code::Blocks. In my resource.rc file I have

#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define TWEETY                                  102

In my resource.h file I have

#include <windows.h>
#include <commctrl.h>
#include <richedit.h>
#include "resource.h"

TWEETY IMAGE "Tweety.png"

I build the project and afterwards open the exe file in ResEdit. This is what I see there. ResEdit

There are no resource names, only resource ids. This has a consequence (at least it's my conclusion) that I cannot find the image resource using FindResource function.

// Loads the PNG containing the splash image into a HBITMAP.
HBITMAP LoadSplashImage()
{
    HBITMAP hbmpSplash = NULL;

    // load the PNG image data into a stream
    IStream * ipImageStream = CreateStreamOnResource(MAKEINTRESOURCE(TWEETY), _T("PNG"));
    if (ipImageStream == NULL)
        goto Return;

    // load the bitmap with WIC
    IWICBitmapSource * ipBitmap = LoadBitmapFromStream(ipImageStream);
    if (ipBitmap == NULL)
        goto ReleaseStream;

    // create a HBITMAP containing the image
    hbmpSplash = CreateHBITMAP(ipBitmap);
    ipBitmap->Release();

    ReleaseStream:
       ipImageStream->Release();
    Return:
       return hbmpSplash;
}

// Creates a stream object initialized with the data from an executable resource.
IStream * CreateStreamOnResource(LPCTSTR lpName, LPCTSTR lpType)
{
    // initialize return value
    IStream * ipStream = NULL;

    // find the resource
    HRSRC hrsrc = FindResource(NULL, lpName, lpType);

    if (hrsrc == NULL)
        goto Return;

    // load the resource
    DWORD dwResourceSize = SizeofResource(NULL, hrsrc);
    HGLOBAL hglbImage = LoadResource(NULL, hrsrc);
    if (hglbImage == NULL)
        goto Return;

    // lock the resource, getting a pointer to its data
    LPVOID pvSourceResourceData = LockResource(hglbImage);
    if (pvSourceResourceData == NULL)
        goto Return;

    // allocate memory to hold the resource data
    HGLOBAL hgblResourceData = GlobalAlloc(GMEM_MOVEABLE, dwResourceSize);
    if (hgblResourceData == NULL)
        goto Return;

    // get a pointer to the allocated memory
    LPVOID pvResourceData = GlobalLock(hgblResourceData);
    if (pvResourceData == NULL)
        goto FreeData;

    // copy the data from the resource to the new memory block
    CopyMemory(pvResourceData, pvSourceResourceData, dwResourceSize);
    GlobalUnlock(hgblResourceData);

    // create a stream on the HGLOBAL containing the data
    if (SUCCEEDED(CreateStreamOnHGlobal(hgblResourceData, TRUE, &ipStream)))
        goto Return;

    FreeData:
    // couldn't create stream; free the memory

    GlobalFree(hgblResourceData);

    Return:

    return ipStream;
}

Please advise what I'm doing wrong.

Update - it is working now.

TWEETY IMAGE "Tweety.png"

IStream * ipImageStream = CreateStreamOnResource(MAKEINTRESOURCE(TWEETY), _T("IMAGE"));

HRSRC hrsrc = FindResource(NULL, lpName, lpType);

However, in ResEdit I still don't see the resource name, only resource id.ResEdit - no resource name

And this: MAKEINTRESOURCE(TWEETY);

returns "error - cannot access memory at address 0x66".

Upvotes: 1

Views: 197

Answers (1)

IInspectable
IInspectable

Reputation: 51463

Resource types are identified by ID or name. Your resource script defines a resource type with name IMAGE (that's why you see "IMAGE" in ResEdit; note the quotation marks).

You are passing a resource type with name PNG to the call to FindResource. The module doesn't have a resource type named PNG. It contains a resource type named IMAGE. When you pass _T("IMAGE") your code starts to work.

Upvotes: 1

Related Questions