/* Read http://z0b.kapsi.fi/snippets.php before using this code. Thank you. */ // Requires some OLE libraries to be linked (on MinGW, libuuid.a, libolepro32.a and libole32.a) // returns NULL if failed HBITMAP loadBitmapResource(const int id, const TCHAR *type, HINSTANCE instance) { // find the resource HRSRC res = FindResource(instance, MAKEINTRESOURCE(id), type); if (!res) return NULL; // get resource size and handle const DWORD size = SizeofResource(instance, res); HGLOBAL data = LoadResource(instance, res); if (!data) return NULL; // allocate a global memory block and copy the resource data into it HGLOBAL mem = GlobalAlloc(GMEM_MOVEABLE, size); if (!mem) return NULL; CopyMemory(GlobalLock(mem), LockResource(data), size); GlobalUnlock(mem); // convert the global memory block into an OLE stream and use // the OLE image loader to load it LPSTREAM stream; LPPICTURE picture; if (CreateStreamOnHGlobal(mem, TRUE, &stream) != S_OK) { GlobalFree(mem); return NULL; } if (OleLoadPicture(stream, 0, FALSE, IID_IPicture, (LPVOID *)&picture) != S_OK) { stream->Release(); GlobalFree(mem); return NULL; } // get handle to bitmap, then convert it HBITMAP bitmap; picture->get_Handle((OLE_HANDLE *)&bitmap); bitmap = (HBITMAP)CopyImage(bitmap, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG); // clean up GlobalFree(mem); stream->Release(); return bitmap; }