Another in a sporadic series on the format of Win32 resources. Here’s a question from a customer:
I’m noticing some strange behavior: When I call
LoadResource
thenLockResource
on an embedded bitmap, the data being returned byLockResource
is not a properly formatted bitmap. The data is missing theBITMAPFILEHEADER
, but the rest of the file is there.SizeOfResource
also states that the bitmap resource is 14 bytes smaller than it actually is. 14 bytes happens to be equal tosizeof(BITMAPFILEHEADER)
. However, if I load the bitmap directly usingLoadBitmap
, everything works fine. If I look at the resource using Visual Studio, the Bitmap displays correctly and the binary data correctly includes theBITMAPFILEHEADER
.Anyone have any ideas as to why
LoadResource
is not correctly returning theBITMAPFILEHEADER
?
Amusingly, a change to the word order changes the question to its own answer: LoadResource
is correctly not returning the BITMAPFILEHEADER
.
In other words, LoadResource
is not stripping off the BITMAPFILEHEADER
: rc.exe
is.
The format of bitmap resources are pretty simple. They are just a bitmap file with the BITMAPFILEHEADER
stripped off. Because it’s just the file header, the thing tacked onto the front when saved as a file. It’s not part of the bitmap itself. For example, if you are using a BITMAPINFOHEADER
-formatted bitmap, then the resource format is a BITMAPINFOHEADER
followed by the pixels.
I can’t explain why Visual Studio is showing you a BITMAPFILEHEADER
that doesn’t exist. They’re probably trying to reduce confusion for people who don’t know the format of bitmap resources and wonder why the binary data doesn’t match their .bmp
file. Of course, in so doing, they end up creating confusion for people who do know the format of bitmap resources, or—as happened here—people who don’t know the format of bitmap resources and think that the LoadResource
function is messing with their bitmaps.
(For the record, the LoadResource
function doesn’t mess with bitmaps, icons, menus, or whatever. It just returns the raw binary data of a Win32 resource. It doesn’t know the internal format of those resources any more than the file system knows the internal format of a Quicken data file or a shortcut file.)
0 comments