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

📄 ch19.htm

📁 delphi自学的好教材!特别适合刚刚起步学习delphi的人员!同样对使用者具有参考价值!
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><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 HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">	<TITLE>Teach Yourself Borland Delphi 4 in 21 Days -- Ch 19 -- Creating and Using DLLs</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"></H1></CENTER><CENTER><H1><BR>Teach Yourself Borland Delphi 4 in 21 Days</H1></CENTER><CENTER><P><A HREF="../ch18/ch18.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch20/ch20.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><CENTER><H1>- 19 -</H1></CENTER><CENTER><H1>Creating and Using DLLs</H1></CENTER><UL>	<LI><A HREF="#Heading1">DLL Overview</A>	<UL>		<LI><A HREF="#Heading2">What Is a DLL?</A>		<LI><A HREF="#Heading3">Why Should You Use a DLL?</A>		<LI><A HREF="#Heading4">Internationalization</A>	</UL>	<LI><A HREF="#Heading5">Anatomy of a DLL Unit</A>	<LI><A HREF="#Heading6">The Basics of DLL Writing</A>	<UL>		<LI><A HREF="#Heading7">Functions and Procedures in DLLs</A>	</UL>	<LI><A HREF="#Heading8">The exports Keyword</A>	<UL>		<LI><A HREF="#Heading9">Using a DLLProc</A>	</UL>	<LI><A HREF="#Heading10">Loading DLLs</A>	<UL>		<LI><A HREF="#Heading11">Static Loading</A>		<LI><A HREF="#Heading12">Dynamic Loading</A>	</UL>	<LI><A HREF="#Heading13">Calling Functions and Procedures Located in DLLs</A>	<UL>		<LI><A HREF="#Heading14">Calling Using Static Loading</A>		<LI><A HREF="#Heading15">Calling Functions and Procedures Using Dynamic Loading</A>	</UL>	<LI><A HREF="#Heading16">Creating a DLL Project with the Object Repository</A>	<LI><A HREF="#Heading17">Using Forms in DLLs</A>	<UL>		<LI><A HREF="#Heading18">Writing a DLL Containing a Form</A>		<LI><A HREF="#Heading19">Calling an MDI Form in a DLL</A>	</UL>	<LI><A HREF="#Heading20">Using Resources in DLLs</A>	<UL>		<LI><A HREF="#Heading21">Creating a Resource DLL</A>		<LI><A HREF="#Heading22">Putting the Resource DLL to Work</A>	</UL>	<LI><A HREF="#Heading23">Summary</A>	<LI><A HREF="#Heading24">Workshop</A>	<UL>		<LI><A HREF="#Heading25">Q&amp;A</A>		<LI><A HREF="#Heading26">Quiz</A>		<LI><A HREF="#Heading27">Exercises</A>	</UL></UL><P><HR SIZE="4"><CENTER><H1></H1></CENTER><P>If you use Windows, you can't escape dynamic link libraries (DLLs). Just takea look in your \Windows and \Windows\System directories. I run Windows NT 4, andI count more than 650 DLLs in the \Winnt directories on my system. So, let's notdebate the facts. DLLs are part of everyday life in Windows.</P><P>The question, then, is whether <I>you</I> need to create and use DLLs. That'swhat you find out today. Specifically, you will look at the following:</P><UL>	<LI>What DLLs are and why you use them	<P>	<LI>Writing DLLs	<P>	<LI>Calling function and procedures in DLLs	<P>	<LI>Using forms in DLLs	<P>	<LI>Using resource DLLs</UL><P>By the end of today you will have a good idea of whether DLLs are for you. I suspectthat DLLs will fit into your development plan somewhere down the road.</P><P><H2><A NAME="Heading1"></A>DLL Overview</H2><P>Simply put, DLLs are very useful. Today I discuss the benefits of DLLs and howthis translates for you, the Delphi programmer. First, let's explore exactly whata DLL is.</P><P><H3><A NAME="Heading2"></A>What Is a DLL?</H3><P><strong>New Term:</strong> A <I>DLL</I> (dynamic link library) is one or more piecesof code stored in a file with a .dll extension.</P><P>The DLL code can be called from executable programs, but the DLL itself isn'ta stand-alone program. You can consider the DLL a helper file for a main program.Applications that use code in DLLs are called <I>calling applications</I> becausethey call functions or procedures in the DLL.</P><P>There are two types of DLLs: code DLLs and resource DLLs. This is not an exclusivearrangement. You can have both code and resources in the same DLL, and there's noproblem with that whatsoever. In some cases, though, it's convenient to store yourcode in one DLL and your resources in another. I'll talk about why you might wantto do that in the section &quot;Using Resources in DLLs.&quot;</P><P>Code contained in a DLL can be of two primary forms. The first form is a standalonefunction that you call from your main application. You have already done that severaltimes in this book. Remember the code from Day 14, &quot;Advanced Programming,&quot;as follows:</P><P><PRE>Screen.Cursors[myCursor] := LoadCursor(HInstance, `MYCURSOR');</PRE><P>or this, also from Day 14:</P><P><PRE>DrawText(Canvas.Handle, Panel.Text,  -1, Temp, DT_CENTER or DT_VCENTER or DT_SINGLELINE);</PRE><P>Both LoadCursor and DrawText are Windows API functions. You probably knew that.But did you know that these functions are called from DLLs? When you think aboutit, these functions have to be stored somewhere. But where? In this case, both LoadCursorand DrawText are contained in the Windows DLL called User32.dll.</P><P>And what about all those common dialog boxes you've been using, such as the FileOpen dialog box, the Print dialog box, and the Printer Setup dialog box? They haveto come from somewhere. In this case, these dialog boxes are stored as resourcesin the file called Comctl32.dll. So, without knowing it, you have been using DLLsall along.</P><P>The second form of code contained in a DLL consists of procedures and functionsused internally by the DLL. These functions and procedures perform some task in theDLL and are not visible to the outside world (applications using the DLL).</P><P>Using DLLs is one thing, but using and creating your own DLLs is an entirely differentmatter, right? Not really. After you create a DLL, you can call functions and proceduresin the DLL just as you do when calling Windows API functions. Creating DLLs is notdifficult, so there's nothing to stop you from putting DLLs to work for you.</P><P><H3><A NAME="Heading3"></A>Why Should You Use a DLL?</H3><P>That's a good question. After all, what does a DLL give you? DLLs offer thesebenefits:</P><UL>	<LI>Effective code reuse	<P>	<LI>Capability to easily share code among many applications	<P>	<LI>Capability to compartmentalize code	<P>	<LI>Internationalization of your application	<P>	<LI>Effective use of Windows resources</UL><P>Let's take a look at these points individually and see whether I can convinceyou what a good thing DLLs are.</P><P><H4>Effective Code Reuse</H4><P>Code reuse is a big part of object-oriented programming. After all, why reinventthe wheel? DLLs aid in code reuse in a big way. Let's say you have a large amountof code you wrote to handle a specific task in Windows. You've worked hard to writethat section of code, so it would be nice if you could reuse it in any of your applicationsthat need it. DLLs enable you to do exactly that.</P><P>What you do is compile all of your code into a DLL. Then all you have to do inorder to use the code in any of your programs is basically load the DLL from an applicationand begin using the code. What could be easier? (Actually, there are a couple othersteps required, but they are simple and I don't want to get ahead of myself by explainingthem now.)</P><P>Now you have this code at your fingertips any time you need it. Better yet, youcan give the DLL to your programming pals, and they can use the code, too. Hey, thisis starting to sound good. Better yet, you can sell the DLL to other programmers.You put all that work into it, so you might as well get out of it what you can!</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> You can easily create DLLs that can be called from applications written	in C++Builder, Visual Basic, Visual C++, or other programming environments. Of course,	this depends on the code contained in the DLL and how that code is called, but it	can be done. <HR></BLOCKQUOTE><P>So, do you see where this is heading? After writing a DLL, you can use it withease whenever and wherever you want. Getting at all the goodies contained in yourDLL is just a few mouse clicks away.</P><P><H4>Sharing Code Among Applications</H4><P>Sharing code ties in with code reuse but takes it a step farther. Let's say thatyou are a programmer for a large corporation. You might have hundreds of users, eachwith his or her own system. (I'll ignore networking considerations for this example.)Let's say that you've written five applications for those users. Further, let's assumethat each of those five applications uses a common set of code that compiles to 100KB(a modest figure). If you don't use a DLL, you will have 100KB of code repeated fivetimes (once for each of the five applications) for a total of 500KB of code. That'scode waste, folks.</P><P>A better approach is to put the classes in a DLL. Each of the five programs canuse the same DLL for the common code. That's part of the beauty of DLLs: After theDLL is written, all your applications can share it. Each user will get an additional100KB DLL on his or her machine, but each of the five applications will be reducedin size by 100KB. The bottom line is that each user saves 400KB of disk space. Saving400KB might not sound like a lot, but if you multiply it by hundreds of users, itstarts to add up from a corporate perspective (bean counters tend to look at thosetypes of issues). This is just a simple example. In a real-world situation, you couldeasily save several megabytes per user by using DLLs.</P><P>What if three of the five programs are running simultaneously? No problem. Eachprogram pulls code from the DLL as needed, and there are no conflicts. Windows keepstrack of who's calling whom and makes sure that it all works together. All you dois write the DLL and start using it. (If this discussion sounds familiar, it is probablybecause I used roughly this same argument when discussing runtime packages on Day8, &quot;Creating Applications in Delphi.&quot;)</P><P><H4>Compartmentalizing Code</H4><P>By keeping your code compartmentalized, you can deal with it more easily whenit comes time to update your code. I am not advocating a separate DLL for each aspectof your program, but when and where it is sensible to break up your program intorelated chunks of code, you should do so. There isn't much benefit in breaking anapplication into DLLs just for the sake of compartmentalizing the code. If you havelibraries of common code that you have written, though, it does make sense to keepthose libraries in separate DLLs. Again, this is most obvious when creating librariesthat can be used by any of your programs.</P><P>One advantage to this approach is that your applications can be more easily upgradedif necessary. Let's face it. Even the best of programs go to market with bugs. It'smore a question of how many or how severe than a question of whether there are bugsat all. If you discover bugs in your DLL, you can fix the bug and ship a new DLLto your users rather than recompile and ship an entire executable file.</P><P>At first this might not appear to benefit the types of applications you have writtenso far. However, you might eventually write programs several megabytes in size withdozens of source modules. In that case, compartmentalizing makes much more sense.</P><P><H3><A NAME="Heading4"></A>Internationalization</H3><P>A few years ago, you could write a program and not worry about the internationalmarket. You would write the program and create menu items, dialog boxes, hint text,error messages, and so forth in your native language, and that was that. You putthe program on the market and didn't think about it again.</P><P>The world, however, is becoming smaller. With the explosion of the Internet andthe World Wide Web, things are completely different than they were just a coupleof years ago. You can create a demo of your program or maybe a shareware version,and put it on the Internet. Within hours, minutes even, people all around the worldhave access to your program. It's exciting and frightening at the same time. Thisphenomenon means that you must plan ahead and prepare for translating your programinto other languages.</P><P>One way you can do this is to create resource DLLs that contain string resourcesin various languages. You can have a separate DLL for each language, or as an alternative,you can have all the string resources in a single DLL and load the correct languageversion of each string at runtime. At runtime, you can use the Windows API functionLoadString to load the strings and assign them to various components, as needed.</P><BLOCKQUOTE>	<P><HR><strong>NOTE:</strong> One disadvantage to the Delphi programming model is that it doesn't	use traditional resources as other programming environments do. Resources such as	menus are not loaded as resources but rather are contained within the form's resource.	This makes internationalization more difficult and is one of the few times that this	model works against you. <HR></BLOCKQUOTE><P>Again, planning ahead can save you a lot of work later. If you plan your applicationwith internationalization in mind, it is much easier to translate your applicationinto other languages when necessary.</P><P><H4>Effective Use of Windows Resources</H4><P>Today's systems are faster, have more RAM, and have more hard disk space thanever before. It's easy to fall into the mode of &quot;Hey, I'm only using 2MB ofsystem RAM, so what's the big deal?&quot; The truth is, you should always be consciousof how much of your users' system resources you are consuming. Here again, DLLs canhelp. This is really a continuation of the discussion on sharing code among differentapplications.</P><P>Let's go back to the example I used earlier. (Remember, you have five applicationsusing some common code.) If you don't use DLLs, when several of your programs arerunning at the same time, they will all load some of the same code into memory. Youare wasting system resources because each program loads its own copy of the exactsame code. Rather than have each program load the same code, you use a DLL and loadthe code once. All your applications can use the same code in memory and reduce the

⌨️ 快捷键说明

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