error using LoadBitmap using MAKEINTRESOURCE

-1

Im using Visual Studio 2012 using Visual C++, I have been trying to load a bitmap from my resources Like this:

HRESULT DemoApp::CreateDIBFromResource(HWND hWnd, HINSTANCE Instance, LPCWSTR ImageID)
{
    HRESULT hr = S_OK;

    HBITMAP bitmap = (HBITMAP)LoadBitmap(GetModuleHandle(0), MAKEINTRESOURCE(ImageID));

    return hr;
}

and calling the function like this

if(SUCCEEDED(CreateDIBFromResource(hWnd, NULL, MAKEINTRESOURCE(IDB_BACKGROUND))))
{
    InvalidateRect(hWnd, NULL, TRUE);
}

It wasn't loading anything so I put a breakpoint at

return hr;

when I hover the cursor over ImageID it says

ImageID = 0x00000065 Error reading characters of string.

I cant figure out the problem. I have Unicode enabled and I don't know if that has anything to do with it.

visual-c++
asked on Stack Overflow Jun 14, 2013 by William Lovelace • edited Jun 14, 2013 by Vivek Jain

1 Answer

1

MAKEINTRESOURCE is a macro that transforms a integer to a string. You use it correctly when you call CreateDIBFromResource, but then you apply it again on the result, which makes no sense. You should use the ImageID parameter directly. The same applies to the Instance parameter.

HRESULT DemoApp::CreateDIBFromResource(HWND hWnd, HINSTANCE Instance, LPCWSTR ImageID)
{
    HRESULT hr = S_OK;

    HBITMAP bitmap = (HBITMAP)LoadBitmap(Instance, ImageID);

    return hr;
}

BTW, what's the point of returning a HRESULT? Shouldn't you return the HBITMAP?

answered on Stack Overflow Jun 14, 2013 by Marius Bancila

User contributions licensed under CC BY-SA 3.0