gpt4 book ai didi

c++ - 在两个独立的应用程序之间共享 dll 中的堆内存

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:27:45 26 4
gpt4 key购买 nike

抱歉,如果这个问题之前已经回答过;然而,所有类似的问题似乎都与 DLL 中的全局变量或静态变量以及这些变量的共享有关。

是否可以在两个独立的应用程序之间共享一个 dll 实例?

我有两个应用程序 (appA, appB) 和一个 DLL (theDLL)。

我正在查看 appA 是否可以调用 DLL 中的函数,该函数然后在堆上创建一个变量并将其存储在 DLL 中。稍后,我想让 appB 连接到 DLL 并能够访问之前创建的那个变量。再次抱歉,如果这个答案与 dll 中的静态和全局变量相同。

这是一些伪代码:

(动态链接库)

class VariableHolder
{
public:
void StoreVariable(int x)
{
mInt = new int(x);
}

int GetVariable()
{
return mInt;
}
private:
int mInt;
}

(应用A)

int main()
{
...
(assuming we got access to a VariableHolder singleton created in theDLL)
theVarialbeHolder.StoreVariable(5);
...
}

(应用B)

int main()
{
...
(assuming we got access to a VariableHolder singleton created in theDLL)
if (theVarialbeHolder.GetVariable() == 5)
{
cout << "Hurray, how did you know?";
}
...
}

最佳答案

这完全是不可能的——因为两个进程的地址空间不同(因为它们是虚拟的,由内核创建),所以一个有效的指针在另一个进程中不起作用。但是,您可以使用共享内存在进程之间传输原始标量数据(字符串、整数)- here's how.

关于c++ - 在两个独立的应用程序之间共享 dll 中的堆内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11964483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com