📄 mfc programmer's sourcebook miscellaneous.htm
字号:
<!-- Sample image - gif or jpg -->
<P><IMG SRC="ExeCreator.gif" >
<!-- For which environment is this code specific??? -->
<p><u>Environment:</u> VC6
<p>ExeCreator is a small utility to enwrap any number of files into a single exe. This new enwrapped exe when executed spits all the files to a desired folder.
</p>
<p>
It consists of two applications: ExeCreator and ExeExtractor.
</p>
<p>
<u>ExeCreator:</u><br> It is the main application which provides GUI for receiving file names to be added, creates a new ExeExtractor.exe from one present in its resources, and adds all the files as resources to this new ExeExtractor.exe.
It uses UpdateResource() to dynamically add files (converted to specified format known to ExeExtractor.exe) into resource in Extractor.exe.
</p>
<p>
<u>ExeExtractor:</u><br> It extracts the files added as resource by ExeCreator into a folder.
</p>
<p>
In next version of this program I'll try to add compression in file data stored and an encryption option. But presently also it provides a useful tool for self-extracting exe's especially used in installations.
</p>
<PRE>
<span class="codeComment">// Create a buffer which will be added as resource to
// the ExeExtractor exe
// First byte in buffer is set to number of files in list</span>
LPSTR buff = (LPSTR)new DWORD(m_InList.GetCount());
UINT nSize = sizeof(DWORD);
<span class="codeComment">// Add each file</span>
for(DWORD i = 0;i < *(DWORD *)buff;++i)
{
CString inFName;
m_InList.GetText(i,inFName);
<span class="codeComment"> // Open i file </span>
FILE* fpIn = fopen(inFName,"rb");
if(fpIn == NULL)
{
CString errMsg;
errMsg.Format( "Could not open %d file : %s",
i + 1,
(LPCSTR)inFName);
AfxMessageBox(errMsg);
delete buff;
return;
}
<span class="codeComment"> // Retieve file size file </span>
fseek(fpIn,0,SEEK_END);
UINT nFSize = ftell(fpIn);
fseek(fpIn,0,SEEK_SET);
<span class="codeComment"> // Extract just file name from full pathname of file</span>
for(int nCtr = 0,nMid = 0;nCtr < inFName.GetLength();++nCtr)
if(inFName[nCtr] == '\\' || inFName[nCtr] == ':')
nMid = nCtr + 1;
CString onlyFName = inFName.Mid(nMid);
<span class="codeComment"> // Realloc previous buffer to accomodate this file's info</span>
LPSTR newbuff = new char[nSize +
(onlyFName.GetLength() + 1) +
sizeof(UINT) + nFSize];
memcpy(newbuff,buff,nSize);
delete buff;
buff = newbuff;
<span class="codeComment"> // Copy file name with null character</span>
memcpy( buff + nSize,
(LPCSTR)onlyFName,
onlyFName.GetLength() + 1);
<span class="codeComment"> // Copy file size</span>
*(UINT *)(buff + nSize + onlyFName.GetLength() + 1) =
nFSize;
<span class="codeComment"> // Copy file data</span>
VERIFY(fread( buff + nSize +
sizeof(UINT) +
(onlyFName.GetLength() + 1),
1,nFSize,fpIn) == nFSize);
nSize += sizeof(UINT) +
(onlyFName.GetLength() + 1) +
nFSize;
fclose(fpIn);
}
<span class="codeComment">// Create a dummy EXE from ExeExtractor</span>
FILE* fpOut = fopen(m_outFName,"wb");
if(fpOut == NULL)
{
AfxMessageBox("Could not create output file" +
m_outFName);
delete buff;
return;
}
<span class="codeComment">// Find Load and Lock resource</span>
HRSRC hResLoad = FindResource(NULL,
MAKEINTRESOURCE(IDR_IDR_EXE_EXEEXTRACTOR),
"IDR_EXE");
HGLOBAL hResData = LoadResource(NULL,hResLoad);
LPCSTR data = (LPCSTR)LockResource(hResData);
<span class="codeComment">// Write the ExeCreator binary data to this file</span>
ASSERT(hResLoad != NULL && hResData != NULL && data != NULL);
fwrite(data,1,SizeofResource(NULL,hResLoad),fpOut);
fclose(fpOut);
FreeResource(hResData);
<span class="codeComment">// Open the file for addition of file data in resource</span>
HANDLE hUpdateRes = BeginUpdateResource(m_outFName, FALSE);
ASSERT(hUpdateRes != NULL);
<span class="codeComment">// Add the file data to the update list.</span>
VERIFY(UpdateResource(hUpdateRes,
"IDR_EXE",MAKEINTRESOURCE(IDR_IDR_EXE_EXEEXTRACTOR),
MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),
buff,nSize));
delete buff;
<span class="codeComment">// Write changes and then close it. </span>
VERIFY(EndUpdateResource(hUpdateRes, FALSE));
</PRE>
<h3>Downloads</h3>
<A HREF="ExeCreator.zip">Download demo project - 24 Kb</A><BR>
<A HREF="ExeCreatorSrc.zip">Download source - 7 Kb</A><br>
<h3>History</h3>
Date Posted: December 21, 2001<BR>
<!-- Only use the following if the article is updated -->
<!-- Date Last Updated: [today's date in the format month day, year] -->
<!--comments--><h3>Comments:</h3><ul><!--startlist--><li><a href="/mfc/comments/41306.shtml">Great! Reading files without extracting?</a> - Lea Hayes (2002/08/08)<li><a href="/mfc/comments/31086.shtml">Excellent Code! Good Job!</a> - Ram Sharma (2001/12/28)<li><a href="/mfc/comments/30919.shtml">Similar utility in codeguru</a> - Sreedharan (2001/12/23)<li><a href="/mfc/comments/30909.shtml">better yet - Moving </a> - ERN (2001/12/23)<li><a href="/mfc/comments/30907.shtml">an alternative.. PEBundle</a> - Jim Tarnoff (2001/12/22)<li><a href="/mfc/comments/30894.shtml">Will it beat WinZip???</a> - Saurabh Agrawal (2001/12/22)<li><a href="/mfc/comments/30876.shtml">Windows 95/98?</a> - Gene (2001/12/21)</ul><P><strong><a href="/cgi-bin/addpage/addpage.cgi?cfg=addcomment&action=Comment">Add Comment</a></strong><P></td>
<!--BLJAds - Added right side tower ad -->
<TD VALIGN="TOP">
<SCRIPT LANGUAGE="javascript">
function SetFormAction() {
newLocation = "http://hardwarecentral.dealtime.com/xKW-" + document.frmGSRequest.keyword.value + "/NS-1/linkin_id-3011678/GS.html";
document.location.href = newLocation;
return;
}
</SCRIPT>
<link rel=stylesheet type="text/css" href="http://www.internet.com/icom_includes/special/dealtime/style.css">
<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="2" BGCOLOR="#000000">
<TR>
<TD>
<table cellpadding="2" cellspacing="0" border="0" width="125" bgcolor="#FFFFCC">
<tr>
<td align="center">
<img src="http://www.codeguru.com/RealMedia/ads/Creatives/House_Dealtime_Promotion_1b/hc_logo.gif" alt="" border="0">
<br>
<font class="normal" color="#4D4D4D"><b>Compare products, prices, and stores at Hardware Central!</b></font>
</td>
</tr>
<tr>
<td align="center" class="norm">
<br>
<B><A HREF="http://hardwarecentral.dealtime.com/BannerIn/1,2844,,00.html?linkin_id=3011654&nFormId=38733-72-1&path=%2FPages%2FCategoryV2">Computers</A></B><BR>
<A HREF="http://hardwarecentral.dealtime.com/xPP-Desktops~linkin_id-3011655">Desktops</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-Notebooks_Mac~linkin_id-3011656">Mac</A> & <A HREF="http://hardwarecentral.dealtime.com/xPP-PC_Notebooks~linkin_id-3011657">PC Notebooks</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-Monitors~linkin_id-3011658">Monitors</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-Scanners~linkin_id-3011659">Scanners</A>,
<A HREF="http://hardwarecentral.dealtime.com/xPP-Web_Cameras~linkin_id-3011660">Webcams</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-Personal_Organizers~linkin_id-3011661">PDA's</A>, <A HREF="http://hardwarecentral.dealtime.com/BannerIn/1,2844,,00.html?linkin_id=3011662&nFormId=38733-72-1&path=%2FPages%2FCategoryV2">more</A>...<BR>
<BR>
<B><A HREF="http://hardwarecentral.dealtime.com/BannerIn/1,2844,,00.html?linkin_id=3011663&nFormId=38863-368-1&path=%2FPages%2FCategoryV2C">Software</A></B><BR>
<A HREF="http://hardwarecentral.dealtime.com/xPP-Creativity_Applications~linkin_id-3011664">Creativity Applications</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-Programming_Tools~linkin_id-3011665">Programming Tools</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-Internet_and_Communication_Applications~linkin_id-3011666">Internet & Communication Applications</A>, <A HREF="http://hardwarecentral.dealtime.com/BannerIn/1,2844,,00.html?linkin_id=3011667&nFormId=38863-368-1&path=%2FPages%2FCategoryV2C">more</A>...<BR>
<BR>
<B><A HREF="http://hardwarecentral.dealtime.com/BannerIn/1,2844,,00.html?linkin_id=3011668&nFormId=38732-3-1&path=%2FPages%2FCategoryV2">Electronics</A></B><BR>
<A HREF="http://hardwarecentral.dealtime.com/xPP-Digital_Cameras~linkin_id-3011669">Digital Cameras</A> & <A HREF="http://hardwarecentral.dealtime.com/xCP-Digital_Camera_Accessories~FD-87204~linkin_id-3011670">Accessories</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-GPS_devices~linkin_id-3011671">GPS devices</A> & <A HREF="http://hardwarecentral.dealtime.com/xCP-GPS_Accessories~FD-93207~linkin_id-3011672">Accessories</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-Camcorders~linkin_id-3011673">Camcorders</A>, <A HREF="http://hardwarecentral.dealtime.com/xPP-MP3_-Digital_Audio_Players~linkin_id-3011674">MP3 Players</A> & <A HREF="http://hardwarecentral.dealtime.com/xCP-MP3_Accessories~FD-87208~linkin_id-3011675">Accessories</A>, <A HREF="http://hardwarecentral.dealtime.com/BannerIn/1,2844,,00.html?linkin_id=3011676&nFormId=38732-3-1&path=%2FPages%2FCategoryV2">more</A>...
</td>
</tr>
<tr><form name="frmGSRequest" method="get" onsubmit="javascript:SetFormAction();return false;">
<td align="center" valign="top" class="normal">
Get the best price on <A HREF="http://hardwarecentral.dealtime.com/xKW-microsoft_visual_studio_net_professional/FN-Programming_Tools/DL-0/NS-1/linkin_id-3011677/GS.html">Microsoft Visual Studio .NET Professional Edition</A> or search for other development tools<br>
<br>
<input type="text" name="keyword" size="9"><br><br>
<input value="Search" type="image" src="http://www.codeguru.com/RealMedia/ads/Creatives/House_Dealtime_Promotion_1b/search.gif" BORDER="0"><br><br>
</td>
</tr></form>
</table>
</TD>
</TR>
</TABLE><IMG SRC="http://www.codeguru.com/RealMedia/ads/adstream_lx.cgi/ew/ewsoftware/www.codeguru.com/misc/ExeCreator.html/1363419877/125x800/House_Dealtime_Promotion_1b/dealtime_developer.html/64333965313038343366623839623830?_RM_EMPTY_" WIDTH=1 HEIGHT=1 BORDER=0>
<p>
<br>
</TD>
</tr>
</table><!-- begin footer information -->
<br>
<CENTER>
<A HREF="http://www.codeguru.com/RealMedia/ads/click_lx.cgi/ew/ewsoftware/www.codeguru.com/misc/ExeCreator.html/605772626/468x60-2/House_468B/itsolutons.gif/64333965313038343366623839623830" target="_top"><IMG SRC="http://www.codeguru.com/RealMedia/ads/Creatives/House_468B/itsolutons.gif" WIDTH=468 HEIGHT=60 ALT="" BORDER="0"></A><img src="http://www.codeguru.com/RealMedia/ads/adstream_lx.cgi/ew/ewsoftware/www.codeguru.com/misc/ExeCreator.html/605772626/468x60-2/House_468B/itsolutons.gif/64333965313038343366623839623830?_RM_EMPTY_" Width="1" Height="1" Border="0">
<br>
<CENTER><FONT SIZE="-1"><SPAN CLASS="foot">Jupitermedia is publisher of the <a href="http://www.internet.com/">internet.com</a> and <a href="http://www.earthweb.com">EarthWeb.com</a> networks.<hr width="95%" size="1" noshade color="#000000">Copyright 2003 Jupitermedia Corporation All Rights Reserved.</SPAN><BR><A HREF="http://www.internet.com/corporate/legal.html" CLASS="foot">Legal Notices</A>, <A HREF="http://www.internet.com/corporate/reprints.html#Licensing1" CLASS="foot">Licensing</A>, <A HREF="http://www.internet.com/corporate/reprints.html#Reprints1" CLASS="foot">Reprints</A>, & <A HREF="http://www.internet.com/corporate/reprints.html#Permissions" CLASS="foot">Permissions</A>, <A HREF="http://www.internet.com/corporate/privacy/privacypolicy.html" CLASS="foot">Privacy Policy</A>.<BR><A HREF="http://www.itmarketer.com/" CLASS="foot">Advertise on EarthWeb</A><BR><A HREF="http://www.earthweb.com/"><IMG SRC="/icom_includes/footers/URL-ew.gif" ALT="http://www.earthweb.com/" WIDTH="200" HEIGHT="18" BORDER="0"></A><br><a href="http://www.internet.com/"><img src="/icom_includes/footers/URL.gif" alt="http://www.internet.com/" width="161" height="18" border="0"></a></FONT></CENTER>
<!-- Old footer. Replaced by above header --
<FONT SIZE="-1">EarthWeb is a service of Jupitermedia Corp.<br>
Copyright 2003 Jupitermedia Corp. All Rights Reserved.<BR>
<A HREF="http://www.internet.com/help/feedback.html">Feedback</A>,
<A HREF="http://www.itmarketer.com">Advertising Info</A>,
<A HREF="http://www.internet.com/corporate/legal.html">Legal Notices</A>,
<A HREF="http://www.internet.com/corporate/reprints.html#Licensing">Licensing</A>,
<A HREF="http://www.internet.com/corporate/reprints.html#Reprints">Reprints</A>, &
<A HREF="http://www.internet.com/corporate/reprints.html#Permissions">Permissions</A>,
<A HREF="http://www.internet.com/corporate/privacy/privacypolicy.html">Privacy Policy</A>.
<BR>
<A HREF="http://www.internet.com"><IMG SRC="/images/internetcom.gif"
ALT="http://www.internet.com/" WIDTH="161" HEIGHT="18"
BORDER="0"></A></FONT>
-->
</CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -