0

The below code executes, but it only extracts an empty bitmap file. Any ideas as to what is wrong with it?

void Extract(WORD wResId , LPSTR lpszOutputPath)
{ //example: Extract(IDB_BITMAP1, "Redrose.bmp");
    HRSRC hrsrc = FindResource(NULL, MAKEINTRESOURCE(wResId) , RT_BITMAP);
    HGLOBAL hLoaded = LoadResource( NULL,hrsrc);
    LPVOID lpLock =  LockResource( hLoaded);
    DWORD dwSize = SizeofResource(NULL, hrsrc);
    HANDLE hFile = CreateFile  (lpszOutputPath,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    DWORD dwByteWritten;
    WriteFile(hFile, lpLock , dwSize , &dwByteWritten , NULL);
    CloseHandle(hFile);
    FreeResource(hLoaded);
}
eric
  • 117
  • 3
  • 10
  • 1
    Did you check the values of stuff like `dwSize` to make sure they are what you expected? – Jim Buck Jul 09 '12 at 02:07
  • Did you try stepping through it under a debugger? What's getting returned from `SizeofResource`? Are you even finding the resource in the first place (i.e. does `FindResource` even find it)? – reuben Jul 09 '12 at 02:07

3 Answers3

4

You are asking for RT_RCDATA but I bet you didn't add your bitmap via a RCDATA statement. You probably added it via a BITMAP statement, which makes it RT_BITMAP.

In the future, please state which step failed rather than making people guess.

Raymond Chen
  • 44,448
  • 11
  • 96
  • 135
  • thank you for pointing out my misuse of RT_RCDATA. I did not know which step failed, if I did I would not be here asking my question. I am using RT_BITMAP and I no longer have an empty bmp but now I have an invalid bmp. – eric Jul 09 '12 at 17:01
  • Certainly you can use the debugger to determine which step failed, right? (See the questions from Jim Buck and reuben, for example.) And the result is not a BMP file because bitmap resources are not stored in the form of a BMP file. – Raymond Chen Jul 09 '12 at 17:50
  • The debugger is telling me nothing when I step through it. If the result is not a BMP file then what is the result and what are they stored as? – eric Jul 09 '12 at 18:01
  • The debugger doesn't show function return values and local variables? You might want to get a better debugger. As for your other question, it's indirectly answered [here](http://stackoverflow.com/questions/1134050/is-there-a-way-to-preserve-the-bitmapfileheader-when-loading-a-bitmap-as-a-windo) and directly [here](http://blogs.msdn.com/b/oldnewthing/archive/2009/12/11/9935462.aspx). – Raymond Chen Jul 09 '12 at 18:55
0

The problem is in passing NULL as your HINSTANCE parameter to FindResource, LoadResource, and SizeOfResource.

If you have not already saved your HINSTANCE during startup (from WinMain or DllMain) you can get it using:

MFC:

HINSTANCE hInstance = AfxGetInstanceHandle();

Else:

HINSTANCE hInstance = (HINSTANCE)GetModuleHandle(NULL);
Deanna
  • 23,876
  • 7
  • 71
  • 156
Bruce Ikin
  • 885
  • 4
  • 6
  • ...but according to MSDN, it's valid to pass in `NULL` if the resource you're looking for is in "the module used to create the current process"... – reuben Jul 09 '12 at 02:40
  • I tried changing NULL to hInstance noted above and still get an empty bitmap. – eric Jul 09 '12 at 07:48
0

Insert your raw file as a custom data. Give this custom data a text name, example "MyType", then:

HRSRC hrsrc = FindResource(NULL, MAKEINTRESOURCE(wResId) , _T("MyType"));
Deanna
  • 23,876
  • 7
  • 71
  • 156
mfc
  • 1
  • Compiler gives error C3861: '_T': identifier not found. Do I need to add an include? – eric Jul 09 '12 at 11:30
  • OK I added tchar.h include and this fixed the error but still I am getting an empty bmp file. – eric Jul 09 '12 at 16:09