静态加载动态链接库_静态与动态动态链接库加载

静态加载动态链接库

A DLL (Dynamic Link Library) acts as a shared library of functions that can be called upon by numerous applications and other DLLs. Delphi lets you create and use DLLs so that you can call these functions at will. However, you must import these routines before you can call them.

DLL(动态链接库)充当功能的共享库,可以由众多应用程序和其他DLL调用。 Delphi使您可以创建和使用DLL,以便可以随意调用这些函数。 但是,必须先导入这些例程,然后才能调用它们。

Functions exported from a DLL can be imported in two ways—either by declaring an external procedure or function (static) or by direct calls to DLL specific API functions (dynamic).

从DLL导出的函数可以通过两种方式导入-通过声明外部过程或函数(静态)或通过直接调用DLL特定的API函数(动态)来导入。

Let's consider a simple DLL. Below is the code for "circle.dll" exporting one function, called "CircleArea," which calculates the area of a circle using the given radius:

让我们考虑一个简单的DLL。 以下是“ circle.dll”的代码,其导出一个称为“ CircleArea”的函数,该函数使用给定的半径计算圆的面积:

Once you have the circle.dll, you can use the exported "CircleArea" function from your application.

拥有circle.dll后,您可以使用应用程序中导出的“ CircleArea”功能。

静态加载 ( Static Loading )

The simplest way to import a procedure or function is to declare it using the external directive:

导入过程或函数的最简单方法是使用外部指令对其进行声明:

If you include this declaration in the interface part of a unit, circle.dll is loaded once when the program starts. Throughout execution of the program, the function CircleArea is available to all units that use the unit where the above declaration is.

如果在单元的接口部分中包含此声明,则在程序启动时会加载一次circle.dll。 在程序的整个执行过程中,CircleArea函数可用于使用上述声明所在单位的所有单位。

动态加载 ( Dynamic Loading )

You can access routines in a library through direct calls to Win32 APIs, including LoadLibrary, FreeLibrary, and GetProcAddress. These functions are declared in Windows.pas.

您可以通过直接调用Win32 API(包括LoadLibraryFreeLibraryGetProcAddress)来访问库中的例程。 这些函数在Windows.pas中声明。

Here's how to call the CircleArea function using dynamic loading:

以下是使用动态加载调用CircleArea函数的方法:

When importing using dynamic loading, the DLL is not loaded until the call to LoadLibrary. The library is unloaded by the call to FreeLibrary.

使用动态加载导入时,直到调用LoadLibrary才会加载DLL。 通过调用FreeLibrary卸载该库。

With static loading, the DLL is loaded and its initialization sections execute before the calling application's initialization sections are executed. This is reversed with dynamic loading.

对于静态加载,在执行调用应用程序的初始化部分之前,将加载DLL并执行其初始化部分。 与动态加载相反。

您应该使用静态还是动态? ( Should You Use Static or Dynamic? )

Here's a simple look at the advantages and disadvantages of both static and dynamic DLL loading:

下面简单介绍一下静态和动态DLL加载的优缺点:

Static Loading

静态加载

Pros:

优点:

  • Easier for a beginner developer; no "ugly" API calls.

    对于初学者来说更容易; 没有“难看的” API调用

  • DLLs are loaded just once, when the program starts.

    程序启动时,DLL仅加载一次。

Cons:

缺点:

  • The application will not start if any DLLs are missing or can not be found. An error message like this will appear: "This application has failed to start because 'missing.dll' was not found. Re-installing the application may fix this problem". By design, the DLL search order with static linking includes the directory from which the application loaded, the system directory, the Windows directory, and directories listed in the PATH environment variable. Note also that the search order might be different for various Windows versions. Always expect to have all the DLLs in the directory where the calling application is.

    如果缺少或找不到任何DLL,则应用程序将无法启动。 这样的错误消息将出现: “此应用程序未能启动,因为未找到'missing.dll”。重新安装该应用程序可能会解决此问题”。 通过设计,具有静态链接的DLL搜索顺序包括从中加载应用程序的目录,系统目录,Windows目录以及PATH环境变量中列出的目录。 还要注意,各种Windows版本的搜索顺序可能不同。 始终希望在调用应用程序所在的目录中具有所有DLL。

  • More memory is used since all DLLs are loaded even if you won't use some of the .functions

    由于即使不使用某些.functions也会加载所有DLL,因此会使用更多的内存。

Dynamic Loading

动态加载

Pros:

优点:

  • You can run your program even when some of the libraries it uses are not present.

    即使不存在使用的某些库,也可以运行程序。
  • Smaller memory consumption since the DLLs are used only when needed.

    较小的内存消耗,因为仅在需要时才使用DLL。
  • You can specify the full path to the DLL.

    您可以指定DLL的完整路径。
  • Could be used for modular applications. The application only exposes (loads) modules (DLLs) "approved" for the user.

    可以用于模块化应用。 该应用程序仅公开(加载)用户“批准”的模块(DLL)。
  • The ability to load and unload library dynamically, is the foundation of a plug-in system that allow a developer to add extra functionality to programs.

    动态加载和卸载库的能力是插件系统的基础,该插件系统允许开发人员向程序添加额外的功能。
  • Backwards compatibility with older Windows versions in which system DLLs might not support the same functions or be supported in the same way. Detecting the Windows version first, then dynamically linking based on what your app is running on, allows you to support more versions of Windows and provide workarounds for older OSs (or at the very least, gracefully disabling features you can't support.)

    与较旧的Windows版本的向后兼容性,在较旧的Windows版本中,系统DLL可能不支持相同的功能或以相同的方式受支持。 首先检测Windows版本,然后根据您的应用程序所运行的内容进行动态链接,使您能够支持更多版本的Windows,并为较旧的操作系统提供解决方法(或者至少可以优雅地禁用您不支持的功能)。

Cons:

缺点:

  • Requires more code, which isn't always easy for a beginner developer.

    需要更多代码,这对于初学者来说并不总是那么容易。

翻译自: https://www.thoughtco.com/static-vs-dynamic-1058452

静态加载动态链接库

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值