﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-&lt;p align=center&gt;Code Is Poetry&lt;/p&gt;-文章分类-程序</title><link>http://www.cppblog.com/lonestep/category/5334.html</link><description>&lt;p align=center&gt;More simple but not too simple&lt;/p&gt;</description><language>zh-cn</language><lastBuildDate>Thu, 17 Mar 2011 03:32:55 GMT</lastBuildDate><pubDate>Thu, 17 Mar 2011 03:32:55 GMT</pubDate><ttl>60</ttl><item><title>可执行资源的获取和修改</title><link>http://www.cppblog.com/lonestep/articles/138860.html</link><dc:creator>lonestep</dc:creator><author>lonestep</author><pubDate>Wed, 19 Jan 2011 05:57:00 GMT</pubDate><guid>http://www.cppblog.com/lonestep/articles/138860.html</guid><wfw:comment>http://www.cppblog.com/lonestep/comments/138860.html</wfw:comment><comments>http://www.cppblog.com/lonestep/articles/138860.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lonestep/comments/commentRss/138860.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lonestep/services/trackbacks/138860.html</trackback:ping><description><![CDATA[
		<font face="Verdana" size="1">  最近尝试用一些WINDOWS API来修改和获取资源，根据MSDN，这些API主要有：<br />  </font>
		<ol>
				<li>
						<font face="Verdana" size="1">Use the <?XML:NAMESPACE PREFIX = MSHelp NS = "http://msdn.microsoft.com/mshelp" /?><mshelp:link tabindex="0" keywords="_win32_LoadLibrary" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">LoadLibrary</font></u></mshelp:link> function to load the executable file Hand.exe. </font>
				</li>
				<li>
						<font face="Verdana" size="1">Use the <mshelp:link tabindex="0" keywords="_win32_FindResource_cpp" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">FindResource</font></u></mshelp:link> and <mshelp:link tabindex="0" keywords="_win32_LoadResource_cpp" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">LoadResource</font></u></mshelp:link> functions to locate and load the dialog box resource. </font>
				</li>
				<li>
						<font face="Verdana" size="1">Use the <mshelp:link tabindex="0" keywords="_win32_LockResource_cpp" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">LockResource</font></u></mshelp:link> function to retrieve a pointer to the dialog box resource data. </font>
				</li>
				<li>
						<font face="Verdana" size="1">Use the <mshelp:link tabindex="0" keywords="_win32_BeginUpdateResource_cpp" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">BeginUpdateResource</font></u></mshelp:link> function to open an update handle to Foot.exe. </font>
				</li>
				<li>
						<font face="Verdana" size="1">Use the <mshelp:link tabindex="0" keywords="_win32_UpdateResource_cpp" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">UpdateResource</font></u></mshelp:link> function to copy the dialog box resource from Hand.exe to Foot.exe. </font>
				</li>
				<li>
						<font face="Verdana" size="1">Use the <mshelp:link tabindex="0" keywords="_win32_EndUpdateResource_cpp" xmlns:mshelp="http://msdn.microsoft.com/mshelp"><u><font color="#0000ff">EndUpdateResource</font></u></mshelp:link> function to complete the update. </font>
				</li>
		</ol>
		<p>
				<br />
				<font face="Verdana" size="1">   我手动的插入了一个二进制资源(RT_RCDATA)，不过发现FindResource这里总是返回NULL，<br />   最后问题终于解决了，要注意的地方主要有2点：<br />   1，可能是VC的BUG，加入资源后需要手动修改 .rc文件，加入<br /> /////////////////////////////////////////////////////////////////////////////<br />//<br />// RCDATA<br />//</font>
		</p>
		<p>
				<br />
				<font face="Verdana" size="1">IDR_RCDATA2           RCDATA           "res\\rcdata1.bin"<br /><br />  否则.rc文件里只有这句：<br />/////////////////////////////////////////////////////////////////////////////<br />//<br />// RCDATA<br />//<br /><br />2，这里指定了RCDATA 放在 res\\rcdata1.bin里，如果你的rcdata1.bin的内容是空的，也会FindResource失败。<br /><br />还有，就是如果你想修改正在执行的程序的资源，恐怕用UpdateResource是会失败的，即使调用EndUpdateResource也不行！<br />只能修改未执行的exe，如果有朋友知道什么解决办法可以修改正在执行中程序的资源，欢迎告知!<br /><br />下面是MSDN上关于如何修改exe资源的例子： 从hand.exe 中读取资源并复制到 foot.exe。</font>
		</p>
		<p>
				<font face="Verdana" size="1">
				</font>
		</p>
		<div style="BORDER-RIGHT: #cccccc 1px solid; PADDING-RIGHT: 5px; BORDER-TOP: #cccccc 1px solid; PADDING-LEFT: 4px; FONT-SIZE: 13px; PADDING-BOTTOM: 4px; BORDER-LEFT: #cccccc 1px solid; WIDTH: 98%; WORD-BREAK: break-all; PADDING-TOP: 4px; BORDER-BOTTOM: #cccccc 1px solid; BACKGROUND-COLOR: #eeeeee">
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080"> 1</span>
								<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								<span style="COLOR: #000000">HRSRC hResLoad;     </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> handle to loaded resource </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080"> 2</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">HANDLE hExe;        </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> handle to existing .EXE file </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080"> 3</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">HRSRC hRes;         </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> handle/ptr. to res. info. in hExe </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080"> 4</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">HANDLE hUpdateRes;  </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> update resource handle </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080"> 5</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">char</span>
								<span style="COLOR: #000000"> </span>
								<span style="COLOR: #000000">*</span>
								<span style="COLOR: #000000">lpResLock;    </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> pointer to resource data </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080"> 6</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">BOOL result; <br /></span>
								<span style="COLOR: #008080"> 7</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Load the .EXE file that contains the dialog box you want to copy. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080"> 8</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">hExe </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> LoadLibrary(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">hand.exe</span>
								<span style="COLOR: #000000">"</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">); <br /></span>
								<span style="COLOR: #008080"> 9</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000"> (hExe </span>
								<span style="COLOR: #000000">==</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> NULL) <br /></span>
								<span style="COLOR: #008080">10</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_393_438_Open_Image" onclick="this.style.display='none'; Codehighlighter1_393_438_Open_Text.style.display='none'; Codehighlighter1_393_438_Closed_Image.style.display='inline'; Codehighlighter1_393_438_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_393_438_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_393_438_Closed_Text.style.display='none'; Codehighlighter1_393_438_Open_Image.style.display='inline'; Codehighlighter1_393_438_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span id="Codehighlighter1_393_438_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_393_438_Open_Text">
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">{ <br /></span>
										<span style="COLOR: #008080">11</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ErrorHandler(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Could not load exe.</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">); <br /></span>
										<span style="COLOR: #008080">12</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> <br /></span>
								<span style="COLOR: #008080">13</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /> <br /></span>
								<span style="COLOR: #008080">14</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Locate the dialog box resource in the .EXE file. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">15</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">hRes </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> FindResource(hExe, </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">AboutBox</span>
								<span style="COLOR: #000000">"</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">, RT_DIALOG); <br /></span>
								<span style="COLOR: #008080">16</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000"> (hRes </span>
								<span style="COLOR: #000000">==</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> NULL) <br /></span>
								<span style="COLOR: #008080">17</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_566_620_Open_Image" onclick="this.style.display='none'; Codehighlighter1_566_620_Open_Text.style.display='none'; Codehighlighter1_566_620_Closed_Image.style.display='inline'; Codehighlighter1_566_620_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_566_620_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_566_620_Closed_Text.style.display='none'; Codehighlighter1_566_620_Open_Image.style.display='inline'; Codehighlighter1_566_620_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span id="Codehighlighter1_566_620_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_566_620_Open_Text">
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">{ <br /></span>
										<span style="COLOR: #008080">18</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ErrorHandler(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Could not locate dialog box.</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">); <br /></span>
										<span style="COLOR: #008080">19</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> <br /></span>
								<span style="COLOR: #008080">20</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /> <br /></span>
								<span style="COLOR: #008080">21</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Load the dialog box into global memory. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">22</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">hResLoad </span>
								<span style="COLOR: #000000">=</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> LoadResource(hExe, hRes); <br /></span>
								<span style="COLOR: #008080">23</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000"> (hResLoad </span>
								<span style="COLOR: #000000">==</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> NULL) <br /></span>
								<span style="COLOR: #008080">24</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_730_782_Open_Image" onclick="this.style.display='none'; Codehighlighter1_730_782_Open_Text.style.display='none'; Codehighlighter1_730_782_Closed_Image.style.display='inline'; Codehighlighter1_730_782_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_730_782_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_730_782_Closed_Text.style.display='none'; Codehighlighter1_730_782_Open_Image.style.display='inline'; Codehighlighter1_730_782_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span id="Codehighlighter1_730_782_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_730_782_Open_Text">
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">{ <br /></span>
										<span style="COLOR: #008080">25</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ErrorHandler(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Could not load dialog box.</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">); <br /></span>
										<span style="COLOR: #008080">26</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> <br /></span>
								<span style="COLOR: #008080">27</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /> <br /></span>
								<span style="COLOR: #008080">28</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Lock the dialog box into global memory. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">29</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">lpResLock </span>
								<span style="COLOR: #000000">=</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> LockResource(hResLoad); <br /></span>
								<span style="COLOR: #008080">30</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000"> (lpResLock </span>
								<span style="COLOR: #000000">==</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> NULL) <br /></span>
								<span style="COLOR: #008080">31</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_892_944_Open_Image" onclick="this.style.display='none'; Codehighlighter1_892_944_Open_Text.style.display='none'; Codehighlighter1_892_944_Closed_Image.style.display='inline'; Codehighlighter1_892_944_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_892_944_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_892_944_Closed_Text.style.display='none'; Codehighlighter1_892_944_Open_Image.style.display='inline'; Codehighlighter1_892_944_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span id="Codehighlighter1_892_944_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_892_944_Open_Text">
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">{ <br /></span>
										<span style="COLOR: #008080">32</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ErrorHandler(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Could not lock dialog box.</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">); <br /></span>
										<span style="COLOR: #008080">33</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> <br /></span>
								<span style="COLOR: #008080">34</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /> <br /></span>
								<span style="COLOR: #008080">35</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Open the file to which you want to add the dialog box resource. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">36</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">hUpdateRes </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> BeginUpdateResource(</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">foot.exe</span>
								<span style="COLOR: #000000">"</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">, FALSE); <br /></span>
								<span style="COLOR: #008080">37</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000"> (hUpdateRes </span>
								<span style="COLOR: #000000">==</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> NULL) <br /></span>
								<span style="COLOR: #008080">38</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_1096_1154_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1096_1154_Open_Text.style.display='none'; Codehighlighter1_1096_1154_Closed_Image.style.display='inline'; Codehighlighter1_1096_1154_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_1096_1154_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1096_1154_Closed_Text.style.display='none'; Codehighlighter1_1096_1154_Open_Image.style.display='inline'; Codehighlighter1_1096_1154_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span id="Codehighlighter1_1096_1154_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_1096_1154_Open_Text">
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">{ <br /></span>
										<span style="COLOR: #008080">39</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ErrorHandler(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Could not open file for writing.</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">); <br /></span>
										<span style="COLOR: #008080">40</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> <br /></span>
								<span style="COLOR: #008080">41</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /> <br /></span>
								<span style="COLOR: #008080">42</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Add the dialog box resource to the update list. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">43</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">result </span>
								<span style="COLOR: #000000">=</span>
								<span style="COLOR: #000000"> UpdateResource(hUpdateRes,       </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> update resource handle </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">44</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">     RT_DIALOG,                   </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> change dialog box resource </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">45</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">     </span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">AboutBox</span>
								<span style="COLOR: #000000">"</span>
								<span style="COLOR: #000000">,                  </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> dialog box name </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">46</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">     MAKELANGID(LANG_NEUTRAL, SUBLANG_NEUTRAL),  </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> neutral language</span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">47</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">     lpResLock,                   </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> ptr to resource info </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">48</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #000000">     SizeofResource(hExe, hRes)); </span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> size of resource info. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">49</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000"> (result </span>
								<span style="COLOR: #000000">==</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> FALSE) <br /></span>
								<span style="COLOR: #008080">50</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_1610_1659_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1610_1659_Open_Text.style.display='none'; Codehighlighter1_1610_1659_Closed_Image.style.display='inline'; Codehighlighter1_1610_1659_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_1610_1659_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1610_1659_Closed_Text.style.display='none'; Codehighlighter1_1610_1659_Open_Image.style.display='inline'; Codehighlighter1_1610_1659_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span id="Codehighlighter1_1610_1659_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_1610_1659_Open_Text">
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">{ <br /></span>
										<span style="COLOR: #008080">51</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ErrorHandler(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Could not add resource.</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">); <br /></span>
										<span style="COLOR: #008080">52</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> <br /></span>
								<span style="COLOR: #008080">53</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /> <br /></span>
								<span style="COLOR: #008080">54</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Write changes to FOOT.EXE and then close it. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">55</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000"> (</span>
								<span style="COLOR: #000000">!</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">EndUpdateResource(hUpdateRes, FALSE)) <br /></span>
								<span style="COLOR: #008080">56</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_1757_1815_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1757_1815_Open_Text.style.display='none'; Codehighlighter1_1757_1815_Closed_Image.style.display='inline'; Codehighlighter1_1757_1815_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_1757_1815_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1757_1815_Closed_Text.style.display='none'; Codehighlighter1_1757_1815_Open_Image.style.display='inline'; Codehighlighter1_1757_1815_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span id="Codehighlighter1_1757_1815_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_1757_1815_Open_Text">
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">{ <br /></span>
										<span style="COLOR: #008080">57</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ErrorHandler(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Could not write changes to file.</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">); <br /></span>
										<span style="COLOR: #008080">58</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000"> <br /></span>
								<span style="COLOR: #008080">59</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" /> <br /></span>
								<span style="COLOR: #008080">60</span>
								<span style="COLOR: #000000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #008000">//</span>
								<span style="COLOR: #008000"> Clean up. </span>
						</font>
				</font>
				<span style="COLOR: #008000">
						<br />
				</span>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #008080">61</span>
								<span style="COLOR: #008000">
										<img src="http://www.cppblog.com/images/OutliningIndicators/None.gif" align="top" />
								</span>
								<span style="COLOR: #0000ff">if</span>
								<span style="COLOR: #000000"> (</span>
								<span style="COLOR: #000000">!</span>
						</font>
				</font>
				<font face="Verdana">
						<font size="1">
								<span style="COLOR: #000000">FreeLibrary(hExe)) <br /></span>
								<span style="COLOR: #008080">62</span>
								<span style="COLOR: #000000">
										<img id="Codehighlighter1_1859_1911_Open_Image" onclick="this.style.display='none'; Codehighlighter1_1859_1911_Open_Text.style.display='none'; Codehighlighter1_1859_1911_Closed_Image.style.display='inline'; Codehighlighter1_1859_1911_Closed_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockStart.gif" align="top" />
										<img id="Codehighlighter1_1859_1911_Closed_Image" style="DISPLAY: none" onclick="this.style.display='none'; Codehighlighter1_1859_1911_Closed_Text.style.display='none'; Codehighlighter1_1859_1911_Open_Image.style.display='inline'; Codehighlighter1_1859_1911_Open_Text.style.display='inline';" src="http://www.cppblog.com/images/OutliningIndicators/ContractedBlock.gif" align="top" />
								</span>
								<span id="Codehighlighter1_1859_1911_Closed_Text" style="BORDER-RIGHT: #808080 1px solid; BORDER-TOP: #808080 1px solid; DISPLAY: none; BORDER-LEFT: #808080 1px solid; BORDER-BOTTOM: #808080 1px solid; BACKGROUND-COLOR: #ffffff">
										<img src="http://www.cppblog.com/images/dot.gif" />
								</span>
						</font>
				</font>
				<span id="Codehighlighter1_1859_1911_Open_Text">
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">{ <br /></span>
										<span style="COLOR: #008080">63</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/InBlock.gif" align="top" />    ErrorHandler(</span>
										<span style="COLOR: #000000">"</span>
										<span style="COLOR: #000000">Could not free executable.</span>
										<span style="COLOR: #000000">"</span>
								</font>
						</font>
						<font face="Verdana">
								<font size="1">
										<span style="COLOR: #000000">); <br /></span>
										<span style="COLOR: #008080">64</span>
										<span style="COLOR: #000000">
												<img src="http://www.cppblog.com/images/OutliningIndicators/ExpandedBlockEnd.gif" align="top" />}</span>
								</font>
						</font>
				</span>
				<span style="COLOR: #000000">
						<font face="Verdana" size="1"> </font>
				</span>
		</div>
		<p>
				<font face="Verdana" size="1">
				</font>
		</p>
<img src ="http://www.cppblog.com/lonestep/aggbug/138860.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lonestep/" target="_blank">lonestep</a> 2011-01-19 13:57 <a href="http://www.cppblog.com/lonestep/articles/138860.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>一起学miniFilter驱动 之（一） 准备篇</title><link>http://www.cppblog.com/lonestep/articles/138680.html</link><dc:creator>lonestep</dc:creator><author>lonestep</author><pubDate>Mon, 17 Jan 2011 08:22:00 GMT</pubDate><guid>http://www.cppblog.com/lonestep/articles/138680.html</guid><wfw:comment>http://www.cppblog.com/lonestep/comments/138680.html</wfw:comment><comments>http://www.cppblog.com/lonestep/articles/138680.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lonestep/comments/commentRss/138680.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lonestep/services/trackbacks/138680.html</trackback:ping><description><![CDATA[
		<font face="Verdana">   以前做过一段时间的文件过滤驱动，是老的文件过滤驱动架构（也就是基于IRP过滤的legacy filter），最近突然对做过滤驱动有些小怀念，又看到大家都在学习WDK的新过滤驱动(miniFilter),于是赶紧去<br />下载了个WDK，念及自己一直都是从互联网获取自己需要的知识，却很少去回帖跟风，这样不好不好，<br />于是这里把自己整个学习过程和需要注意的环节记录下来，希望能对别人有所帮助。<br /><br />  工欲善其事必先利其器，这里还是选用WINDOWS开发者喜欢的VC+WDK+WinDbg来学习和调试驱动。<br /><br />   VC装的是VisualStudio 2008，这个我不能提供给您，自己搞定吧^^<br /><br />   WDK开发包(WINDOWS DRIVER KITS) 下载：<br />   <font size="1">简单的说，WDK 是DDK的升级版，微软把以前DDK的很多函数、机制做了封装，简化了开发难度。<br /></font>   关于WDK的新旧版本过滤驱动管理的区别，微软有一个文档，可以从这里下载：<br /><a href="http://download.microsoft.com/download/f/0/5/f05a42ce-575b-4c60-82d6-208d3754b2d6/filter_manager.ppt">http://download.microsoft.com/download/f/0/5/f05a42ce-575b-4c60-82d6-208d3754b2d6/filter_manager.ppt</a><br />   WDK可以从微软免费下载：<br />   <a href="http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&amp;FamilyID=36a2630f-5d56-43b5-b996-7633f2ec14ff">http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&amp;FamilyID=36a2630f-5d56-43b5-b996-7633f2ec14ff</a><br /><br /><br />   DDKWizard下载：<br /><font size="1">   DDKWizard是一个可以用VC来建立驱动项目的软件，安装之后就可以创建WDK项目，这样我们就可以用VisualStudio 来build驱动，也方便编辑源码。<br />   下载地址：</font><br />   <a href="http://ddkwizard.assarbad.net/">http://ddkwizard.assarbad.net/</a><br />   <br />   安装完将ddkbuild.cmd拷贝到你的WDK目录下，例如D:\\WDK（假如你的WDK在D:\\WDK\\7600.16385.1），打开VS2008-》工具-》选项-》项目和解决方案-》VC++目录-》可执行文件，选择刚才放ddkbuild.cmd的目录(刚才的D:\\WDK)，然后新建一个驱动项目，把PREfast选项去掉，点完成，然后把自己的驱动代码加进项目，按F7即可编译。<br /><br />   遇到问题，互联网是你最好的朋友，学驱动有几个网站可以常去：<br />   <a href="http://www.driverdevelop.com">www.driverdevelop.com</a> 和 <a href="http://www.osronline.com">www.osronline.com</a> 都不错。<br /><br />   同时，微软提供了很多的过滤驱动学习资料下载：<br />   <a href="http://www.microsoft.com/whdc/driver/filterdrv/default.mspx">http://www.microsoft.com/whdc/driver/filterdrv/default.mspx</a><br /><br />   安装完了， 也可以BUILD了，可以在虚拟机上跑，关于用Windbg和虚拟机一起来调试程序的，<br />请点这里：<br /><br />   <a href="http://www.baidu.com/s?bs=ddkwizard&amp;f=8&amp;wd=%D0%E9%C4%E2%BB%FA+%B5%F7%CA%D4%C7%FD%B6%AF&amp;oq=xuniji&amp;sug=%D0%E9%C4%E2%BB%FA&amp;n=1&amp;rsp=0">http://www.baidu.com/s?bs=ddkwizard&amp;f=8&amp;wd=%D0%E9%C4%E2%BB%FA+%B5%F7%CA%D4%C7%FD%B6%AF&amp;oq=xuniji&amp;sug=%D0%E9%C4%E2%BB%FA&amp;n=1&amp;rsp=0</a><br /><br /><br />   可能您还会遇到很多问题，但是我相信都是可以解决的，所以假设到这里您已经搭好环境，能build出目标驱动，并用Windbg和虚拟机进行调试。如果有问题，可以发邮件到<a href="mailto:lonestep@qq.com">lonestep@qq.com</a>互相学习。</font>
<img src ="http://www.cppblog.com/lonestep/aggbug/138680.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lonestep/" target="_blank">lonestep</a> 2011-01-17 16:22 <a href="http://www.cppblog.com/lonestep/articles/138680.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>[转载] 关于share memory</title><link>http://www.cppblog.com/lonestep/articles/45727.html</link><dc:creator>lonestep</dc:creator><author>lonestep</author><pubDate>Sun, 30 Mar 2008 00:37:00 GMT</pubDate><guid>http://www.cppblog.com/lonestep/articles/45727.html</guid><wfw:comment>http://www.cppblog.com/lonestep/comments/45727.html</wfw:comment><comments>http://www.cppblog.com/lonestep/articles/45727.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/lonestep/comments/commentRss/45727.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/lonestep/services/trackbacks/45727.html</trackback:ping><description><![CDATA[<h1 align=center>Shared Memory</h1>
<p>In Win32, memory can be shared amongst several processes on the same machine using file mapping.&nbsp; Mechanical devices such as hard drives have very high access latencies and low transfer rates as compared to electrical devices such as memory.&nbsp; Operating systems alleviate these discrepancies by using buffering techniques.&nbsp; A buffer is nothing more than memory allocation that is used to hold information from the slower mechanical device.&nbsp; The buffer caches all the data that is read from or written to the disk.&nbsp;&nbsp; For example, a hundred bytes written to a file would be buffered with out actual writing to the file containing device.&nbsp; If the same 100 bytes need to be read, they would be read from the buffer.&nbsp; Reading from the buffer is far faster than, if the data was written to the disk, followed by a read of the disk drive.&nbsp; If several processes need access to the same file at the same time, they all access it through the same buffer(to keep consistent reads and writes).&nbsp; File mapping in win32 offers direct access to the user for this file buffer.&nbsp; The file buffer space is mapped to the local process space of each of the processes.&nbsp; Since memory is broken into pages, the buffer space is always rounded up to the next page size.</p>
<p>The <strong>CreateFileMapping</strong> function is used to create a file-mapping kernel object which is used to refer to the file buffer of a given file.&nbsp; The function takes a handle to a file object as an argument.&nbsp; File mapping kernel objects can either anonymous or named.&nbsp; The <strong>CreateFileMapping</strong> function takes a name argument.&nbsp; If the argument is NULL, than the file mapping object is anonymous. &nbsp; If the argument is a non-NULL string, then the object is named.&nbsp; If the object already exists, a reference to the existing file mapping object is returned. The <strong>OpenFileMapping</strong> function can be used to get access to the a named file mapping object.&nbsp; The function <strong>MapViewOfFile</strong> is used to map the file buffer to the process space of the current process. It takes an argument of a handle to the file mapping object.&nbsp; The return value of the function is a pointer to the beginning of the buffer in the processes own memory space.&nbsp; If the process modifies memory locations in the buffer, when the file is finally closed and buffers flushed(written) to the disk, the file would be changed.&nbsp; The function <strong>UnmapViewOfFile</strong> is used to remove the buffer from the current processes memory space.&nbsp; The <strong>CloseHandle</strong> function can be used to close the a handle to the file-mapping kernel object.</p>
<p>If the <strong>CreateFileMapping</strong> function is passed a value <strong>0xFFFFFFFF</strong> as an argument for the file handle(file whose buffer is going to be shared), the function will return a handle to a buffer that is not associated with any file.&nbsp; This buffer can be used amongst several processes as shared memory.&nbsp; Using <strong>OpenFileMapping</strong> is more difficult, due to writing code for creation and opening of file mapping kernels, as compared to that of just using <strong>CreateFileMapping</strong> function alone.</p>
<p>The <strong>CreateFileMapping</strong> takes arguments to specify the memory access rights.&nbsp; If a file is opened for reading only and the handle is passed to <strong>CreateFileMapping</strong>, the associated buffer can only be read from.&nbsp; It cannot be written to.&nbsp; If an attempt is made to write to that memory, the process will receive a protection fault. &nbsp; If a file is opened for both reading and writing, then the file mapping object can be created with a subset of the permissions.&nbsp; The file mapping object can be made for reading and writing or only reading.</p>
<p>Using shared memory in conjunction with either interlocked functions or critical section objects is very efficient as compared to creating and using kernel objects such as semaphores, mutexes, and event objects.</p>
<table width="100%" border=1>
    <tbody>
        <tr>
            <td width="17%">Proto-Type:</td>
            <td width="83%"><strong>HANDLE CreateFileMapping</strong>
            <blockquote>
            <blockquote>
            <p><strong>(</strong> </p>
            <table>
                <tbody>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>HANDLE</strong> <em>hFile</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// handle to file to map -- 0xFFFFFFF, if only shared memory req.</font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>LPSECURITY_ATTRIBUTES</strong> <em>lpFileMappingAttributes</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// optional security attributes </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>DWORD</strong> <em>flProtect</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// protection for mapping object </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>DWORD</strong> <em>dwMaximumSizeHigh</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// high-order 32 bits of object size&nbsp; - use 0, for file size</font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>DWORD</strong> <em>dwMaximumSizeLow</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// low-order 32 bits of object size&nbsp; - use 0, for file size</font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>LPCTSTR</strong> <em>lpName</em> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// name of file-mapping object </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>);</strong> </font></td>
                    </tr>
                </tbody>
            </table>
            </blockquote></blockquote></td>
        </tr>
        <tr>
            <td width="17%">Info:</td>
            <td width="83%">Function return a handle to either a new file mapping object or to an existing one.&nbsp; The return value is NULL, if function fails.&nbsp; The flProtect argument can be one of the following.
            <blockquote>
            <blockquote>
            <ul>
                <li>PAGE_READONLY&nbsp;&nbsp; -&nbsp; memory&nbsp; is readonly
                <li>PAGE_READWRITE&nbsp; - memory is readable and writable
                <li>PAGE_WRITECOPY&nbsp; - memory is readable, after memory has been written too. </li>
            </ul>
            </blockquote></blockquote>
            <p>The dwMaximumSizeHigh and dwMaximuSizeLow arguments are used to specify the size of the object.</p>
            </td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<table width="100%" border=1>
    <tbody>
        <tr>
            <td width="17%">Proto-Type:</td>
            <td width="83%"><strong>LPVOID MapViewOfFile</strong>
            <blockquote>
            <blockquote>
            <p><strong>(</strong> </p>
            <table>
                <tbody>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>HANDLE</strong> <em>hFileMappingObject</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// file-mapping object to map into address space </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>DWORD</strong> <em>dwDesiredAccess</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// access mode </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>DWORD</strong> <em>dwFileOffsetHigh</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// high-order 32 bits of file offset </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>DWORD</strong> <em>dwFileOffsetLow</em><strong>,</strong> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// low-order 32 bits of file offset </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>DWORD</strong> <em>dwNumberOfBytesToMap</em> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// number of bytes to map </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>);</strong> </font></td>
                    </tr>
                </tbody>
            </table>
            </blockquote></blockquote></td>
        </tr>
        <tr>
            <td width="17%">Info:</td>
            <td width="83%">Function maps a buffer referred to by the file mapping object to the local process space of the current process. The function returns NULL, if&nbsp; there was an error.&nbsp; Otherwise, it returns a legal address.&nbsp; The dwDesiredAccess has the following values:
            <blockquote>
            <blockquote>
            <ul>
                <li>FILE_MAP_READ -&nbsp;&nbsp; mapping can only be read only
                <li>FILE_MAP_WRITE&nbsp; - mapping can either be read or write </li>
            </ul>
            </blockquote></blockquote></td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<table width="100%" border=1>
    <tbody>
        <tr>
            <td width="17%">Proto-Type:</td>
            <td width="83%"><strong>BOOL UnmapViewOfFile</strong>
            <blockquote>
            <blockquote>
            <p><strong>(</strong> </p>
            <table>
                <tbody>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>LPCVOID</strong> <em>lpBaseAddress</em> </font></td>
                        <td><font face=ARIAL,HELVETICA size=2>// address where mapped view begins </font></td>
                    </tr>
                    <tr vAlign=top>
                        <td><font face=ARIAL,HELVETICA size=2><strong>);</strong> </font></td>
                    </tr>
                </tbody>
            </table>
            </blockquote></blockquote></td>
        </tr>
        <tr>
            <td width="17%">Info:</td>
            <td width="83%">A file buffer that was mapped to the current process space is realeased with this function.&nbsp; The lpBaseAddress argument takes the return value from the &nbsp; <strong>MapViewOfFile</strong> function.&nbsp; The return value of this function is TRUE on success.</td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<p>The next program demonstrates the conversion of a file to upper case by first creating a file mapping for the ReadMe.txt text file.</p>
<table width="100%" border=1>
    <tbody>
        <tr>
            <td width="100%">//&nbsp; Program converts a file to upper case
            <p>#include &lt;assert.h&gt;<br>#include &lt;ctype.h&gt;<br>#include &lt;stdio.h&gt;<br>#include &lt;windows.h&gt;<br><br><br>static const char szFileName[]="ReadMe.txt";<br><br>int main(void)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; HANDLE hFile=CreateFile(szFileName,GENERIC_READ|GENERIC_WRITE<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0,NULL);<br>&nbsp;&nbsp;&nbsp; assert(hFile!=INVALID_HANDLE_VALUE);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // create anonymous file mapping<br>&nbsp;&nbsp;&nbsp; HANDLE hMap=CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,0,NULL);<br>&nbsp;&nbsp;&nbsp; assert(hMap!=NULL);<br><br>&nbsp;&nbsp;&nbsp; DWORD dwFileSize=GetFileSize(hFile,NULL); // get file size<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // map buffer to local process space<br>&nbsp;&nbsp;&nbsp; char *memory=(char *)MapViewOfFile(hMap,FILE_MAP_WRITE,0,0,dwFileSize);<br>&nbsp;&nbsp;&nbsp; assert(memory!=NULL);<br><br>&nbsp;&nbsp;&nbsp; for(DWORD i=0;i&lt;dwFileSize;++i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(islower(memory[i])!=0)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { toupper(memory[i]); }<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp; UnmapViewOfFile(memory); // unmap file buffer<br><br>&nbsp;&nbsp;&nbsp; CloseHandle(hMap); CloseHandle(hFile);<br>&nbsp;&nbsp;&nbsp; return 0;<br>&nbsp;&nbsp;&nbsp; }</p>
            </td>
        </tr>
    </tbody>
</table>
<p>&nbsp;</p>
<p>The next program demonstrates access to shared memory.&nbsp; Two processes access a ten buffers.&nbsp; Each buffer has a maximum size of 100 bytes.&nbsp; The first process is writer process that write information to the buffers in ascending order.&nbsp; The second process is a reader that reads the buffers.&nbsp; This is the standard reader-writer problem found in operating system courses.</p>
<table width="100%" border=1>
    <tbody>
        <tr>
            <td width="100%">/* Writer Code */<br><br>#include &lt;windows.h&gt;<br>#include &lt;assert.h&gt;<br>#include &lt;stdio.h&gt;<br><br><br>static const LONG BufferSize=100;<br>static const LONG Buffers=10;<br>static const char MemoryName[]="Share Memory Name";<br>static char (*Memory)[Buffers][BufferSize];<br>static const char WriterSemaphoreName[]="Writer Semaphore";<br>static const char ReaderSemaphoreName[]="Reader Semaphore";<br><br>static HANDLE hWriterSemaphore;<br>static HANDLE hReaderSemaphore;<br>static HANDLE hMemory;<br><br>int main(void)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; hWriterSemaphore=CreateSemaphore(NULL,Buffers,Buffers,WriterSemaphoreName);<br>&nbsp;&nbsp;&nbsp; assert(hWriterSemaphore!=NULL);<br><br>&nbsp;&nbsp;&nbsp; hReaderSemaphore=CreateSemaphore(NULL,0,Buffers,ReaderSemaphoreName);<br>&nbsp;&nbsp;&nbsp; assert(hReaderSemaphore!=NULL);<br><br>&nbsp;&nbsp;&nbsp; hMemory=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,sizeof(char [Buffers][BufferSize]),MemoryName);<br>&nbsp;&nbsp;&nbsp; assert(hMemory!=NULL);<br><br>&nbsp;&nbsp;&nbsp; Memory=(char (*)[Buffers][BufferSize])MapViewOfFile(hMemory<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,FILE_MAP_WRITE,0,0,sizeof(char [Buffers][BufferSize]));<br>&nbsp;&nbsp;&nbsp; assert(Memory!=NULL);<br><br>&nbsp;&nbsp;&nbsp; for(int i=0;;++i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WaitForSingleObject(hWriterSemaphore,INFINITE);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; printf("Writing to Buffer %i\n",i);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; wsprintf((*Memory)[i%Buffers],"This is the writer - %i\n",i);<br>&nbsp;&nbsp;&nbsp; <br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Sleep(1000);<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReleaseSemaphore(hReaderSemaphore,1,NULL);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; UnmapViewOfFile(Memory); <br>&nbsp;&nbsp;&nbsp; CloseHandle(hWriterSemaphore); CloseHandle(hReaderSemaphore);<br>&nbsp;&nbsp;&nbsp; return 0;<br>&nbsp;&nbsp;&nbsp; }<br><br></td>
        </tr>
        <tr>
            <td width="100%">// Reader Code<br><br>#include &lt;windows.h&gt;<br>#include &lt;assert.h&gt;<br>#include &lt;stdio.h&gt;<br><br><br>static const LONG BufferSize=100;<br>static const LONG Buffers=10;<br>static const char MemoryName[]="Share Memory Name";<br>static char (*Memory)[Buffers][BufferSize];<br>static const char WriterSemaphoreName[]="Writer Semaphore";<br>static const char ReaderSemaphoreName[]="Reader Semaphore";<br><br>static HANDLE hWriterSemaphore;<br>static HANDLE hReaderSemaphore;<br>static HANDLE hMemory;<br><br>int main(void)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; hWriterSemaphore=CreateSemaphore(NULL,Buffers,Buffers,WriterSemaphoreName);<br>&nbsp;&nbsp;&nbsp; assert(hWriterSemaphore!=NULL);<br><br>&nbsp;&nbsp;&nbsp; hReaderSemaphore=CreateSemaphore(NULL,0,Buffers,ReaderSemaphoreName);<br>&nbsp;&nbsp;&nbsp; assert(hReaderSemaphore!=NULL);<br><br>&nbsp;&nbsp;&nbsp; hMemory=CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE,0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,sizeof(char [Buffers][BufferSize]),MemoryName);<br>&nbsp;&nbsp;&nbsp; assert(hMemory!=NULL);<br><br>&nbsp;&nbsp;&nbsp; Memory=(char (*)[Buffers][BufferSize])MapViewOfFile(hMemory<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ,FILE_MAP_WRITE,0,0,sizeof(char [Buffers][BufferSize]));<br>&nbsp;&nbsp;&nbsp; assert(Memory!=NULL);<br><br>&nbsp;&nbsp;&nbsp; for(int i=0;;++i)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; WaitForSingleObject(hReaderSemaphore,INFINITE);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; puts((*Memory)[i%10]); // Reader reads data<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ReleaseSemaphore(hWriterSemaphore,1,NULL);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br><br>&nbsp;&nbsp;&nbsp; UnmapViewOfFile(Memory); <br>&nbsp;&nbsp;&nbsp; CloseHandle(hWriterSemaphore); CloseHandle(hReaderSemaphore);<br>&nbsp;&nbsp;&nbsp; return 0;<br>&nbsp;&nbsp;&nbsp; }<br></td>
        </tr>
    </tbody>
</table>
<comine.com><small>copyright?001 Prof Devi - mdevi@comine.com, mdevi@liu.edu. All rights reserved<small></comine.com> </small></small>
<img src ="http://www.cppblog.com/lonestep/aggbug/45727.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/lonestep/" target="_blank">lonestep</a> 2008-03-30 08:37 <a href="http://www.cppblog.com/lonestep/articles/45727.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>