目前,我有一个系统,具有以下简化视图。具有来自同一DLL的全局变量的多个实例
The entire system run under single process
------------------------------------------
--- DLL0.DLL --- COMMON.DLL (contains global_variable in COMMON.DLL)
EXE ---|
--- DLL1.DLL --- COMMON.DLL (contains global_variable in COMMON.DLL)
为COMMON.DLL的源代码是如下。
// COMMON.DLL
#ifdef COMMON_EXPORTS
_declspec(dllexport) int global_variable = 100;
// Function used to access and print global_variable.
__declspec(dllexport) void common_fun_which_access_global_variable();
#else
_declspec(dllimport) int global_variable;
__declspec(dllimport) void common_fun_which_access_global_variable();
#endif
DLL0.DLL的源代码如下。
__declspec(dllexport)
void DLL0() {
printf ("This is DLL0\n");
printf ("In DLL0, global_variable is %i\n", global_variable);
common_fun_which_access_global_variable();
global_variable = 200;
printf ("DLL0 is now setting global_variable to 200\n");
common_fun_which_access_global_variable();
}
DLL1.DLL的来源如下。
__declspec(dllexport)
void DLL1() {
printf ("This is DLL1\n");
printf ("In DLL1, global_variable is %i\n", global_variable);
common_fun_which_access_global_variable();
global_variable = 400;
printf ("DLL1 is now setting global_variable to 400\n");
common_fun_which_access_global_variable();
}
EXE的源代码如下。
HINSTANCE instance0 = AfxLoadLibrary(_T("DLL0.dll"));
FARPROC fun0 = GetProcAddress(instance0, "DLL0");
HINSTANCE instance1 = AfxLoadLibrary(_T("DLL1.dll"));
FARPROC fun1 = GetProcAddress(instance1, "DLL1");
_fun0();
_fun1();
的输出如下。
This is DLL0
In DLL0, global_variable is 100
In COMMON, global_varialbe is 100
DLL0 is now setting global_variable to 200
In COMMON, global_varialbe is 200
This is DLL1
In DLL1, global_variable is 200 <-- I wish 100 is being printed.
In COMMON, global_varialbe is 200 <-- I wish 100 is being printed here too.
<-- I wish DLL0 and DLL1 have their own instance of
<-- global_variable respectively.
DLL1 is now setting global_variable to 400
In COMMON, global_varialbe is 400
整个系统在单个过程中执行。虽然DLL0.DLL
和DLL1.DLL
正在被明确加载,但依赖项COMMON.DLL
将只在整个应用程序生命周期中加载一次。 EXE不会加载两次相同的COMMON.DLL
。
有没有什么办法,我可以在不违反规则的情况下实现以下目标?
- DLL0和DLL1可以有自己的实例
global_variable
? global_variable
必须是全球的,并重新在里面COMMON.DLL
?COMMON.DLL
将通过使用LIB文件的隐式链接进行加载。- 无重命名
COMMON.DLL
至COMMON-DLL0.DLL
和COMMON-DLL1.DLL
。 - 没有静态链接。
- 如果DLL0更改值
global_variable
,则从DLL0调用common_fun_which_access_global_variable
应该访问DLL0的更改值。但是,从DLL1调用common_fun_which_access_global_variable
不应该实现这些更改。
**我知道这太多了。但我现在正在处理遗留代码。你知道:)
并排组装能否解决这类问题?我的理解是,并排组装用于解决具有相同名称但不同版本问题的多个DLL。我不确定它是否适用于我的上述情况?
或者,我应该问一个问题吗?我们如何可以在同一个EXE中加载2个COMMON.DLL实例?
对,它太多了。你无法完成这项工作。放弃子弹4 – 2011-03-29 03:25:14