Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

embed and retrieve files in an executable

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hello! I would like to know how can I embed a binary data file into an executable. When the exe is run, the binary file will be copied to the temp folder of the user's computer. Can I include the file in the resource file? But then how can I retrieve and save the file to a directory? Please don't use MFC (just use API and c/c++).


Thanks in advance
 
Hi

Here is how I did it for an html file that I included as a resource to ensure existence of this file at the startup of the application.

There is a little bit MFC inside ( I'm working only with MFC, sorry ..) but You'll remove it easily

HTH

Thierry
EMail: Thierry.Marneffe@swing.be


// Load the resource into memory
// Note: the resource is identified as follow in the resource file:
//
/////////////////////////////////////////////////////////////////////////////
//
// REPORT_HTML
//
// IDR_REPORT_TEMPLATE REPORT_TEMPLATE DISCARDABLE "res\\report_h.bin"

HINSTANCE hinst = AfxGetInstanceHandle();

HRSRC hRes = FindResource( hinst, (LPCSTR) IDR_REPORT_TEMPLATE, "REPORT_TEMPLATE");

if ( hRes == NULL)
{
AfxMessageBox( "Couldn't find Report Template !!! ");

return FALSE;
}

// Get Size of Resource in Bytes

int nLength = ( int) SizeofResource( hinst, hRes);

// Load the Resource in Global Memory

BYTE* lpRes = ( BYTE*) LoadResource( hinst, hRes);
ASSERT( lpRes);

char* pszBuffer;

// Create a Memory File from Resource Data

CMemFile file( lpRes, nLength);

// Clean Up

FreeResource(( HANDLE) lpRes);

// Allocate Memory to Read Memory File

pszBuffer = ( char*) new char[nLength];

if ( pszBuffer == ( char*) NULL)
{
AfxMessageBox( "Not Enough Memory to Get Report Template !!!");

return FALSE;
}

// Read File

file.Read( pszBuffer, nLength);

// Save Memory File to Disk

CFileException e;
CFile fileReportTemplate;

try
{
fileReportTemplate.Open( "ReportTemplate.htm",
CFile::modeCreate | CFile::modeWrite | CFile::shareDenyNone, &e);

}
catch ( CFileException e)
{
AfxMessageBox( "Unable to Create Template " + e.m_cause);
}

// Save Data to File

fileReportTemplate.Write( pszBuffer, nLength);

fileReportTemplate.Close();

// Clean Up

delete [] pszBuffer;

return TRUE;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top