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

📄 297-300.html

📁 WindowsCE.[Essential Windows CE Application Programming].Jon Wiley & Son.zip
💻 HTML
📖 第 1 页 / 共 2 页
字号:
</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=11//-->
<!--PAGES=297-300//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->

<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="294-297.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="300-303.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P><I>hinstDLL</I> contains the instance handle of the DLL for which <I>DllMain</I> is being called. <I>fdwReason</I> and <I>lpvReserved</I> can take on various values depending on why <I>DllMain</I> is being called. These values and when they are passed to <I>DllMain</I> are described in Tables 11.2 and 11.3.</P>
<TABLE WIDTH="100%" BORDER RULES="ROWS"><CAPTION ALIGN=LEFT><B>Table 11.2</B> DllMain fdwReason Parameter Values
<TR>
<TH WIDTH="30%" ALIGN="LEFT">VALUE
<TH WIDTH="70%" ALIGN="LEFT">MEANING
<TR>
<TD>DLL_PROCESS_ATTACH
<TD>DLL is being loaded for the first time by an application.
<TR>
<TD VALIGN="TOP">DLL_PROCESS_DETACH
<TD>DLL is being detached from the process that uses it. This happens when the process terminates or a <I>FreeLibrary</I> call has forced the DLL usage count to 0.
<TR>
<TD>DLL_THREAD_ATTACH
<TD>A new thread has been created in the calling process.
<TR>
<TD>DLL_THREAD_DETACH
<TD>A thread has been terminated in the calling process.
</TABLE>
<P>
</P>
<TABLE WIDTH="100%" BORDER><CAPTION ALIGN=LEFT><B>Table 11.3</B> DllMain lpvReserved Parameter Values
<TR>
<TH WIDTH="25%" ALIGN="LEFT">VALUE
<TH WIDTH="75%" ALIGN="LEFT">MEANING
<TR>
<TD VALIGN="TOP">NULL
<TD>If <I>fdwReason</I> is DLL_PROCESS_ATTACH, this means that <I>DllMain</I> was called as a result of a dynamic <I>LoadLibrary</I> call.<BR>If <I>fdwReason</I> is DLL_PROCESS_DETACH, this means that <I>DllMain</I> was called as a result of a dynamic <I>FreeLibrary</I> call that reduced the DLL reference count to 0.
<TR>
<TD VALIGN="TOP">Non-NULL
<TD>If <I>fdwReason</I> is DLL_PROCESS_ATTACH, this means that <I>DllMain</I> was called as a result of a static DLL load when the process started.<BR>If <I>fdwReason</I> is DLL_PROCESS_DETACH, this means that <I>DllMain</I> was called as a result of process termination.
</TABLE>
<BLOCKQUOTE>
<P><FONT SIZE="-1"><HR><B>NOTE:&nbsp;&nbsp;</B><B>C<SMALL>HANGING THE</SMALL> DLL E<SMALL>NTRY</SMALL> P<SMALL>OINT</SMALL> F<SMALL>UNCTION</SMALL> N<SMALL>AME</SMALL></B>
<P><B>You can freely change the entry point function name on a DLL-by-DLL basis. Simply use the /entry linker flag when linking the particular DLL. For example, many programmers like to use the name <I>DllEntryPoint</I> for their DLLs. To do so, add the following to the Project Options under the Link tab of the corresponding Microsoft Developer Studio project settings:</B></P>
<!-- CODE SNIP //-->
<PRE>
    /entry:"DllEntryPoint"
</PRE>
<!-- END CODE SNIP //-->
<P><B>Of course this new name must then be used in the entry point function implementation.</B><HR></FONT>
</BLOCKQUOTE>
</P>
<P>The value returned by <I>DllMain</I> is ignored except when the <I>fdwReason</I> parameter is DLL_PROCESS_ATTACH. In this case, <I>DllMain</I> should return TRUE if the DLL initialization succeeds and FALSE if it fails.</P>
<P><FONT SIZE="+1"><B>DLL Benefits</B></FONT></P>
<P>There are numerous reasons for using dynamic link libraries. The most important reasons include:
</P>
<DL>
<DD><B>Maintainability.</B> Applications are broken down into a number of components, each of which is easier to maintain than a larger monolithic application.
<DD><B>Reusability.</B> Functions and resources exported by a DLL are more easily used by multiple applications.
<DD><B>Memory management.</B> Applications have more direct control over memory usage if they choose when to load and free DLLs.
</DL>
<H3><A NAME="Heading4"></A><FONT COLOR="#000077">Initializing the DLL in the Client Application</FONT></H3>
<P>Before we explore the details of programming our custom control DLL, let&#146;s look at how the client application initializes the DLL. This provides a real example of how to dynamically link an application with a DLL.
</P>
<P>To use our custom button control, an application must do two things. It must first link with the dynamic link library that implements the control. As discussed above, this can be done either statically or dynamically. Next, the application must call the appropriate function to register the custom control window class.</P>
<P>The client application CUSTOM.EXE dynamically links with the custom control library CONTROL.DLL. The code below comes from the <I>WinMain</I> function found on the companion CD in the file \Samples\custom\main.cpp. <I>hInstDLL</I> is an HINSTANCE defined in the file \Samples\custom\custom.h.</P>
<P>The DLL initialization function called <I>InitCustomButton</I> is described in detail in the next section.</P>
<!-- CODE //-->
<PRE>
  #include &lt;control.h&gt;
  typedef void (*LPINITCUSTOMBUTTON)();
  LPINITCUSTOMBUTTON lpicb;
  HINSTANCE hInstDLL;
  HWND hwndExit;
  int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPTSTR lpCmdLine,
    int nCmdShow)
  &#123;
    MSG msg;
    WNDCLASS wndClass;
    /* Save application instance in ghInst for
      possible use by other functions, such as
      the main window&#146;s window procedure.
     */
    ghInst = hInstance;
    /* We are dynamically linking with control.dll.
      The application must therefore load the DLL
      and get the address of all exported functions
      that it wishes to call.
     */
    hInstDLL = LoadLibrary(TEXT("control.dll"));
    if (hInstDLL)
    &#123;
     lpicb = (LPINITCUSTOMBUTTON)GetProcAddress(
      hInstDLL, TEXT("InitCustomButton"));
     /* Call the custom control initialization function
       by dereferencing the function pointer extracted by
       the previous line of code.
      */
     (*lpicb)();
    &#125;
    else
    &#123;
     MessageBox(NULL, TEXT("Could not load DLL"),
      TEXT("Custom Control Sample Error"),
      MB_ICONEXCLAMATION|MB_OK);
    &#125;
    /* Application code which registers the application
      main window class, creates the main window, etc.
      not shown
     */
    hwndExit = CreateWindow(
     CUSTOMBUTTON,
     TEXT("Exit"),
     WS_VISIBLE|WS_CHILD|CBTN_LARGEFONT,
     0,0,100,100,
     hwndMain,
     (HMENU)IDC_EXIT,
     hInstance,
     NULL);
    /* Etc. etc. */
  &#125;
</PRE>
<!-- END CODE //-->
<P>The first interesting thing in this code sample is the <I>LoadLibrary</I> call. This function loads the dynamic link library CONTROL.DLL into the client application&#146;s address space. <I>hInstDLL</I> contains an instance handle of this DLL.</P>
<P>Next, the client application gets the address of the exported DLL function <I>InitCustomButton</I>. It does this by calling <I>GetProcAddress</I>:</P>
<!-- CODE SNIP //-->
<PRE>
  lpicb = (LPINITCUSTOMBUTTON)GetProcAddress(
    hInstDLL, TEXT("InitCustomButton"));
</PRE>
<!-- END CODE SNIP //-->
<P><I>lpicb</I> is declared as type LPINITCUSTOMBUTTON. This type is defined by the client application as an alias for pointers to functions with the same signature as <I>InitCustomButton</I>. Therefore, after the <I>GetProcAddress</I> call, <I>lpicb</I> contains a pointer to the <I>InitCustomButton</I> function. The application then initializes the custom button control by calling this function by simply de-referencing this function pointer.</P><P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="294-297.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="300-303.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 + -