How to Save an Embedded Resource to a File

By: mykle hoban

Abstract: This article describes how to save a resource embedded in your application to a file.

Question

How dow I save an embedded resource to a file?

Answer

The following code sample illustrates how to save an embedded resource to a file.


BOOL SaveResourceToFile(char *fn, char *res)
{ HRSRC hrsrc = FindResource(HInstance,res,RT_RCDATA);
  if (hrsrc == NULL) return FALSE;
  DWORD size = SizeofResource(HInstance,hrsrc);
  HGLOBAL hglob = LoadResource(HInstance,hrsrc);
  LPVOID rdata = LockResource(hglob);
  HANDLE hFile =
    CreateFile(fn,GENERIC_WRITE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  DWORD writ; WriteFile(hFile,rdata,size,&writ,NULL);
  CloseHandle(hFile);
  return TRUE;
}


Server Response from: ETNASC04