📄 439-443.html
字号:
<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=""> <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=439-443//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="435-439.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="443-446.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Therefore, any string operation performed on <I>pszNoteText</I> is reflected in the memory owned by the file mapping <I>hNoteFile</I>. This is how the next line of code copies the text contained in <I>pszString</I> not just to <I>pszNoteText</I>, but to the named file mapping <I>hNoteFile,</I> which can be accessed by any other Windows CE process. (<I>pszString</I> contains the note text entered in the edit control shown in Figure 16.3.)</P>
<TABLE BORDER="2" BORDERCOLOR="#0000" ALIGN="CENTER">
<TR><TD><FONT SIZE="+1"><B>Registered Window Messages</B></FONT>
<P>The inter-process communication example in this chapter uses a special window message to notify the helper process that text has been copied into a named memory mapped file. The message used is called a <I>registered window message</I>.</P>
<P>Every window message is defined by a unique integer identifier. Browsing through the header file WINUSER.H, one finds definitions like this:</P>
<!-- CODE SNIP //-->
<PRE>
#define WM_PAINT 0x000F
</PRE>
<!-- END CODE SNIP //-->
<P>Since every Windows CE application includes WINUSER.H (by including WINDOWS.H), WM_PAINT messages in any application are identified by the same unique integer.
</P>
<P>But what if you want to define a custom window message without requiring every application that might use it to reserve the same integer identifier? The function <I>RegisterWindowMessage</I> provides a mechanism for defining messages with a unique string name:</P>
<!-- CODE SNIP //-->
<PRE>
RegisterWindowMessage(lpString);
</PRE>
<!-- END CODE SNIP //-->
<P>Any time this function is called with the same string during the same Windows CE session, it returns the same unique integer identifier. <I>RegisterWindowMessage</I> returns an integer in the range 0xC000 and 0xFFFF. This range of message identifiers is reserved by Windows CE specifically for registered window messages.</P>
<P>Therefore, any application that calls <I>RegisterWindowMessage</I> with a specific string message name will get the exact same integer identifier as any other application calling the function with the same message name. Thus, applications can define custom messages by defining some descriptive string name. Other applications needing to respond to this message only need to know the string name. The unique integer identifier will be faithfully returned by <I>RegisterWindowMessage</I>.</P>
<P>Note that the value returned by <I>RegisterWindowMessage</I> cannot be used in the switch statement of a window procedure. Case values in switch statements must be compile-time constants. Your window procedures must test for registered window message identifiers with if statements, and let the switch statement handle only the standard Windows CE messages.</P>
<P>Message name strings can be stored in the Windows CE registry. This way applications needing to send or respond to such a message only need to know the name of the registry subkey and value under which the message name is stored. Requiring applications to know registry subkey and value names is much more standard than forcing them to reserve the same message identifiers.</P>
</TABLE>
<P>MEMORY.EXE notifies the helper process that the note has been copied by broadcasting the registered window message identified by <I>nCopyMsgID</I>.<SUP><SMALL><B>1</B></SMALL></SUP></P>
<BLOCKQUOTE>
<HR>
<SUP><SMALL><B>1</B></SMALL></SUP><FONT SIZE="-1">The HWND_BROADCAST argument to <I>SendMessage</I> or <I>PostMessage</I> sends or posts the specified message to all top-level windows in the system. Notifying the helper process in this way is not ideal, as it assumes that HELPER.EXE is the only application that responds to the message being sent. But as our goal is to demonstrate the use of memory mapped files, HWND_BROADCAST is fine for our current purpose.
</FONT>
<HR>
</BLOCKQUOTE>
<P>Over in HELPER.EXE, the main window procedure handles the message <I>nCopyMsgID</I> by calling <I>CopyNoteFromOtherProcess</I>:</P>
<!-- CODE //-->
<PRE>
void CopyNoteFromOtherProcess()
{
int nSize = NOTE_LENGTH*sizeof(TCHAR);
hNoteFile = CreateFileMapping(
(HANDLE)-1,
NULL,
PAGE_READWRITE,
0,
nSize,
MAPPED_NOTE_FILE_NAME);
if (hNoteFile)
{
pszNoteText = (TCHAR*)LocalAlloc(LPTR, nSize);
pszNoteText = (TCHAR*)MapViewOfFile(
hNoteFile,
FILE_MAP_READ,
0,0,0);
}
}
</PRE>
<!-- END CODE //-->
<P>This function is almost exactly the same as <I>OnCopyNote</I> of MEMORY .EXE. It creates a file mapping with the name Note, so that it can access the same memory as was written by MEMORY.EXE. A read-only access view of this file is created, and the contents of the named file mapping are assigned to the string <I>pszNoteText</I>.</P>
<P>When this function returns, the HELPER.EXE main window procedure invalidates the client area. The WM_PAINT handler draws the note text:</P>
<!-- CODE //-->
<PRE>
LRESULT CALLBACK WndProc(
HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
RECT rc;
HDC hdc;
//Handle the nCopyMsgID message
//...
switch(message)
{
//Other window messages
//...
case WM_PAINT:
HDC hdc;
RECT rc;
PAINTSTRUCT ps;
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rc);
rc.bottom = (rc.top+50);
DrawText(hdc, pszNoteText, -1, &rc, DT_LEFT);
EndPaint(hwnd, &ps);
return (0);
default:
return (DefWindowProc(hwnd, message, wParam, lParam));
}
}
</PRE>
<!-- END CODE //-->
<H3><A NAME="Heading6"></A><FONT COLOR="#000077">Handling Low Memory Conditions</FONT></H3>
<P>Our final subject in the area of Windows CE memory management is how to handle low memory conditions in your applications.
</P>
<P>As was discussed at the beginning of this chapter, Windows CE devices are often designed with very low memory requirements. For example, the Handheld PC on which I tested all of the applications in this book has only 8 MB of RAM. With many applications running at the same time, it is possible for system RAM to be consumed very quickly. Writing Windows CE applications in a memory-efficient manner certainly helps. But with devices with as little memory as this, low memory conditions should always be expected.</P>
<P>Windows CE defines a special message for notifying applications that system memory resources are getting low. This message, WM_HIBERNATE, is sent to all active applications when a <I>hibernation threshold</I> is crossed. The hibernation threshold is defined as 128 KB of free memory on systems with 1 KB memory pages, and 160 KB for systems with 4 KB pages.</P>
<P>Applications that receive WM_HIBERNATE messages should respond to the message by closing applications that are no longer in use, freeing unused memory, or performing other operations that free memory resources. The parameters are explained in Table 16.4.</P>
<TABLE WIDTH="100%" BORDER RULES="ROWS"><CAPTION ALIGN=LEFT><B>Table 16.4</B> The WM_HIBERNATE Message
<TR>
<TH WIDTH="30%" ALIGN="LEFT">PARAMETER
<TH WIDTH="70%" ALIGN="LEFT">MEANING
<TR>
<TD>wParam
<TD>0, not used
<TR>
<TD>lParam
<TD>0, not used
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="435-439.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="443-446.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> | <a href="/contactus.html"><font color="#006666">Contact Us</font></a> | <a href="/aboutus.html"><font color="#006666">About Us</font></a> | <a href="http://www.earthweb.com/corporate/privacy.html" target="_blank"><font color="#006666">Privacy</font></a> | <a href="http://www.itmarketer.com/" target="_blank"><font color="#006666">Ad Info</font></a> | <a href="/"><font color="#006666">Home</font></a></b>
<br><br>
Use of this site is subject to certain <a href="/agreement.html">Terms & Conditions</a>, <a href="/copyright.html">Copyright © 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 + -