LoadBitmap function call is constantly failing for some reason

This is a discussion on LoadBitmap function call is constantly failing for some reason within the Windows Programming forums, part of the Platform Specific Boards category; This is my code so far for trying to load a few bitmaps into my program, although at the moment, ...

  1. #1
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195

    LoadBitmap function call is constantly failing for some reason

    This is my code so far for trying to load a few bitmaps into my program, although at the moment, it's not going well, lol. For some reason, the function calls to LoadBitmap are constantly failing, and I can't find where the problem is.

    In my main.cpp file:
    Code:
    case WM_CREATE:
                    // Create handles for the bitmaps. These will be used in the Button replace function
                    hbmCross = LoadBitmap(hThisInstance, "ID_BMP_CROSS");
                    hbmNaught = LoadBitmap(hThisInstance, "ID_BMP_NAUGHT");
                    if(!hbmCross || !hbmNaught) {
                              sprintf(Info.buffer, "Could not load resources from file");
                              MessageBox(hwnd, Info.buffer, "Error Loading Resources", MB_ICONERROR);
                              SendMessage(hwnd, WM_DESTROY, 0, 0);
                              return -1;
                              }
    The important line is the LoadBitmap function calls, since these are the one(s) which are always failing.

    included in my images.rc file:
    Code:
    /* images.rc which contains the IDs for the image files used */
    ID_BMP_CROSS BITMAP "cross.bmp"
    ID_BMP_NAUGHT BITMAP "naught.bmp"
    I've added my images.rc file to the project. At first, I thought that the problem was different names used in the program, from the names of the files in the directory, but that's still not working. I'm out of ideas as to what the problem might be.

    [edit]I've also tried using MAKEINTRESOURCE, but that didn't work either[/edit]
    Last edited by Swarvy; 09-10-2008 at 06:30 AM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are loading a resource rather than a file, your second argument to LoadBitmap should be something like:
    Code:
    MAKEINTRESOURCE(ID_BMP_CROSS)
    Right now you are trying to load a bitmap from a file called "ID_BMP_CROSS", and my guess is that this is not a valid bitmap file in your system.

    You could, potentially use "cross.bmp" instead, but then you wouldn't need to have them in the resource file - so my guess is that you really want to use the MAKEINTRESOURCE variant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Neither method of loading the bitmaps into my application seem to work, so I don't really know what to do about it.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what does "GetLastError" say about it? What is your current code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    213
    Have you tried importing the bmps into the main .rc file, then use the MAKEINTRESOURCE method.

  6. #6
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Is that not the same as putting in the resource file:
    Code:
    hbmCross BITMAP "cross.bmp"
    hbmNaught BITMAP "naught.bmp"
    If so, then yeah, i've done that. lol. I've also got defines in the main header file for them, but that hasn't worked so far.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Swarvy View Post
    Is that not the same as putting in the resource file:
    Code:
    hbmCross BITMAP "cross.bmp"
    hbmNaught BITMAP "naught.bmp"
    If so, then yeah, i've done that. lol. I've also got defines in the main header file for them, but that hasn't worked so far.
    So, again, what does "GetLastError" return?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by matsp View Post
    So, again, what does "GetLastError" return?

    --
    Mats
    The error gives the error: ERROR_RESOURCE_NAME_NOT_FOUND, so I the program can't seem to find the files. That's what made me initially assume that I had mistyped the names of the bitmaps, but from what I can tell, I haven't. The names are the same.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    I hope they're in the proper directory?

  10. #10
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by tabstop View Post
    I hope they're in the proper directory?
    Lol. That question insults me Yes, they are in the proper directory.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, it's one of those "is your printer on"-type questions that works way more often than it should. (I'm tempted to say you should copy cross.bmp into every directory on your computer just to be sure, but I won't.) Can you find your other resources, or is it LoadBitmap specific?

  12. #12
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    Quote Originally Posted by tabstop View Post
    Well, it's one of those "is your printer on"-type questions that works way more often than it should. (I'm tempted to say you should copy cross.bmp into every directory on your computer just to be sure, but I won't.) Can you find your other resources, or is it LoadBitmap specific?
    I also have a menu in my program, but that works fine. Both the bitmaps and the menu are defined in the same resource file, so I don't think that's the problem. Would it cause a problem if both are defined in the same resource file? I can't see how it should, but I'm no expert.

    [edit] I've attached all the program files to do with the program (some are a little unnecessary, but I've included them anyway). The program is a little rough round the edges at the moment, but I was planning on cleaning it up when I had something which worked as I wanted it to. [/edit]

    Code from resource.rc (cant upload it):
    Code:
    /* This is the main resource file which is used in opensource tictactoe */
    #include "resource.h"
    
    /* images.rc which contains the IDs for the image files used */
    ID_BMP_CROSS BITMAP "cross.bmp"
    ID_BMP_NAUGHT BITMAP "naught.bmp"
    
    IDD_MENU MENU
    BEGIN
        POPUP "File"
        BEGIN
            MENUITEM "New Game", ID_FILE_NEW
            MENUITEM SEPARATOR
            MENUITEM "Exit", ID_FILE_EXIT
        END
        POPUP "About"
        BEGIN
             MENUITEM "About Opensource TicTacToe", ID_ABOUT_ABOUT
        END
    END
    The part of interest is the WM_CREATE case Callback prodecure. It is there that the LoadBitmap() functions are called.
    Attached Files Attached Files
    Last edited by Swarvy; 09-10-2008 at 10:01 AM.

  13. #13
    Registered User
    Join Date
    Sep 2008
    Location
    Near Paris
    Posts
    6
    Quote Originally Posted by Swarvy View Post
    The part of interest is the WM_CREATE case Callback prodecure. It is there that the LoadBitmap() functions are called.
    I have "successfully" reproduced your problem, and fixed it:

    1) change the name of the global HINSTANCE variable. (eg: g_hThisInstance)
    2) add g_hThisInstance = hThisInstance; to your WinMain Function.
    3) fix the name used in Information::GameInit
    4) use hbmCross = LoadBitmap(g_hThisInstance, MAKEINTRESOURCE(ID_BMP_CROSS));

  14. #14
    Registered User Swarvy's Avatar
    Join Date
    Apr 2008
    Location
    United Kingdom
    Posts
    195
    I don't understand why that works, and why it doesn't work with just hThisInstance. Why is that?

    Thank you very much. I've been trying to get that to work for ages, with no success.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Location
    Near Paris
    Posts
    6
    Quote Originally Posted by Swarvy View Post
    I don't understand why that works, and why it doesn't work with just hThisInstance. Why is that?
    Your WinMain function has a hThisInstance parameter, but choosing the same name won't magically update the global variable. You have to choose two different names, and explicitly store the handle in the global variable.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21