DEV Community

binaries
binaries

Posted on

How to share data between different instance of same DLL

Usually Dlls are loaded into its corresponding process address space. Indeed the code is shared between all instances but not the global data. So the issue that we might face is – how can we share common data between all dll instances?

#pragma data_seg (".MYSEG")
    int g_nInteger = 0;
    char g_szString[] = "hello world";
#pragma data_seg()

// Tell the linker that the section
// .MYSEG is readable, writable and is shared.
#pragma comment(linker, "/SECTION:.MYSEG,RWS")
Enter fullscreen mode Exit fullscreen mode

Here we are declaring our data segment and are telling that, this segment is RWS – readable, writable and shared. This way we manage to share this data segment between all dll instances.

Top comments (2)

Collapse
 
binaries profile image
binaries

The simplest example is to use it for inter-process data communication. If it creates a dll with the above shared section and load this dll file in multiple processes, the dll will be loaded into the virtual memory space of each different process, but the shared section will allow all processes to access the data equally.

Collapse
 
pgradot profile image
Pierre Gradot • Edited

Can present a typical use-case where this is useful? :)