Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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
#pragma once
#ifdef COMMON_EXPORTS
_declspec( dllexport ) int global_variable = 100;
#else
_declspec(dllimport) int global_variable;
#endif


The 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);
    global_variable = 200;
    printf ("DLL0 is now setting global_variable to 200\n");
    printf ("In DLL0, global_variable is %i\n", 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);
    global_variable = 400;
    printf ("DLL1 is now setting global_variable to 400\n");
    printf ("In DLL1, global_variable is %i\n", 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 DLL0
In DLL0, global_variable is 100
DLL0 is now setting global_variable to 200
In DLL0, global_variable is 200
This is DLL1
In DLL1, global_variable is 200   <-- I wish 100 is being printed.
                                  <-- I wish DLL0 and DLL1 have their own instance
                                  <-- of global_variable respectively.
DLL1 is now setting global_variable to 400
In DLL1, global_variable is 400


The 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?


1. DLL0 and DLL1 can have their own instance of global_variable?
2. global_variable must be global, and re-inside COMMON.DLL?
3. COMMON.DLL will be loaded through implicit linking using LIB file.
4. No renaming COMMON.DLL to COMMON-DLL0.DLL and COMMON-DLL1.DLL.
5. No static linking.


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?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900