Delphi静态或动态链接到windows dll [英] Delphi link to windows dll statically or dynamically

查看:226
本文介绍了Delphi静态或动态链接到windows dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道在加载时间内隐式链接到库可能会导致性能提高,所以我想知道如果是编译时这样做的良好做法,从而增加可执行大小(诚然,这只是边际的)比较在运行时显式链接。我的问题是当链接到位于System32的Microsoft Windows dll文件时,在加载时链接是否更好,因为您可以大部分确定这些库将出现或按照明确的方法?



使用的语言是Delphi(pascal),有问题的库是WTsAPI32.dll - 终端服务。



编辑:强>如所指出的那样 - 我选择的语言是不正确的,并且已被修改。另外,由于在Unix中只有真正的链接库,所以我可以省略对可执行文件大小的评论,我相信在我实际上是指将库代码捆绑到可执行文件中的静态链接,现在我意识到使用dll文件(DUH!)是不可能的。感谢所有。

解决方案

两种形式的DLL链接可能更好地称为隐式和显式。隐式连接是您所指的静态链接。而显式连接就是你所指的运行链接。



对于隐式链接,链接器将条目写入可执行文件的导入表中。此导入表是加载程序用于在模块加载时解析DLL导入的元数据。每个隐式导入都包含一个stub函数,该引用只有几个字节大小。隐式链接的可执行大小含义可以忽略不计。



通过显式链接,导入的函数的地址通过调用GetProcAddress来解决。这个调用是在程序员选择时进行的。如果DLL或函数无法解析,程序员可以编写回退行为。对于我估计与隐式链接相似的显式链接,有大小的影响。如果功能地址被评估一次并且在调用之间记住,则性能特征与隐式链接相似。



我的建议如下:


  1. 喜欢隐式链接。代码更方便。

  2. 如果DLL可能不存在,请使用显式链接。

  3. 如果DLL必须使用完整的路径,使用显式链接。

  4. 如果要在程序执行期间卸载DLL,请使用显式链接。

您特别提到Windows DLL。你可以放心地假设他们会在场。不要尝试代码来允许程序运行,以防万一user32.dll丢失。旧版本的Windows中可能不存在某些功能。如果您支持这些旧版本,则需要使用显式链接并提供后备。决定您支持哪个版本并使用MSDN确保您的最低支持平台上的功能可用。


I am aware that implicitly linking to libraries at load time can lead to performance increases and as such I was wondering if it was good practice to link in this way at compile time thus increasing executable size (admittedly this is only marginal) compared to linking explicitly at runtime. My question is when linking against Microsoft Windows dll files located in System32, is it 'better' to link at load time as you can be mostly certain that the libraries will be present or follow the explicit approach?

Language used is Delphi (pascal) and the library in question is the WTsAPI32.dll - Terminal Services.

EDIT: As pointed out - my choice of language was incorrect and has been amended. Also, due to having only really every extensively linked to libraries in Unix, my comments about executable size can be omitted, I believed at the time I WAS in fact referring to static linking which bundles the library code into the executable and I now realise this is impossible when using dll files (DUH!). Thanks all.

解决方案

The two forms of DLL linking are perhaps better named implicit and explicit. Implicit linking is what you refer to as static linking. And explicit linking is what you refer to as runtime linking

For implicit linking the linker writes entries into the import table of the executable file. This import table is metadata that is used by the loader to resolve DLL imports at module load time. A stub function is included for each implicit import that is only a few bytes in size. The executable size implications of implicit linking are negligible.

With explicit linking the imported function's address is resolved by a call to GetProcAddress. This call is made when the programmer chooses. If the DLL or the function cannot be resolved, the programmer can code fall back behaviour. There are size implications to explicit linking that I estimate to be similar to implicit linking. If the function address is evaluated once and remembered between calls then the performance characteristics are similar to implicit linking.

My advice is as follows:

  1. Prefer implicit linking. It is more convenient to code.
  2. If the DLL may not be present, use explicit linking.
  3. If the DLL must be loaded using a full path, use explicit linking.
  4. If you want to unload the DLL during program execution, use explicit linking.

You specifically mention Windows DLLs. You can safely assume that they will be present. Don't try to code to allow your program to run in case user32.dll is missing. Some functions may not be present in older versions of Windows. If you support those older versions you'll need to use explicit linking and provide a fallback. Decide which version you support and use MSDN to be sure that a function is available on your minimum supported platform.

这篇关于Delphi静态或动态链接到windows dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆