6

I use this code to load embedded resource (bitmap image)

HRSRC hResInfo = ::FindResource(hInstance, MAKEINTRESOURCE(resourceId), RT_BITMAP);
HGLOBAL hRes = ::LoadResource(hInstance, hResInfo);
LPVOID memRes = ::LockResource(hRes);
DWORD sizeRes = ::SizeofResource(hInstance, hResInfo);

It works fine.

How I can to save it to the file C:\image.bmp?

This code creates only empty file:

HANDLE hFile = ::CreateFile(L"C:\\image.bmp", GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwWritten = 0;
::WriteFile(hFile, memRes, sizeRes, &dwWritten, NULL);
::CloseHandle(hFile);



SOLUTION:
Create new type "BMP" in resources and place image
HRSRC hResInfo = FindResource(hInstance, MAKEINTRESOURCE(resourceId), _T("BMP"));

2
  • You could use the CImage class or GDI+ to get a bitmap encoder. The more practical problem is that programs can't write files to the C: root folder without UAC elevation. This is really the kind of job for an installer. Commented Apr 30, 2012 at 13:56
  • My program runs with elevated privileges Commented Apr 30, 2012 at 13:59

1 Answer 1

3

CreateFile(), WriteFile(), and CloseHandle().

WriteFile() takes a pointer and a size so can write from the locked resource directly.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.