Problem loading bitmap from .exe

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Problem loading bitmap from .exe

Post by Tony0945 » Wed May 08, 2013 2:37 am

According to the online documentation, one can load the embedded icon in a Windows executable as a bitmap. When I try it, an internal error box pops up and after I dismess it, the program crashes with a "Send details to Microsoft?" Windows error box. Here is the code snippet:

Code: Select all

    
    bool loaded;
    wxBitmap    map(question_xpm);
    wxFileName filename(IconFileName, wxPATH_NATIVE);
    wxString ext;
    wxBitmapType type;


    ext = filename.GetExt();
    ext = ext.Upper();
    if (ext == wxT("PNG"))
        type = wxBITMAP_TYPE_PNG;
    else if (ext == wxT("BMP"))
        type = wxBITMAP_TYPE_BMP;
    else if (ext == wxT("EXE"))
        type = wxBITMAP_TYPE_BMP_RESOURCE;
    else if (ext == wxT("GIF"))
        type = wxBITMAP_TYPE_GIF;
    else if (ext == wxT("XBM"))
        type = wxBITMAP_TYPE_XBM;
    else if (ext == wxT("XPM"))
        type = wxBITMAP_TYPE_XPM;
    else    
        type = wxBITMAP_TYPE_PNG;

    loaded = map.LoadFile(IconFileName,type);
The purpose of the "loaded" flag is to check and avoid further processing if the file icon wasn't loaded, but it crashes inside LoadFile (I put a breakpoint after this code in the debugger to verify). The debugger also verifies that the EXE extension is being properly detected.

Is the EXE extension supported? or not? More importantly, how can I avoid getting a crash when there is a problem in the file whatever type it is?

Windows XP sp3 32-bit, wxWidgets ver 2.9.3.1, MingW gcc compiler 4.6.1 , code built with wxDEV-c++ GUI 7.4.2.569

User avatar
doublemax
Moderator
Moderator
Posts: 14519
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Problem loading bitmap from .exe

Post by doublemax » Wed May 08, 2013 6:51 am

You can't load a bitmap from a resource this way.

You can:
a) Load an icon or bitmap from the resource of the current application. That's what wxBITMAP_TYPE_BMP_RESOURCE and wxBITMAP_TYPE_ICO_RESOURCE are for. And you have to pass the name of the resource instead of the filename to the Load() method.

b) You can load the icon of any .exe like this:

Code: Select all

wxIcon icon( wxT("c:\\someexe"), wxBITMAP_TYPE_ICO );
wxIcon icon( wxT("c:\\someexe;1"), wxBITMAP_TYPE_ICO );
// add ";" + number to control the index of the icon you want to load
Passing wrong parameters shouldn't make the code crash though. Were you using a debug build? I suspect you just got an assert message.
Use the source, Luke!

Tony0945
Experienced Solver
Experienced Solver
Posts: 96
Joined: Wed Oct 21, 2009 4:02 pm

Re: Problem loading bitmap from .exe

Post by Tony0945 » Thu May 09, 2013 2:38 am

I've modified the code to load a wxIcon for an exe then do the CopyFromIcon method. It works like a charm. Thank you very much.

Yes, first was the debug Assert, then the crash. I like the Assert, though because it gives details of what was wrong i.e. no such file, file of wrong type, et cetera.

Post Reply