加载DLL和直接调用之间的区别 [英] difference between Load DLL and Direct Call

查看:44
本文介绍了加载DLL和直接调用之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这很愚蠢,但我听不懂

i think is very stupid, but I can't understand,

例如,我想使用Windows API,例如GetWindowsDirectory,GetSystemInfo等.我可以直接使用Api或通过GetProcAddress调用:

for example, I want use Windows API like GetWindowsDirectory, GetSystemInfo and etc... I can use Api directly or calling through GetProcAddress :

方法1 在这里,我可以使用LoadLibrary和GetProcAddress调用API:

Method 1 here I can calling APIs with LoadLibrary and GetProcAddress :

#include <windows.h>

typedef UINT (WINAPI *GET_WIN_DIR)(LPWSTR lpBuffer, UINT size);

TCHAR infoBuffer[MAX_PATH + 1];
HINSTANSE dllLoad = LoadLibrary("Kernel32.dll");
GET_WIN_DIR function = (GET_WIN_DIR )GetProcAddress(dllLoad, "GetWindowsDirectoryW");

int result = function2(infoBuffer, MAX_PATH + 1);

方法2 在这里,我可以直接调用诸如GetWindowsDirectory之类的API:

Method 2 here I can calling directly APIs like GetWindowsDirectory :

#include <windows.h>

TCHAR infoBuffer[MAX_PATH + 1];
GetWindowsDirectory(infoBuffer, MAX_PATH);

我有2个问题:

  1. 以上两种方法有什么区别?

  1. What is the difference between the two methods above?

是否将库文件加载到可执行文件上?(.exe)(我做了测试,但未更改)

is it load Library impact on executable file?(.exe)(I did test, but it'snot changed)

推荐答案

Microsoft调用

Microsoft calls

  • 方法1 ...显式链接
  • 方法2 ...隐式链接
  • Method 1 ... Explicit linking
  • Method 2 ... Implicit linking

从MSDN 将可执行文件链接到DLL :

隐式链接有时称为静态负载或负载时动态链接.显式链接有时称为动态负载或运行时动态链接.

Implicit linking is sometimes referred to as static load or load-time dynamic linking. Explicit linking is sometimes referred to as dynamic load or run-time dynamic linking.

通过隐式链接,使用DLL的可执行文件链接到DLL制造商提供的导入库(.lib文件).加载使用DLL的可执行文件时,操作系统将加载DLL.客户端可执行文件调用DLL的导出函数,就像这些函数包含在可执行文件中一样.

With implicit linking, the executable using the DLL links to an import library (.lib file) provided by the maker of the DLL. The operating system loads the DLL when the executable using it is loaded. The client executable calls the DLL's exported functions just as if the functions were contained within the executable.

通过显式链接,使用DLL的可执行文件必须进行函数调用以显式加载和卸载DLL并访问DLL的导出函数.客户端可执行文件必须通过函数指针调用导出的函数.

With explicit linking, the executable using the DLL must make function calls to explicitly load and unload the DLL and to access the DLL's exported functions. The client executable must call the exported functions through a function pointer.

可执行文件可以通过任何一种链接方法使用相同的DLL.此外,这些机制不是互斥的,因为一个可执行文件可以隐式链接到DLL,而另一个可执行文件可以显式地附加到它.

An executable can use the same DLL with either linking method. Furthermore, these mechanisms are not mutually exclusive, as one executable can implicitly link to a DLL and another can attach to it explicitly.

在我们的项目中,在任何常见情况下我们都使用隐式链接.

In our projects we use implicit linking in any common case.

我们在两种情况下例外地使用显式链接:

We use the explicit linking exceptionally in two situations:

  1. 用于在运行时显式加载的插件DLL
  2. 在特殊情况下,隐式链接函数不是正确的函数.

如果我们使用本身链接到其他DLL的不同版本(例如,来自Microsoft)的DLL,则可能会发生2 nd 情况.当然,这有点关键.实际上,我们试图防止出现第二个 nd 情况.

The 2nd case may happen if we use DLLs which themselves link to distinct versions of other DLLs (e.g. from Microsoft). This is, of course, a bit critical. Actually, we try to prevent the 2nd case.

这篇关于加载DLL和直接调用之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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