GetObject returns strange size

This is 64-bit Microsoft C++ Windows native code:

 hIcon = LoadBitmap( hInstance, "InternalIconBase" );
 BITMAP bmp;
 int goret = GetObject(hIcon, sizeof(BITMAP), &bmp);

This fails, goret==0. If I pass in null for the third argument, then GetObject tells you how many bytes it needs. It returns 32. But sizeof(BITMAP) is 28, according to the debugger and the definition in wingdi.h:

typedef struct tagBITMAP
  {
    LONG        bmType;
    LONG        bmWidth;
    LONG        bmHeight;
    LONG        bmWidthBytes;
    WORD        bmPlanes;
    WORD        bmBitsPixel;
    LPVOID      bmBits;
  } BITMAP, *PBITMAP,  *NPBITMAP,  *LPBITMAP;

Its size surely is 28 bytes (4*4 bytes, 2*2 bytes, 8 bytes)! We are clearly definitely loading a BITMAP, and the documentation on this page http://msdn.microsoft.com/en-us/library/dd144904.aspx says that for such a handle it will return either the size of BITMAP (which is 28) or DIBSECTION (which is 92). And none of the other return types on the documentation page has a size anywhere near 32.

And LoadBitmap and GetObject are just the plain GDI methods (no overloading or anything).

Our code does make use of /Zp1 packing stuff on the byte boundary, which could explain why it works on 32-bit code but not 64-bit code. Though usually Microsoft header files push the packing setting and restore it again? Maybe that is missing from the GDI header files somehow?


确实,因为如果我这样做似乎工作得很好:

//wingdi.h does not set the proper pack setting, causing structures to have the wrong size due to our /Zp1!
//32-bit: sizeof(BITMAP)==24
//32-bit: sizeof(BITMAP)==28 but GetObject says it wants 32, so:
    #ifdef _WIN64
       #pragma pack(push,8)
    #endif
#   include <windows.h>
    #ifdef _WIN64
       #pragma pack(pop)
    #endif
链接地址: http://www.djcxy.com/p/84140.html

上一篇: 如何在WinRT中将一组画布转换为动画GIF?

下一篇: GetObject返回奇怪的大小