📄 294-297.html
字号:
</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=""> <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=11//-->
<!--PAGES=294-297//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="291-294.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="297-300.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>Dynamic linking offers an alternative. With dynamic linking, an application does not link with the DLL’s import library. No import records are copied into the executable, and no dynamic links are established. And if there are no dynamic links in the executable file, no DLLs get loaded when the application begins execution.
</P>
<P>Instead, with dynamic linking the application is responsible for loading any DLLs that it uses. Furthermore, the application must determine the address of any function it needs to call, get a pointer to the function, and call the function by de-referencing the pointer. Finally, the application must unload the DLL when it is done using it, in the same way that it frees up resources like fonts or bitmaps.</P>
<P>The benefit of using dynamic linking is that an application can control when a particular DLL is loaded. The application can also better manage its memory resources by deleting DLLs from memory when they are not in use.</P>
<P>Dynamic linking obviously means more work for the application programmer. With static linking, as long as you include the appropriate DLL header files and link with the import library, you can make calls to DLL functions as you would call any other function. Dynamic linking forces the application programmer to load DLLs, get function pointers, and free DLLs.</P>
<P>Let’s look at the dynamic linking steps in more detail. An application loads a dynamic link library by calling the Windows CE function <I>LoadLibrary</I>:</P>
<!-- CODE SNIP //-->
<PRE>
LoadLibrary(lpLibFileName);
</PRE>
<!-- END CODE SNIP //-->
<P><I>lpLibFileName</I> is the null-terminated Unicode string name of the DLL to load. A search path to the DLL name cannot be specified. You must therefore either give the full path name of the DLL to be loaded or depend on Windows CE to find the DLL. If you choose the latter alternative, Windows CE will first look in the root directory of the storage card attached to the device, if any. If there is no such card, or the DLL is not found in the card’s root directory, Windows CE proceeds by looking in the \Windows directory. Finally, if that fails, it searches the device’s root directory.</P>
<P>If the DLL is found, <I>LoadLibrary</I> returns a handle to the DLL as an HINSTANCE. If it fails, the function returns NULL.</P>
<P>Note that <I>LoadLibrary</I> can be used to load any Windows CE module (.EXE or .DLL). It is most commonly used for DLLs, though.</P>
<P>When an application is done with a DLL, it calls <I>FreeLibrary</I>:</P>
<!-- CODE SNIP //-->
<PRE>
FreeLibrary(hLibModule);
</PRE>
<!-- END CODE SNIP //-->
<P><I>hLibModule</I> is the DLL instance handle returned by the previous <I>LoadLibrary</I> call.</P>
<P>Note that calling <I>FreeLibrary</I> does not necessarily mean that the specified module is removed from the process memory. In multithreaded applications, <I>LoadLibrary</I> can be called by one or more threads in a process, incrementing the module’s <I>reference count</I>. <I>FreeLibrary</I> decrements this reference count for the specified module and only removes it from memory once its usage count goes to zero.</P>
<P>Finally, to get a pointer to an exported DLL function, an application uses <I>GetProcAddress</I>:</P>
<!-- CODE SNIP //-->
<PRE>
GetProcAddress(hModule, lpProcName);
</PRE>
<!-- END CODE SNIP //-->
<P><I>hModule</I> is the instance handle of the DLL containing the function of interest. <I>lpProcName</I> is the Unicode string name of the function. <I>GetProcAddress</I> returns a pointer to the requested function.</P>
<P>We will see an example of how to dynamically link with a DLL a little later in this chapter.</P>
<P><FONT SIZE="+1"><B><I>Exporting DLL Functions</I></B></FONT></P>
<P>The discussion above made references to <I>exported</I> DLL functions. A function must be exported by a DLL in order for it to be available to applications or other DLLs.</P>
<P>A dynamic link library can export a function using any one of the following techniques:</P>
<DL>
<DD><B>•</B> Using the /EXPORT linker option
<DD><B>•</B> Defining functions to be exported with the __declspec(dllexport) modifier
<DD><B>•</B> Specifying the functions to be exported in a module definition file
</DL>
<P>A module definition file name uses the .DEF extension.
</P>
<P>Let’s look at an example of a DLL module definition file. The custom button control DLL of this chapter exports the function <I>InitCustomButton</I> via this module definition file:</P>
<!-- CODE SNIP //-->
<PRE>
LIBRARY CONTROL.DLL
EXPORTS
InitCustomButton @1
</PRE>
<!-- END CODE SNIP //-->
<P>The first line of the file assigns the name “CONTROL.DLL” to the DLL. The EXPORTS keyword says that the functions that follow are to be exported. Specifically, these are the functions for which import records are included in the import library that is generated when the DLL is linked. The @ sign specifies the ordinal number to assign to the corresponding function in the import record. If an ordinal number is not specified in the .DEF file, one is assigned by the linker.
</P>
<P>Any application that appropriately links with CONTROL.DLL, either statically or dynamically, can now call the function <I>InitCustomButton</I>.</P>
<P><FONT SIZE="+1"><B><I>The DLL Entry Point</I></B></FONT></P>
<P>When a Windows CE application starts running, a little piece of start-up code added to the beginning of the .EXE file by the linker calls a function known as the application <I>entry point</I>. This function is called <I>WinMain</I>. It is the function that application programmers think of as the starting point of their applications.</P>
<P>Dynamic link libraries also have an entry point. At various times, such as when a DLL is initially loaded by Windows CE, the operating system calls the function <I>DllMain</I>. The signature of <I>DllMain</I> is:</P>
<!-- CODE SNIP //-->
<PRE>
BOOL WINAPI DllMain(hinstDLL,
fdwReason, lpvReserved);
</PRE>
<!-- END CODE SNIP //-->
<P>The WINAPI modifier is simply defined as __<I>stdcall</I> in the Windows CE header files.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="291-294.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="297-300.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 + -