【发布时间】:2011-12-04 01:53:20
【问题描述】:
共享内存在 DLL 中是如何工作的?
当 DLL 附加到进程时,它使用与进程相同的内存地址。假设我们在 DLL 中有以下函数:
int * data = 0;
int foo()
{
if (!data) data = new int(random());
return *data;
}
当进程 A 调用此函数时,它会创建新对象 (int) 并返回其值。
但是现在进程 B 附加了这个 DLL。它调用 foo() 但我不明白它是如何工作的,因为 data 正在处理中的内存空间。 B怎么能直接使用呢?
【问题讨论】:
-
进程间共享内存需要OS API支持,在Windows下可用。