Read and create a resource file
Author: liang bo
Brianliangx@hotmail.com
In many cases, developers need to save files to a specific location when the file is saved to the resource, and the following code can help you:
// The following code is from TZ MFC.NET Primer 1.01, access www.appTemplate.com to get more code and information
// Respect the copyright, in order to revitalize China software
CTSTAPP :: CreateFileFromResource (LPCTSTR LPSZRESOURENAME, LPCTSTR LPSZTYPE, LPCTSTR LPSZTARGETNAME)
{
Assert (lpszResourceName! = NULL);
IF (lpsztargetname == _ t (")) RETURN FALSE;
// determine location of the binary resource in Resource fork
Hinstance hinst = afxfindresourceHandle (LpszResourceName, LPSZTYPE);
HRSRC HRSRC = :: FindResource (Hinst, LpszResourcename, LPSZTYPE);
// if failed, Try Instance Handle
IF (HRSRC == 0)
HRSRC = :: FindResource (hinst = afxgetinstancehandle (), lpszresourceename, lpsztype);
IF (hrsrc == null)
Return False;
Hglobal HDATA = LoadResource (Hinst, HRSRC);
IF (HDATA == Null)
Return False;
DWORD DWSIZE = SizeOfResource (HinST, HRSRC);
LPBYTE PDATA = (LPBYTE) LockResource (HDATA);
CFILE M_FILE;
CfileException E;
IF (! m_file.open (lpsztargetname, cfile :: modecreate | cfile :: ModeWrite, & E))
{
#ifdef _Debug
AFXDUMP << "File Could Not Be Opened" << E.M_CAUSE << "/ N";
#ENDIF
UNLOCKRESOURCE (HDATA);
Freeresource (HRSRC);
Return False;
}
Else
{
m_file.write (PDATA, DWSIZE);
m_file.close ();
}
UNLOCKRESOURCE (HDATA);
Freeresource (HRSRC);
Return True;
}
Parameter Description:
LpszResourceName resource name, use strings to calibrate ID values, such as "config.xml"
LPSZTYPE resource type, such as "icon", "jpg", "XML"
LPSZTARGETNAME target file name, such as "AppConfig.xml"
Call example:
CreateFileFromResource (_T ("config.xml"), _ T ("XML"), _ T ("AppConfig.xml");