本文介绍了拥有来自同一个DLL的全局变量的多个实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 目前,我有一个具有以下简化视图的系统。 整个系统在单个进程下运行 ------------------------------------------ --- DLL0 .DLL --- COMMON.DLL(在COMMON.DLL中包含global_variable) EXE --- | --- DLL1.DLL --- COMMON.DLL(在COMMON.DLL中包含global_variable) COMMON.DLL的源代码如下。 / COMMON.DLL #ifdef COMMON_EXPORTS _declspec(dllexport)int global_variable = 100; //用于访问和打印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\\\); printf(在DLL0中,global_variable是%i \\\,global_variable); common_fun_which_access_global_variable(); global_variable = 200; printf(DLL0现在将global_variable设置为200 \\\); common_fun_which_access_global_variable(); } DLL1.DLL的源代码如下。 __ declspec(dllexport) void DLL1(){ printf(This is DLL1\\\); printf(在DLL1中,global_variable是%i \\\,global_variable); common_fun_which_access_global_variable(); global_variable = 400; printf(DLL1现在将global_variable设置为400 \\\); 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(); 输出如下。 这是DLL0 在DLL0中,global_variable为100 在COMMON中,global_varialbe为100 DLL0现在将global_variable设置为200 在COMMON中,global_varialbe是200 这是DLL1 在DLL1中,global_variable为200 在COMMON,global_varialbe是200 < - 我希望DLL0和DLL1分别有自己的< - global_variable实例。 DLL1现在将global_variable设置为400 在COMMON中,global_varialbe为400 整个系统在一个进程内执行。虽然 DLL0.DLL 和 DLL1.DLL 正在分别加载,依赖 COMMON。 DLL 只会在整个应用程序生命周期中一次加载。 EXE将不会加载相同的 COMMON.DLL 两次。 有任何方式,我可以实现以下不违反一个规则? DLL0和DLL1可以有自己的实例 global_variable ? COMMON.DLL 全局变量必须是全局变量, ? 将通过使用LIB文件的隐式链接加载COMMON.DLL 。 否重命名 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实例。解决方案 .dll是遗留代码,遗留代码由DLL0.dll和DLL1.dll使用。 不知道所有的细节,我会建议以下: p> 1)在DLL0.dll和DLL1.dll中创建一个全局变量。 2)使用COMMON.dll的global_variable值初始化步骤1中的每个全局变量。 3)根据需要使用两个新的global_variables。 Currently, I have a system with the following simplified view.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)The source code for COMMON.DLL is as follow.// 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();#endifThe source code for DLL0.DLL is as follow.__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();}The source for for DLL1.DLL is as follow.__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();}The source code for EXE is as follow.HINSTANCE instance0 = AfxLoadLibrary(_T("DLL0.dll"));FARPROC fun0 = GetProcAddress(instance0, "DLL0");HINSTANCE instance1 = AfxLoadLibrary(_T("DLL1.dll"));FARPROC fun1 = GetProcAddress(instance1, "DLL1");_fun0();_fun1();The output is as follow.This is DLL0In DLL0, global_variable is 100In COMMON, global_varialbe is 100DLL0 is now setting global_variable to 200In COMMON, global_varialbe is 200This is DLL1In 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 400In COMMON, global_varialbe is 400The entire system is executed within a single process. Although DLL0.DLL and DLL1.DLL are being loaded explicitly each, the dependency COMMON.DLL will only be loaded once in entire application life cycle. EXE will not load same COMMON.DLL twice.Is there any way, I can achieve the following without violating one of the rules?DLL0 and DLL1 can have their own instance of global_variable?global_variable must be global, and re-inside COMMON.DLL?COMMON.DLL will be loaded through implicit linking using LIB file.No renaming COMMON.DLL to COMMON-DLL0.DLL and COMMON-DLL1.DLL.No static linking.If DLL0 change the value of global_variable, calling common_fun_which_access_global_variable from DLL0 should access to DLL0's changed value. However, calling common_fun_which_access_global_variable from DLL1 shouldn't realize the changes.** I know this is too much. But I am now dealing with a legacy code. You know :)Will side-by-side assembly able to solve this kind of problem? What I understand is that, side-by-side assembly is used to solve multiple DLL with same name but different version issues. I am not sure whether it is applicable in my above case?Or, should I ask the other way around? How can we have 2 instance of COMMON.DLL being loaded within a same EXE? 解决方案 If I understand the situation correctly, COMMON.dll is the legacy code and the legacy code is used by both DLL0.dll and DLL1.dll.Without knowing all the details I would suggest the following:1) Create a global variable in DLL0.dll and DLL1.dll.2) Initialize each global variable from step 1 with COMMON.dll's global_variable value.3) Use the two new global_variables as needed. 这篇关于拥有来自同一个DLL的全局变量的多个实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-13 10:14