Windows程序设计学习——动态链接库

来源:互联网 发布:冰寒扒皮知乎 编辑:程序博客网 时间:2024/06/06 03:48

动态链接

Windows运作机制的核心是一个称作「动态链接」的概念。Windows为应用程序提供了丰富的调用函数(function calls),用于实现用户接口或文本图形显示。这些函数采用动态链接库(Dynamic Linking Library,DLL)的方式撰写。这些动态链接库是些具有.DLL或者有时是.EXE扩展名的文件,在Windows 98中通常位于\WINDOWS\SYSTEM子目录中,在Windows NT中通常位于\WINNT\SYSTEM和\WINNT\SYSTEM32子目录中。

在早期,Windows主体仅由三个动态链接库实现,分别构成Windows的三个主要子系统,即Kernel、User和GDI。当子系统的数目在Windows最近版本中增多时,大多数典型的Windows程序产生的函数调用仍对应到这三个模块。Kernel(日前由16位的KRNL386.EXE和32位的KERNEL32.DLL实现)处理所有在传统上由操作系统核心处理的事务-内存管理、文件I/O和多任务管理。User(由16位的USER.EXE和32位的USER32.DLL实作)指使用者接口,实作所有窗口运作机制。GDI(由16位的GDI.EXE和32位的GDI32.DLL实作)是一个图形设备接口,允许程序在屏幕和打印机上显示文字和图形。

Windows 98支持上千种应用程序可使用的函数调用。每个函数都有一个描述名称,例如CreateWindow。该函数(如您所猜想的)为程序建立新窗口。所有应用程序可以使用的Windows函数都在表头文件里预先声明过。

在Windows程序中,使用Windows函数调用的方式与使用如strlen等C语言链接库函数的方式相同。主要的区别在于C语言链接库函数的机器码被链接到应用程序代码中,而Windows函数调用的代码在应用程序之外的DLL中。

当您执行Windows程序时,通过「动态链接」过程与Windows相接。一个Windows的.EXE文件中包含所使用的动态链接库及函数调用的参数。当Windows程序被加载到内存中时,程序中的函数调用被定向到DLL中的函数实体,并将该函数实体加载到内存中。

当您连结Windows程序以产生一个可执行文件时,您必须连结程序开发环境提供的特定「引用链接库(import library)」。这些引用链接库包含了动态链接库名称和所有Windows函数调用的引用信息。链接程序使用该信息在.EXE文件中建立一个表格,在加载程序时,Windows使用该表格将函数调用定位到Windows函数实体。

注:没明白动态链接库与程序开发环境什么关系,貌似是“import library”包含了DLL使用的相关信息,那么,这些“import library”又是什么?在写程序的时候,这些对应程序的哪个部分?它是怎么形成“相关表格”的?

原文:

Dynamic Linking Library

Central to the workingsof Windows is a concept known as "dynamic linking." Windows providesa wealth of function calls that an application can take advantage of, mostly toimplement its user interface and display text and graphics on the videodisplay. These functions are implemented in dynamic-link libraries, or DLLs.These are files with the extension .DLL or sometimes .EXE, and they are mostlylocated in the \WINDOWS\SYSTEM subdirectory under Windows 98 and the\WINNT\SYSTEM and \WINNT\SYSTEM32 subdirectories under Windows NT.

In the early days, thegreat bulk of Windows was implemented in just three dynamic-link libraries.These represented the three main subsystems of Windows, which were referred toas Kernel, User, and GDI. While the number of subsystems has proliferated inrecent versions of Windows, most function calls that a typical Windows programmakes will still fall in one of these three modules. Kernel (which is currentlyimplemented by the 16-bit KRNL386.EXE and the 32-bit KERNEL32.DLL) handles allthe stuff that an operating system kernel traditionally handles—memorymanagement, file I/O, and tasking. User (implemented in the 16-bit USER.EXE andthe 32-bit USER32.DLL) refers to the user interface, and implements all thewindowing logic. GDI (implemented in the 16-bit GDI.EXE and the 32-bitGDI32.DLL) is the Graphics Device Interface, which allows a program to displaytext and graphics on the screen and printer.

Windows 98 supportsseveral thousand function calls that applications can use. Each function has adescriptive name, such as CreateWindow.This function (as you might guess) creates a window for your program. All theWindows functions that an application may use are declared in header files.

In your Windows program,you use the Windows function calls in generally the same way you use C libraryfunctions such as strlen. Theprimary difference is that the machine code for C library functions is linkedinto your program code, whereas the code for Windows functions is locatedoutside of your program in the DLLs.

When you run a Windowsprogram, it interfaces to Windows through a process called "dynamiclinking." A Windows .EXE file contains references to the variousdynamic-link libraries it uses and the functions therein. When a Windowsprogram is loaded into memory, the calls in the program are resolved to pointto the entries of the DLL functions, which are also loaded into memory if notalready there.

When you link aWindows program to produce an executable file, you must link with special"import libraries" provided with your programming environment. Theseimport libraries contain the dynamic-link library names and referenceinformation for all the Windows function calls. The linker uses thisinformation to construct the table in the .EXE file that Windows uses toresolve calls to Windows functions when loading the program.
原创粉丝点击