Create a DLL to share memory between two processes

c, dll, windows

Solution

By default, each process using a DLL has its own instance of all the DLLs global and static variables.

See What happens to global and static variables in a shared library when it is dynamically linked?

Also see https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/4636bfec-ff42-49ea-9023-ed7ff9b6a6fb/how-to-share-data-in-a-dllpragma-dataseg?forum=vclanguage.

Problem

I need to use DLL's to function similar to Linux Shared Memory. I have very little Windows programming experience, but I think it is possible to accomplish my goal. I want to so something similar to below: DLL ``` int x; void write(int temp) { x = temp } int read() { return x; } ``` Process 1: ``` LoadDLL(); write(5); //int x = 5 now ``` Process 2: ``` LoadDLL(); printf(read()); //prints 5 since int x = 5 from Proccess 1 ``` Naturally this example neglects race conditions and the like, but is there a simple way to go about something like this? I would be using Microsoft Visual Studio 10 to create the DLL. Could someone explain how I would write something this simple and build it into a DLL that can be loaded and called similar to the pseudo-code above? EDIT: Shared memory segments and Memory Mapped Files cannot be used because the processes I am creating are in LabVIEW and Lua which do not support the above. They do, however, support DLLs which is why I need this "outdated" approach.

Original source

Related problems