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++執行期庫就是個極好的例子。許多應用程式都使用這個庫。如果所有的應用程式都連結到這個靜態庫,那麼sprintf、strcpy和malloc等函式的程式碼就要多次存在於記憶體中。但是,如果所有這些應用程式連結到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控制元件來說,情況也是一樣。