432-435.html

来自「WindowsCE.[Essential Windows CE Applicat」· HTML 代码 · 共 363 行 · 第 1/2 页

HTML
363
字号
<!-- LEFT NAV SEARCH END -->

		</td>
		
<!-- PUB PARTNERS END -->
<!-- END LEFT NAV -->

<td rowspan="8" align="right" valign="top"><img src="/images/iswbls.gif" width=1 height=400 alt="" border="0"></td>
<td><img src="/images/white.gif" width="5" height="1" alt="" border="0"></td>
<!-- end of ITK left NAV -->

<!-- begin main content -->
<td width="100%" valign="top" align="left">


<!-- END SUB HEADER -->

<!--Begin Content Column -->

<FONT FACE="Arial,Helvetica" SIZE="-1">
To access the contents, click the chapter and section titles.
</FONT>
<P>
<B>Essential Windows CE Application Programming</B>
<FONT SIZE="-1">
<BR>
<I>(Publisher: John Wiley & Sons, Inc.)</I>
<BR>
Author(s): Robert Burdick
<BR>
ISBN: 0471327476
<BR>
Publication Date: 03/01/99
</FONT>
<P>
<form name="Search" method="GET" action="http://search.earthweb.com/search97/search_redir.cgi">

<INPUT TYPE="hidden" NAME="Action" VALUE="Search">
<INPUT TYPE="hidden" NAME="SearchPage" VALUE="http://search.earthweb.com/search97/samples/forms/srchdemo.htm">
<INPUT TYPE="hidden" NAME="Collection" VALUE="ITK">
<INPUT TYPE="hidden" NAME="ResultTemplate" VALUE="itk-simple-intrabook.hts">
<INPUT TYPE="hidden" NAME="ViewTemplate" VALUE="view.hts">

<font face="arial, helvetica" size=2><b>Search this book:</b></font><br>
<INPUT NAME="queryText" size=50 VALUE="">&nbsp;<input type="submit" name="submitbutton" value="Go!">
<INPUT type=hidden NAME="section_on" VALUE="on">
<INPUT type=hidden NAME="section" VALUE="http://www.itknowledge.com/reference/standard/0471327476/">

</form>


<!-- Empty Reference Subhead -->

<!--ISBN=0471327476//-->
<!--TITLE=Essential Windows CE Application Programming//-->
<!--AUTHOR=Robert Burdick//-->
<!--PUBLISHER=John Wiley & Sons, Inc.//-->
<!--IMPRINT=Wiley Computer Publishing//-->
<!--CHAPTER=16//-->
<!--PAGES=432-435//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="428-432.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="435-439.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>The only <I>flOptions</I> flag allowed is HEAP_NO_SERIALIZE. If specified, only the calling thread can allocate and free memory on the heap. <I>flOptions</I> can also be zero, in which case multiple threads can access the heap.</P>
<P><I>dwInitialSize</I> specifies the amount of memory initially allocated for the heap. <I>dwMaximumSize</I> specifies the maximum size of the heap.</P>
<P>If <I>dwMaximumSize</I> is zero, the heap can grow, and Windows CE grows the heap if more than <I>dwInitialSize</I> bytes are allocated. If <I>dwMaximumSize</I> is not zero, the heap cannot grow larger than this size.</P>
<P>If successful, <I>HeapCreate</I> returns a handle to the newly created heap. Otherwise it returns NULL.</P>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B><B>H<SMALL>EAP</SMALL>C<SMALL>REATE</SMALL> O<SMALL>PTIONS</SMALL></B>
<P><B>The heap APIs under Windows CE do not support the HEAP_GENERATE_EXCEPTIONS flag.</B><HR></FONT>
</BLOCKQUOTE>
</P>
<P>A heap is destroyed by calling <I>HeapDestroy</I>. This is done when the heap is no longer needed. All heap memory is decommitted and released by this function.</P>
<!-- CODE SNIP //-->
<PRE>
  HeapDestroy(hHeap);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>hHeap</I> parameter is the handle to the heap to be destroyed.</P>
<P>Allocating and freeing memory on an application heap are done with the <I>HeapAlloc</I> and <I>HeapFree</I> functions, respectively.</P>
<P><I>HeapAlloc</I> works very much like <I>LocalAlloc</I>:</P>
<!-- CODE SNIP //-->
<PRE>
  HeapAlloc(hHeap, dwFlags, dwBytes);
</PRE>
<!-- END CODE SNIP //-->
<P>Whereas <I>LocalAlloc</I> always operates on the application&#146;s default heap, <I>HeapAlloc</I> can be used to allocate memory on any heap an application creates with <I>HeapCreate</I>. You specify which heap to allocate memory in by passing the handle to the heap in the <I>hHeap</I> parameter. <I>dwFlags</I> specifies how the heap is created. <I>dwSize</I> is the number of bytes to allocate.</P>
<P><I>dwFlags</I> can be any combination of the values HEAP_NO_SERIALIZE and HEAP_ZERO_MEMORY. HEAP_NO_SERIALIZE has the same meaning as it does when used with the <I>HeapCreate</I> function. HEAP_ZERO_MEMORY tells <I>HeapAlloc</I> to initialize all allocated memory to zero.</P>
<P>Freeing a heap is done with <I>HeapFree</I>:</P>
<!-- CODE SNIP //-->
<PRE>
  HeapFree(hHeap, dwFlags, lpMem);
</PRE>
<!-- END CODE SNIP //-->
<P><I>hHeap</I> is the heap containing the memory to be freed. <I>lpMem</I> points to the block of memory to be freed. <I>dwFlags</I> can be 0, or HEAP_NO_SERIALIZE. Generally this parameter will be zero.</P>
<H3><A NAME="Heading5"></A><FONT COLOR="#000077">Windows CE Memory Mapped Files</FONT></H3>
<P>Earlier in the chapter I said that only about half of the 2 GB virtual address space shared by applications is dedicated to the process slots in which running processes reside. What about the rest of this space?
</P>
<P>The rest of the 2 GB application address space is used for <I>memory mapped files.</I> A memory mapped file is a block of physical memory that can be shared by different applications. This memory is called a <I>file</I> because memory mapped files are often used to access the contents of files in the Windows CE file system.</P>
<P>Memory mapped files do not have to correspond to a file system file. Memory mapped files can be created to hold data that is shared between processes. As we will see, this provides a powerful technique for inter-process communication in Windows CE.</P>
<P><FONT SIZE="+1"><B>Memory Mapped File Fundamentals</B></FONT></P>
<P>To use a memory mapped file, an application must create a <I>file mapping object</I>. A file mapping object associates, or maps, the contents of a file to a region of an application&#146;s virtual address space.</P>
<P>The file mapping object is a kernel object that Windows CE uses to maintain the file mapping. This object does not provide any access to the mapped file data. To access the physical memory containing the file contents, an application must create a <I>view</I> of the memory mapped file.</P>
<P>The file view is that portion of the application&#146;s virtual address space which the application references to get to the contents of the file. The actual file data is somewhere in physical memory.</P>
<P>The memory mapped file memory is accessed by referencing this file view object. Applications treat file views as they would data pointers. Any change to the data pointed to by the file view is reflected in the memory mapped file. If the memory mapped file is backed by a real file, writing data to the view has the effect of writing to the actual file. Likewise, reading data from the address specified by the view reads data from the file system file.</P>
<P>If the memory mapped file is not backed by a file, reading or writing data through the view reads or writes data to the physical memory corresponding to the file mapping.</P>
<P>Memory mapped files are often used instead of the file system API for file access. This is much faster because, as the name suggests, the memory mapped file resides in memory. Windows CE takes care of writing the data to the actual file efficiently.</P>
<P>Memory mapped files can be <I>named</I> or <I>unnamed</I>. Since the Windows CE kernel maintains file mapping objects, naming a file mapping makes it unique throughout the system. In short, this means that different processes that create views into the same file mapping have access to the same physical memory. This is why memory mapped files can be used for communicating between Windows CE processes.</P>
<P>Finally, memory mapped files do not have to be backed up by a real file in the file system. Applications can create memory mapped files simply to access regions of physical memory.</P>
<P><FONT SIZE="+1"><B>Creating a File Mapping</B></FONT></P>
<P>A file mapping is created with the Windows CE API <I>CreateFileMapping</I>:</P>
<!-- CODE SNIP //-->
<PRE>
  CreateFileMapping(hFile, lpFileMappingAttributes, flProtect,
    dwMaximumSizeHigh, dwMaximumSizeLow, lpName);
</PRE>
<!-- END CODE SNIP //-->
<P>The <I>lpFileMappingAttributes</I> parameter is not used under Windows CE and must therefore be NULL.</P>
<P><I>hFile</I> is a file handle. If the file mapping object is being created to access a physical file, this parameter will be the file handle returned by a <I>CreateFileForMapping</I> call. To create a file mapping not backed by a file, pass &#150;1 to the <I>hFile</I> parameter.</P>
<P><I>CreateFileForMapping</I> has exactly the same signature as the file system API <I>CreateFile</I>. It returns a handle to a file just as <I>CreateFile</I> does, except that this handle can be used to create a file mapping. See Chapter 6 for details on using the <I>CreateFile</I> function.</P>
<P>If the mapping is not backed by a file, you must specify the size of the memory mapped region via the <I>dwMaximumSizeLow</I> and <I>dwMaximumSizeHigh</I> parameters. The first specifies the low-order 32 bits of the region size, while the second specifies the upper 32 bits.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="428-432.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="435-439.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>


<!-- all of the reference materials (books) have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->

<!-- BEGIN SUB FOOTER -->
		<br><br>
		</TD>
    </TR>
	</TABLE>

		
	<table width="640" border=0 cellpadding=0 cellspacing=0>
		<tr>
		<td align="left" width=135><img src="/images/white.gif" width=100 height="1" alt="" border="0"></td>
		
		
<!-- END SUB FOOTER -->

<!-- all of the books have the footer and subfoot reveresed -->
<!-- reference_subfoot = footer -->
<!-- reference_footer = subfoot -->

<!-- FOOTER -->
			
		<td width="515" align="left" bgcolor="#FFFFFF">
<font face="arial, helvetica" size="1"><b><a href="/products.html"><font color="#006666">Products</font></a>&nbsp;|&nbsp; <a href="/contactus.html"><font color="#006666">Contact Us</font></a>&nbsp;|&nbsp; <a href="/aboutus.html"><font color="#006666">About Us</font></a>&nbsp;|&nbsp; <a href="http://www.earthweb.com/corporate/privacy.html" target="_blank"><font color="#006666">Privacy</font></a> &nbsp;|&nbsp; <a href="http://www.itmarketer.com/" target="_blank"><font color="#006666">Ad Info</font></a> &nbsp;|&nbsp; <a href="/"><font color="#006666">Home</font></a></b>
		<br><br>
		
		Use of this site is subject to certain <a href="/agreement.html">Terms &amp; Conditions</a>, <a href="/copyright.html">Copyright &copy; 1996-1999 EarthWeb Inc.</a><br> 
All rights reserved.  Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.</font><p>
</td>
		</tr>
</table>
</BODY>
</HTML>

<!-- END FOOTER -->

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?