0

I've been trying to get LoadBitmap() to work for some time now. It's returning error 1814, ERROR_RESOURCE_NAME_NOT_FOUND. Code below, but so far I've tried:

  • Using (HBITMAP)LoadImage() instead of LoadBitmap()
  • Using MAKEINTRESOURCE('ICON.BMP') instead of the resource idenfitier
  • Changing the filename, resource idenfitier name and resource type of the bitmap, as well as trying it with full path name or file name only
  • Using GetModuleHandle(NULL) instead of passing the instance handle to the function
  • Separating MAKEINTRESOURCE and putting it behind its own error check just to make sure the problem isn't there (it's not)
  • Checking compiler and linker for updates
  • Stepping through the debugger

I've also been through every SO question I could find about this, other sites, MSDN documentation, and tried applying what I found there, to no avail. What am I missing?

Using MinGW on Eclipse

resource.rc

#include "resource.h"

IDBMP_TREEICON BITMAP "icon.bmp"

resource.h

#ifndef __RESOURCE_H_INCLUDED__
#define __RESOURCE_H_INCLUDED__

#define IDBMP_TREEICON 101

#endif

main.cpp

#include <windows.h>
#include <stdio.h>
#include <commctrl.h>
#include <string>
#include "resource.h"

BOOL InitTreeViewImage(HINSTANCE hInstance, HWND hWnd){
    HBITMAP hbmp = NULL;
    HIMAGELIST hImgList;

    if((hImgList = ImageList_Create(10, 10, FALSE, 1, 0)) == NULL){
        MessageBox(NULL, "Failed to create ImageList!", "Eror", MB_OK);
        return false;
    }

    LPCSTR theBitmap = NULL;
    theBitmap = MAKEINTRESOURCE(IDBMP_TREEICON);
    if(theBitmap == NULL){
        //GetLastError(), output to console, messagebox - removed for brevity in this example
        return false;
    }

    hbmp = LoadBitmap(hInstance, theBitmap);
    if(hbmp == NULL){
        //GetLastError(), output to console, messagebox - removed for brevity in this example
        return false;
    }
    treeIcon = ImageList_Add(hImgList, hbmp, NULL);
    if(treeIcon == 1){
        MessageBox(NULL, "Failed to add icon to image list!", "Eror", MB_OK);
        return false;
    }
    DeleteObject(hbmp);

    TreeView_SetImageList(hWnd, hImgList, TVSIL_NORMAL);

    return true;
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 2
    Try open the .exe file with a resource browser (e.g. visual studio). This should show if the resource is being compiled into the .exe, or being missed off. – mksteve Oct 01 '15 at 06:40
  • Your suggestion led me here: http://stackoverflow.com/questions/1411040/how-to-add-an-icon-to-an-application-built-with-eclipse-galileo-c-and-mingw I tried a few variations of that, still no luck. This is the second time I've been told to switch to Visual Studio, so I guess I'd better do it. Will edit when I have some sort of result. – Darth Fisticuffs Oct 01 '15 at 07:03
  • Show the build commands and output. This sure seems like the resources didn't get linked to the module or you're using the wrong HINSTANCE. – Adrian McCarthy Oct 01 '15 at 18:42
  • I finally had a chance to try all of this in Visual Studio 2015. It builds and compiles, but the error message is now "The specified resource name cannot be found in the image file", which I take to be Visual Studio's version of g++'s error 1814, which is what I was getting before. My resource appears to be set up correctly though. – Darth Fisticuffs Oct 07 '15 at 05:05

1 Answers1

5

When LoadBitmap fails with ERROR_RESOURCE_NAME_NOT_FOUND, that means the module you specify has no resource of type RT_BITMAP with the resource ID that you supplied. It is absolutely that simple.

There are many possible failure modes:

  • You supplied the wrong module. Check that hInstance really does identify the correct module that you linked the resource to.

  • The resource that you linked has the wrong type, or the wrong ID. That seems quite unlikely given what you present in the question.

  • You failed to pass the compiled resource file to the linker, and no resource was actually linked to the executable.

As an aside, MAKEINTRESOURCE is a simple macro, and it's a mistake to expect GetLastError() to return anything related to MAKEINTRESOURCE. As a hard rule, only ever use GetLastError() if the documentation explicitly says that it is meaningful to do so.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • I've tried this with an instance handle passed from the main function, a global instance handle, and GetInstanceHandle(NULL), with no perceptible change in results. Is there anything else I should be trying? – Darth Fisticuffs Oct 07 '15 at 05:15
  • Use a resource editor program to check that the resource is linked correctly. – David Heffernan Oct 07 '15 at 06:01