程序使用的DLL,其中声明的变量存储在哪里?

发布于 2024-11-14 16:23:10 字数 211 浏览 2 评论 0 原文

我有一个程序(不是我的,没有源代码),它公开了一个接口,这样我就可以编写一个将由我的程序调用的 DLL。现在我想知道当我在我创建的这个 DLL 中声明某个变量时,它将存储在什么内存空间中?

我的意思是,它只是位于 EXE 地址空间的内存空间中,对吗? DLL 是如何加载到 EXE 中的呢?我认为 DLL 只在内存中加载一次,那么这与我在 DLL 中创建局部变量有何关系呢? (如对象、类等)

I have a program (not mine, have no source code) which exposes an interface so I can write a DLL which will be called by my program. Now I wondered when I declare some variable in this DLL I make, in what memory space is this going to be stored?

I mean, it's just gonna sit in the memory space of the EXE's address space, right? How is the DLL loaded in regards to the EXE though? I thought a DLL was only ever loaded in memory once, so how does that work in relation to me creating local variables in my DLL? (like objects, classes etc)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

又爬满兰若 2024-11-21 16:23:10

每个进程加载 DLL 一次。曾几何时,DLL 是在进程之间共享的,但自从 Windows 3.1 走上渡渡鸟之路以来,情况就不再如此了。

您在 DLL 中声明的任何全局变量都将存储在数据页中。请注意,与 EXE 的全局变量不同的页面。

现在,如果您在堆上分配内存,您的分配是否与 EXE 混合取决于您使用的堆。如果 EXE 和 DLL 使用作为 DLL 链接的相同运行时,那么它们都将从同一堆获取内存。如果它们具有不同的运行时,或者静态链接到运行时,它们将获得不同的堆。这变成了一个非常大的蠕虫罐头,所以我不会在这里继续下去。

A DLL is loaded once per process. Once upon a time DLLs were shared between processes, but that hasn't been the case since Windows 3.1 went the way of the dodo.

Any global variables that you declare in your DLL will be stored in a data page. A different page from the EXE's global variables, mind.

Now, if you allocate memory on the heap, whether or not your allocations are mixed in with the EXEs depend on which heap you use. If both EXE and DLL use the same runtime linked as a DLL then they will both get memory from the same heap. If they have different runtimes, or link against runtime statically, they'll get different heaps. This becomes a very big can of worms, so I shan't go any further here.

×纯※雪 2024-11-21 16:23:10

您的 DLL 将声明一个 DllMain,它相当于常规可执行文件中的入口点。当你的 DLL 被加载时,你的 DLLMain 就会被调用。 此处是应采取的最佳做法的链接在那里。

通常你会在那里进行某种初始化。当您的 DLL 被加载时,它被加载到调用 LoadLibrary 的可执行文件的虚拟内存空间中。 LoadLibrary 处理所有需要处理的映射和重定位。从此时起,您通过 DLL 分配或修改的所有内存都与其映射到的进程位于同一虚拟内存空间中。

大概是通过加载 DLL 然后调用其中的某种导出函数来实现可执行接口。基本上,一旦 DLL 被加载,您所做的一切都将在它被加载的进程的内存空间内进行。

如果您想详细了解加载 DLL 时发生的情况,您应该查看 LoadLibrary()

Your DLL will declare a DllMain which is the equivalent to the entry point in a regular executable. When your DLL is loaded your DLLMain gets called. Here is a link to the best practices of what should be done in there.

Usually you will do some sort of intialisation there. When your DLL is loaded, it is loaded into the virtual memory space of the executable that called LoadLibrary. LoadLibrary handles all the mapping and relocations that need to be dealt with. From this point all memory you allocate or modify through your DLL is in the same virtual memory space as the process it's mapped into.

Presumably the executable interfaces by loading your DLL then calling some sort of exported function in it. Basically everything that you do once your DLL is loaded will be within the memory space of the process it is loaded into.

If you want to know more about exactly what goes on when your DLL is loaded you should look into the semantics of LoadLibrary().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文