动态加载库和运行时误解的显式链接

Explicit Linking of Dynamic Load Library and Runtime misconception

本文关键字:链接 误解 运行时 加载 动态      更新时间:2023-10-16

我在Windows中使用DLL。我创建了其中一个,并且我可以成功地将我的客户端程序链接到 DLL。但我有一个误解。当我阅读有关DLL的文章时,有一句话被强调,当DLL加载到内存中时,所有程序实例都可以使用它。因此,它导致我们有效地使用内存,并且从未发生过代码重复。

所以我写了一个程序,它可以成功地加载一个DLL并使用它。 当程序运行时,我在另一个路径中执行了前一个程序的示例,DLL 不存在,但是当我运行第二个程序时,它显示错误,DLL 不加载。

我的假设是当第一个程序将 DLL 加载到内存中时,内存中存在它的一个实例,所以我应该再次使用它,但它没有发生。所以我想知道多个程序如何使用 DLL 的实例?我应该如何实现一个示例来测试此行为?程序必须在自身的路径中包含 DLL 的示例吗?

对不起,英语说得不好,我不得不提一下,我是一个新手程序员,不是专业人士。对不起,如果你发现这个问题如此愚蠢。这是我的代码:

程序.cpp

#include <Windows.h>
#include <iostream>
#include <string>
typedef void(__cdecl *PtrSetInformation)(std::string, std::string, int);
typedef void(__cdecl *PtrShowInformation)(void);
auto main() -> int {
HINSTANCE HandlerInstance = LoadLibrary(TEXT("LibEngine.dll"));
if (!HandlerInstance) {
std::cout << "DLL doesn't load successfuly." << std::endl;
}
else {
std::cout << "Dll is loaded successfuly." << std::endl;
}
PtrSetInformation OSetInformation = reinterpret_cast<PtrSetInformation>(GetProcAddress(HandlerInstance, "SetInformation"));
PtrShowInformation OShowInformation = reinterpret_cast<PtrShowInformation>(GetProcAddress(HandlerInstance, "ShowInformation"));
if (!OSetInformation || !OShowInformation) {
std::cout << "Function pointers doesn't initiliazed successfuly." << std::endl;
}
else {
OSetInformation("Mikhail", "Razborov", 24);
OShowInformation();
}
std::cin.get();
return 0;
}

我的 DLL 代码:

#include <iostream>
#include <string>
std::string __name;
std::string __family;
int __age;
extern "C" {
__declspec(dllexport) void __cdecl SetInformation(std::string arg_name, std::string arg_family, int arg_age) {
__name = arg_name;
__family = arg_family;
__age = arg_age;
}
__declspec(dllexport) void __cdecl ShowInformation() {
std::cout << "Your name is " << __name << " " << __family << std::endl;
std::cout << "You are a " << __age << " year old programmer." << std::endl;
}
}

即使 DLL 的内存中映像可能已共享(并非总是如此),Windows 在加载.exe时仍需要访问磁盘上的副本。 这是因为您可能在不同的目录中有两个具有相同名称的不同 DLL,并且 Windows 将每个 DLL 视为单独的实体。

地址空间布局随机化 (ASLR) 的出现改变了进程之间共享 DLL 代码的目标。 Raymond Chen在博客上对此进行了广泛的讨论,例如在这里。