⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 435-439.html

📁 WindowsCE.[Essential Windows CE Application Programming].Jon Wiley & Son.zip
💻 HTML
📖 第 1 页 / 共 2 页
字号:

<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=435-439//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="432-435.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="439-443.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><I>lpName</I> can be used to pass a string used to name the file mapping. This is particularly important for inter-process communication. For two applications to gain access to the same physical memory using memory mapped files, they must both create views into the same file mapping. Naming a mapping provides a way to uniquely identify it throughout the Windows CE system.</P>
<P>Finally, <I>flProtect</I> is used to specify the type of access that is granted to the memory mapped by the file mapping object. PAGE_READONLY gives read-only access, PAGE_WRITECOPY gives write access, and PAGE_READWRITE grants read and write access.</P>
<P>If successful, <I>CreateFileMapping</I> returns a handle to the file mapping object. If the function fails, it will return NULL.</P>
<P>If <I>CreateFileMapping</I> is called and the mapping object already exists, it will return a handle to the existing object. Calling <I>GetLastError</I> in this case will return the error code:</P>
<!-- CODE SNIP //-->
<PRE>
  ERROR_ALREADY_EXISTS
</PRE>
<!-- END CODE SNIP //-->
<P><FONT SIZE="+1"><B>Creating a File View</B></FONT></P>
<P>Accessing the physical memory that corresponds to a file mapping object requires a view into the memory mapped file. The function <I>MapViewOfFile</I> is used for this purpose:</P>
<!-- CODE SNIP //-->
<PRE>
  MapViewOfFile(hFileMappingObject, dwDesiredAccess,
    dwFileOffsetHigh, dwFileOfsetLow,
     dwNumberOfBytesToMap);
</PRE>
<!-- END CODE SNIP //-->
<P><I>hFileMappingObject</I> is the mapping object handle returned previously by <I>CreateFileMapping</I>. <I>dwDesiredAccess</I> specifies the access the view will have of the memory mapped by the file mapping. Allowed values are listed in Table 16.3.</P>
<TABLE WIDTH="100%" BORDER RULES="ROWS"><CAPTION ALIGN=LEFT><B>Table 16.3</B> MapViewOfFile Access Flags
<TR>
<TH WIDTH="30%" ALIGN="LEFT">FLAG
<TH WIDTH="70%" ALIGN="LEFT">MEANING
<TR>
<TD>FILE_MAP_WRITE
<TD>The view has read-write access to the mapped file.
<TR>
<TD>FILE_MAP_READ
<TD>The view has read-only access to the mapped file.
<TR>
<TD>FILE_MAP_ALL_ACCESS
<TD>Same meaning as FILE_MAP_WRITE.
</TABLE>
<P><I>dwFileOffsetLow</I> and <I>dwFileOffsetHigh</I> are used to specify the file offset where the view starts. These parameters are useful when an application only needs to view a section of a memory mapped file. These parameters specify the low- and high-order 32 bits of the offset address, respectively.</P>
<P><I>dwNumberOfBytesToMap</I> specifies the number of bytes of the file to map. This parameter can be zero, in which case the entire file is mapped.</P>
<P><I>MapViewOfFile</I> returns a handle to the view of the memory mapped file. Physical memory corresponding to the file is accessed via this handle.</P>
<P><FONT SIZE="+1"><B>An Inter-process Communication Example</B></FONT></P>
<P>Let&#146;s put all of these concepts to work in an example. We will look at a simple example of using named memory mapped file for sharing data between two different applications.
</P>
<P>The companion CD directory that contains the MEMORY.EXE project also contains a subdirectory called Helper. This directory contains the project files for building an application, called HELPER.EXE, with which MEMORY.EXE shares data.</P>
<P>HELPER.EXE is pretty boring. It consists of nothing but a main window with a title bar. Once the application is started, it cannot be closed except from MEMORY.EXE (or the task manager). HELPER.EXE&#146;s only role in life is helping MEMORY.EXE demonstrate memory mapped files.</P>
<P>MEMORY.EXE includes a feature for entering a simple text note. This note can be read by HELPER.EXE through the use of a named memory mapped file.</P>
<P>Launch MEMORY.EXE and select the Enter a Note option from the Notes menu. An edit box appears for entering note text (Figure 16.3). The text you enter is limited to 256 characters. Once you have entered your note, press the Press To Dismiss button.</P>
<P><A NAME="Fig3"></A><A HREF="javascript:displayWindow('images/16-03.jpg',654,245 )"><IMG SRC="images/16-03t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/16-03.jpg',654,245)"><FONT COLOR="#000077"><B>Figure 16.3</B></FONT></A>&nbsp;&nbsp;Entering a note in MEMORY.EXE.</P>
<P>Next, choose the Create Helper Process option from the IPC menu (Figure 16.4). This automatically launches the HELPER.EXE application. Windows CE will switch the active process to HELPER.EXE. Go back to MEMORY.EXE by pressing its button on the taskbar.
</P>
<P><A NAME="Fig4"></A><A HREF="javascript:displayWindow('images/16-04.jpg',654,245 )"><IMG SRC="images/16-04t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/16-04.jpg',654,245)"><FONT COLOR="#000077"><B>Figure 16.4</B></FONT></A>&nbsp;&nbsp;Launching the Helper Process.</P>
<P>Finally, to copy your note text to HELPER.EXE, select the Copy Note option from the Notes menu. If you then switch back to HELPER.EXE, you will see that the main window now contains your note text (Figure 16.5).
</P>
<P><A NAME="Fig5"></A><A HREF="javascript:displayWindow('images/16-05.jpg',654,245 )"><IMG SRC="images/16-05t.jpg"></A>
<BR><A HREF="javascript:displayWindow('images/16-05.jpg',654,245)"><FONT COLOR="#000077"><B>Figure 16.5</B></FONT></A>&nbsp;&nbsp;Note text successfully copied to the Helper Process.</P>
<P>To terminate HELPER.EXE, choose the Terminate Helper Process option from the IPC menu.
</P>
<P><FONT SIZE="+1"><B><I>How It Works</I></B></FONT></P>
<P>This simple example of Windows CE inter-process communication is accomplished using a named memory mapped file. Both applications include the following definition, which is used to name a file mapping:
</P>
<!-- CODE SNIP //-->
<PRE>
  #define MAPPED_NOTE_FILE_NAME TEXT("Note")
</PRE>
<!-- END CODE SNIP //-->
<P>After launching MEMORY.EXE, note text is copied to the helper process by selecting the Copy Note menu option. This causes the application-specific function <I>OnCopyNote</I> to be called:</P>
<!-- CODE //-->
<PRE>
  #define NOTE_LENGTH 257
  TCHAR* pszNoteText;
  HANDLE hNoteFile;
  void OnCopyNote()
  &#123;
  int nSize = NOTE_LENGTH*sizeof(TCHAR);
  hNoteFile = CreateFileMapping(
   (HANDLE)-1,
   NULL,
   PAGE_READWRITE,
   0,
   nSize,
   MAPPED_NOTE_FILE_NAME);
  if (hNoteFile)
  &#123;
   pszNoteText = (TCHAR*)LocalAlloc(LPTR, nSize);
   pszNoteText = (TCHAR*)MapViewOfFile(
    hNoteFile,
    FILE_MAP_WRITE,
    0, 0, 0);
   //Copy note text into the memory mapped file
   lstrcpy(pszNoteText, pszString);
   SendMessage(HWND_BROADCAST, nCopyMsgID, 0, 0);
  &#125;
&#125;
</PRE>
<!-- END CODE //-->
<P>This looks like a lot of code, but it&#146;s actually pretty straightforward. <I>CreateFileMapping</I> creates a named file mapping object. The mapping has the name Note. Note that &#150;1 is passed to the <I>hFile</I> parameter, indicating that the file mapping is not backed by a file system file. The mapping has read-write access, and its size is large enough to hold a 256-character note, including NULL terminator.</P>
<P>Next we allocate enough heap space for the string <I>pszNoteText</I> to hold 257 Unicode characters. The next step is the critical one. <I>MapViewOfFile</I> is called, and the return view handle is cast to a TCHAR pointer and assigned to <I>pszNoteText</I>. In other words, the TCHAR array defined by <I>pszNoteText</I> is the view into the file mapping created by the <I>CreateFileMapping</I> call.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="432-435.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="439-443.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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -