You can use data_seg pragma
For example, in an old DLL using global hooks, I have :
#pragma comment(linker, "/section:.Shared,rws")
#pragma data_seg (".Shared")
static HHOOK g_hHook = 0;
HWND g_hWndNotify = NULL;
#pragma data_seg()
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
.h file:
extern int myGlobal;
void work();
.cpp file:
int myGlobal = 42;
void work(){ myGlobal++; }
When the .cpp file is put in a static library and more than one shared library (DLL) or executable links against the static library, each one has its own copy of myGlobal. work() would modify its own version of the variable.
is there a way to get a process-wide unique variable or pointer to that variable?
You can use data_seg pragma
For example, in an old DLL using global hooks, I have :
#pragma comment(linker, "/section:.Shared,rws")
#pragma data_seg (".Shared")
static HHOOK g_hHook = 0;
HWND g_hWndNotify = NULL;
#pragma data_seg()
Another way to share memory between dlls - using-shared-memory-in-a-dynamic-link-library
Shared sections may be simpler to implement but they do have security implications - Why .shared sections are a security hole
Neither method of sharing memory between dlls will change the fact the each dll will contain its own copy of the variable from the linked-in static library. One possibility would be to wrap the static library in a dll. This wrapper dll would expose the methods and variables from the static library. Other dlls could then reference the wrapper dll without having to link with their own copy of the static library.