3

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.

MrHappyAsthma
  • 6,332
  • 9
  • 48
  • 78
  • 1
    possible duplicate of [Sharing memory between two processes (C, Windows)](http://stackoverflow.com/questions/1200998/sharing-memory-between-two-processes-c-windows) – sashoalm Jul 17 '13 at 14:47
  • I believe this question is slightly different. I have the requirement of using the DLL where as the other question took the approach suggested here by Joe White to use Memory Mapped Files. Although Mem-Mapped files might also have worked, it was significantly more challenging (if not impossible) to suit my needs. – MrHappyAsthma Jul 17 '13 at 14:59
  • See https://blogs.msdn.microsoft.com/oldnewthing/20040804-00/?p=38253 for a description of some of the reasons this is such a bad idea. – Harry Johnston Dec 31 '16 at 22:39

4 Answers4

7

Althought I accepted the solution above, I wanted to also post my code in case anyone has a very similar issue this might save them some work. The solution provides some background knowledge on the approach that solved my problem, so here is an actual implementation of it.

This code was quickly made as a skeleton and is tested and works perfectly fine. You may need some synchronization depending on your final application, but it is definitely a good stepping stone:

dlltest.h

#ifndef _DLLTEST_H_
#define _DLLTEST_H_

#include <iostream>
#include <stdio.h>
#include <windows.h>

extern "C" __declspec(dllexport) int get();
extern "C" __declspec(dllexport) void set(int temp);


 #endif

dlltest.cpp

#include "dlltest.h"

#pragma data_seg("SHARED")
int x = 0;
#pragma data_seg()

extern "C" __declspec(dllexport)

int get()
{
    return x;
} 

extern "C" __declspec(dllexport)

void set(int temp)
{
    x = temp;
}

#pragma comment(linker, "/section:SHARED,RWS")  
MrHappyAsthma
  • 6,332
  • 9
  • 48
  • 78
  • Note that this won't work on Vista/W7 etc unless you are admin and have UAC access. – cup Jul 25 '19 at 12:42
4

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.

jsp
  • 1,225
  • 11
  • 21
4

If you want to share memory between processes, you don't need to use a DLL. (That was how you did it back in 16-bit Windows, but that was almost 20 years ago.)

Instead, you need to use memory-mapped files. You basically create an imaginary file in memory, and your processes can all see and modify the contents of that imaginary file.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • 1
    The reason I was interested in the DLL approach is because the two processes I will be writing are in LabVIEW and Lua which both have native DLL support. I will have to do some research into these "Memory Mapped Files". In the mean time, would you mind creating a "hello world" style set of C processes that use one such Mem Map File? – MrHappyAsthma Jul 17 '13 at 13:12
  • 2
    There's nothing stopping your DLL from creating a memory mapped file. That's the proper solution. – Harry Johnston Jul 18 '13 at 06:00
2

you can create dll that is loadable by both peers , and that dll creates a shared memory block , it have PutInMemory() and GetFromMemory() functions that each process loads it can call to comunicate with other process that are using the dll , see this https://msdn.microsoft.com/en-us/library/windows/desktop/ms686958(v=vs.85).aspx

cup
  • 7,589
  • 4
  • 19
  • 42
Dhia Hassen
  • 508
  • 4
  • 20