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

📄 ch17.htm

📁 有关Visual C++ 6.0 编程实例与技巧方面的相关资料
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD>	<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</head>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//-->                                                                </SCRIPT><link rel="stylesheet" href="/includes/stylesheets/ebooks.css">	<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">	<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 17 -- Sharing Your Functionality with Other Applications--Creating DLLs</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR>Teach Yourself Visual C++ 6 in 21 Days</H1><CENTER><P><A HREF="../ch16/ch16.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch18/ch18.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER">- 17 -<BR>Sharing Your Functionality with Other Applications--Creating DLLs</H1><H1></H1><UL>	<LI><A HREF="#Heading1">Why Create DLLs?</A>	<UL>		<LI><A HREF="#Heading2">Creating and Using DLLs</A>		<LI><A HREF="#Heading3">Designing DLLs</A>	</UL>	<LI><A HREF="#Heading4">Creating and Using an MFC Extension DLL</A>	<UL>		<LI><A HREF="#Heading5">Creating the MFC Extension DLL</A>		<LI><A HREF="#Heading6">Adapting the Test Application</A>		<LI><A HREF="#Heading7">Changing the DLL</A>	</UL>	<LI><A HREF="#Heading8">Creating and Using a Regular DLL</A>	<UL>		<LI><A HREF="#Heading9">Creating the Regular DLL</A>		<LI><A HREF="#Heading10">Adapting the Test Application</A>	</UL>	<LI><A HREF="#Heading11">Summary</A>	<LI><A HREF="#Heading12">Q&amp;A</A>	<LI><A HREF="#Heading13">Workshop</A>	<UL>		<LI><A HREF="#Heading14">Quiz</A>		<LI><A HREF="#Heading15">Exercises</A>	</UL></UL><P><HR SIZE="4"><BR>Yesterday you learned how you could create a set of functionality that might be usefulfor multiple applications and how you could package it in a library file that couldbe linked into those applications. Today you will learn how to do this same thing,only with a much more dynamic package.</P><P>Often, a family of applications will have some functionality in common. When youplace this shared functionality into DLLs instead of library modules, all the applicationscan use the same functionality with only a single copy of the functionality distributedin the form of DLLs, instead of duplicating the same functionality in each of theapplications. This method saves disk space on any systems where the applicationsare installed.</P><P>Today, you will learn</P><P><UL>	<LI>About the different types of DLLs that you can create with Visual C++ and how	to determine which type best suits your needs.	<P>	<LI>How to build two of these types of DLLs and the different approaches for the	various DLL types.	<P>	<LI>How to use the functionality for both of these types of DLLs in a Visual C++	application.	<P>	<LI>How to determine when an application needs to be relinked when you make modifications	to a DLL that is used by the application.</UL><H2><A NAME="Heading1"></A>Why Create DLLs?</H2><P>Dynamic link libraries (DLL) were introduced by Microsoft back in the early daysof Windows. DLLs are similar to library modules in that they both contain sets offunctionality that have been packaged for use by applications. The difference iswhen the applications link to the library. With a library module (LIB), the applicationis linked to the functionality in the library during the compile and build process.The functionality contained in the library file becomes part of the application executablefile. With a DLL, the application links to the functionality in the library filewhen the application is run. The library file remains a separate file that is referencedand called by the application.</P><P>There are several reasons for creating DLLs instead of library module files. First,you can reduce the size of the application executable files by placing functionalitythat is used by multiple applications into DLLs that are shared by all of the applications.You can update and modify functionality in the DLLs without having to update theapplication executable (assuming that the exported interface for the DLL doesn'tchange). Finally, you can use DLLs with just about any other Windows programminglanguage, which makes your functionality available to a wider number of programmers,not just fellow Visual C++ programmers.</P><P><H3><A NAME="Heading2"></A>Creating and Using DLLs</H3><P>DLLs are library files with compiled code that can be used by other applications.The DLLs expose certain functions and classes to these applications by exportingthe function. When a function is exported, it is added to a table that is includedin the DLL. This table lists the location of all exported functions contained inthe DLL, and it is used to locate and call each of these functions. Any functionsthat are not exported are not added to this table, and they cannot be seen or calledby any outside application or DLL.</P><P>An application can call the functions in the DLL in two ways. The more involvedmethod of calling these functions is to look up the location of the desired functionin the DLL and get a pointer to this function. The pointer can then be used to callthe function.</P><P>The other, much easier way (and the only way that you'll use in any of the examplesin this book) is to link the application with the LIB file that is created with theDLL. This LIB file is treated by the linker as a standard library file, just likethe one that you cre-ated yesterday. However, this LIB file contains stubs for eachof the exported functions in the DLL. A stub is a pseudo-function that has the samename and argument list as the real function. In the interior of the function stubis a small amount of code that calls the real function in the DLL, passing all ofthe arguments that were passed to the stub. This allows you to treat the functionsin the DLL as if they were part of the application code and not as a separate file.</P><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> The LIB file for a DLL is automatically created for the DLL during	the compiling of the DLL. There is nothing extra that you need to do to create it.<HR>	<P><HR><STRONG>TIP:</STRONG> Not only is it easier to create your applications using the LIB files	for any DLLs that you will be using, but also it can be safer when running the application.	When you use the LIB files, any DLLs that are used by your application are loaded	into memory the moment the application is started. If any of the DLLs are missing,	the user is automatically informed of the problem by Windows, and your application	does not run. If you don't use the LIB files, then you are responsible for loading	the DLL into memory and handling any errors that occur if the DLL cannot be found.<HR></BLOCKQUOTE><P>There are two types of DLLs that you can easily create using Visual C++. Thesetwo types are MFC extension DLLs and regular DLLs.</P><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> You can create other types of DLLs using Visual C++. All these other	types of DLLs involve a significant amount of ActiveX functionality, so they are	beyond the scope of this book. If you need to build ActiveX in-process server DLLs,	or other types of ActiveX DLLs, I recommend that you find an advanced book on Visual	C++ that provides significant coverage for these topics.<HR></BLOCKQUOTE><H4>MFC Extension DLLs</H4><P>MFC DLLs are the easiest to code and create because you can treat them just likeany other collection of classes. For any classes that you want to export from theDLL, the only thing that you need to add is the AFX_EXT_CLASS macro in the classdeclaration, as follows:</P><P><PRE>class AFX_EXT_CLASS CMyClass{...};</PRE><P>This macro exports the class, making it accessible to Visual C++ applications.You need to include this macro in the header file that is used by the applicationsthat will use the DLL, where it will import the class from the DLL so that it canbe used.</P><P>The one drawback to creating MFC extension DLLs is that they cannot be used byany other programming languages. They can be used with other C++ compilers as longas the compiler supports MFC (such as with Borland's and Symantec's C++ compilers).</P><P><H4>Regular DLLs</H4><P>The other type of DLL is a regular DLL. This type of DLL exports standard functionsfrom the DLL, not C++ classes. As a result, this type of DLL can require a littlemore thought and planning than an MFC extension DLL. Once inside the DLL, you canuse class

⌨️ 快捷键说明

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