DLL Basics - DLL基础

Dynamic-link libraries (DLLs) have been the cornerstone of Microsoft Windows since the first version of the operating system. All the functions in the Windows API are contained in DLLs. The three most important DLLs are Kernel32.dll, which contains functions for managing memory, processes, and threads; User32.dll, which contains functions for performing user-interface tasks such as window creation and message sending; and GDI32.dll, which contains functions for drawing graphical images and displaying text.

自从Microsoft公司推出第一个版本的Windows操作系统以来,动态链接库(DLL)一直是这个操作系统的基础。WindowsAPI中的所有函数都包含在DLL中。3个最重要的DLL是Kernel32.dll,它包含用于管理内存、进程和线程的各个函数;User32.dll,它包含用于执行用户界面任务(如窗口的创建和消息的传送)的各个函数;GDI32.dll,它包含用于画图和显示文本的各个函数。

Windows also comes with several other DLLs that offer functions for performing more specialized tasks. For example, AdvAPI32.dll contains functions for object security, registry manipulation, and event logging; ComDlg32.dll contains the common dialog boxes (such as File Open and File Save); and ComCtl32.DLL supports all of the common window controls.

Windows还配有若干别的DLL,它们提供了用于执行一些特殊任务的函数。例如,AdvAPI32.dll包含用于实现对象安全性、注册表操作和事件记录的函数;ComDlg32.dll包含常用对话框(如FileOpen和FileSave);ComCtl32.DLL则支持所有的常用窗口控件。

In this chapter, you'll learn how to create DLLs for your own applications. Here are some reasons for using DLLs:

本章将要介绍如何为应用程序创建DLL。下面是为什么要使用DLL的一些原因:

  • They extend the features of an application. Since DLLs can be dynamically loaded into a process's address space, an application can determine at run time what actions to perform and then load the code to execute those actions on demand. For example, a DLL is useful when one company creates a product and wants to allow other companies to extend or enhance the product.
  • 它们扩展了应用程序的特性。由于DLL能够动态地装入进程的地址空间,因此应用程序能够在运行时确定需要执行什么操作,然后装入相应的代码,以便根据需要执行这些操作。例如,当一家公司开发了一种产品,想要让其他公司改进或增强该产品的功能时,那么就可以使用DLL。
  • They can be written in many programming languages. You can choose the best language for the job at hand. Perhaps your application's user interface is best programmed with Microsoft Visual Basic, but the business logic is better handled by C++. The system allows a Visual Basic program to load a C++ DLL, a Cobol DLL, a Fortran DLL, and so on.
  • 它们可以用许多种编程语言来编写。可以选择手头拥有的最好的语言来编写DLL。也许你的应用程序的用户界面使用Microsoft Visual Basic编写得最好,但是用C++来处理它的商用逻辑更好。系统允许Visual Basic程序加载C++ DLL、Cobol DLL和Fortran DLL等。
  • They simplify project management. If different groups work on different modules during the development process, the project is easier to manage. However, an application should ship with as few files as possible. I know of one company that shipped a product with one hundred DLLs—up to five DLLs per programmer. The application's initialization time was horribly slow because the system had to open one hundred disk files before the program could do anything.
  • 它们简化了软件项目的管理。如果在软件开发过程中不同的工作小组在不同的模块上工作,那么这个项目管理起来比较容易。但是,应用程序在销售时附带的文件应该尽量少一些。我知道有一家公司销售的产品附带了100个DLL——每个程序员最多有5个DLL。这样,应用程序的初始化时间将会长得吓人,因为系统必须打开100个磁盘文件之后,程序才能执行它的操作。
  • They help conserve memory. If two or more applications use the same DLL, the DLL has its pages in RAM once and the pages are shared by all of the applications. The C/C++ run-time library is a perfect example. Many applications use this library. If all these applications link to the static library, the code for functions such as sprintf, strcpy, malloc, and so on exist in memory multiple times. However, if all these applications link to the DLL C/C++ run-time library, the code for these functions is in memory only once, which means that memory is used more efficiently.
  • 它们有助于节省内存。如果两个或多个应用程序使用同一个DLL,那么该DLL的页面只要放入RAM一次,所有的应用程序都可以共享它的各个页面。C/C++运行期库就是个极好的例子。许多应用程序都使用这个库。如果所有的应用程序都链接到这个静态库,那么sprintfstrcpymalloc等函数的代码就要多次存在于内存中。但是,如果所有这些应用程序链接到DLLC/C++运行期库,那么这些函数的代码就只需要放入内存一次,这意味着内存的使用将更加有效。
  • They facilitate resource sharing. DLLs can contain resources such as dialog box templates, strings, icons, and bitmaps. Multiple applications can use DLLs to share these resources.
  • 它们有助于资源的共享。DLL可以包含对话框模板、字符串、图标和位图等资源。多个应用程序能够使用DLL来共享这些资源。
  • They facilitate localization. Applications frequently use DLLs to localize themselves. For example, an application that contains only code and no user interface components can load the DLL containing localized user interface components.
  • 它们有助于应用程序的本地化。应用程序常常使用DLL对自己进行本地化。例如,只包含代码而不包含用户界面组件的应用程序可以加载包含本地化用户界面组件的DLL。
  • They help resolve platform differences. The various versions of Windows offer different functions. Frequently, developers want to call new functions if they exist on the host version. However, if your source code contains a call to a new function and your application is about to run on a version of Windows that doesn't offer that function, the operating system loader will refuse to run your process. This is true even if you never actually call the function. If you keep these new functions in a DLL, however, applications can load on an older version of Windows. Of course, you still cannot successfully call the function.
  • 它们有助于解决平台差异。不同版本的Windows配有不同的函数。开发人员常常想要调用新的函数(如果它们存在于主机的Windows版本上的话)。但是,如果你的源代码包含了对一个新函数的调用,而你的应用程序将要在不能提供该函数的Windows版本上运行,那么操作系统的加载程序将拒绝运行你的进程。即使你实际上从不调用该函数,情况也是这样。如果将这些新函数保存在DLL中,那么应用程序就能够将它们加载到Windows的老版本上。当然,你仍然可以成功地调用该函数。
  • They can serve special purposes. Windows makes certain features available only to DLLs. For example, you can install certain hooks (set using SetWindowsHookEx and SetWinEventHook) only if the hook notification function is contained in a DLL. You can extend Windows Explorer's shell by creating COM objects that must live inside a DLL. The same is true for ActiveX controls that can be loaded by a Web browser to create rich Web pages.
  • 它们可以用于一些特殊的目的。Windows使得某些特性只能为DLL所用。例如,只有当DLL中包含某个挂钩通知函数的时候,才能安装某些挂钩(使用SetWindowsHookEx和SetWinEventHook来进行安装)。可以通过创建必须在DLL中生存的COM对象来扩展Windows Explorer的外壳程序。对于可以由Web浏览器加载的、用于创建内容丰富的Web页的ActiveX控件来说,情况也是一样。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值