Why does GetObject fail here when lbvobject is NULL

时间:2016-08-31 17:04:43

标签: c++ winapi gdi

The GetObject() documentation says that when lpvObject is NULL, the function returns the number of bytes required. But, I am getting 0, which is the return value when it fails.

I've been searching and trying to figure out what I'm doing wrong.

I've just got a basic window setup using the WM_PAINT message:

case WM_PAINT:
{
    PAINTSTRUCT ps;
    HDC hdc;
    hdc = BeginPaint(hwnd, &ps);
    HBITMAP hbmap;
    hbmap = (HBITMAP) LoadImage(NULL, str_path, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
    BITMAP bm;
    HDC bmapmemory = CreateCompatibleDC(hdc);
    SelectBitmap(bmapmemory, hbmap);
    int a = GetObject(hbmap, sizeof(BITMAP), NULL);

    BitBlt(hdc, 0, 0, 750, 750, bmapmemory, 0, 0, SRCCOPY);
    char c[64];
    sprintf_s(c, sizeof(c), "%f\n", a);
    OutputDebugStringA(c);

    DeleteDC(bmapmemory);
    DeleteObject(bmap);

    EndPaint(hwnd,& ps);

    return 0;
}

As I said, a is always 0, or failure, for the GetObject() function call.

1 个答案:

答案 0 :(得分:2)

You're using "%f" format but passing an integer. That's undefined behavior. If you look at the return value in the debugger you'll probably find that it's not zero.