IPC through two different executables?

832 views Asked by At

I have the following problem, and I understand that I need to use either IPC via shared memory or network sockets.

I have one executable (meaning a separate .exe), compiled with VS2010, that gets the data from somewhere, and it should make that data available to the second executable.

boost::interprocess::managed_shared_memory managed_shm(
    boost::interprocess::open_or_create,
    "MyMemBlock",
    4000000);

The second executable is compiled with VS2012 and should receive that data (or fetch from memory) and process it.

// fails with a boost::interprocess::interprocess_exception
boost::interprocess::managed_shared_memory managed_shm(
    boost::interprocess::open_only,
    "MyMemBlock");

The whole thing needs to be as fast as possible. Compiling both executables with the same Visual Studio version is not an option, one codebase compiles only with VS2010, the other one only with VS2012/2013.

However, my first try with boost::interprocess didn't work (the second process throws a boost::interprocess::interprocess_exception), and I don't fully understand how exactly the memory gets shared, or more precise, how the shared memory information gets communicated from one process to the other. How does the first exe populate the information of the shared memory block? Does it only work for multiple processes inside one executable? Not multiple .exe's? Does it have to be the same boost DLL that is used by both executables? Is my only option IPC via Sockets?

2

There are 2 answers

0
Ela782 On BEST ANSWER

IPC works across two different executables. The two processes accessing the shared memory don't need to be compiled into the same executable. In fact, they can be compiled with different versions of visual studio and different boost DLL's. However, one has to use the same version of boost in both executables.

Quite interestingly, what is also not working, is running one executable in release-build and the other one in debug-build. I guess they allocate memory in a completely different way and can't share it.

5
Boris Ivanov On

You can try native Windows IPC. There many you can google them. I recommend Memory-mapped files. Here also nice article from MS

This is for example scenario of Non-persisted memorymap file.

1. Process A creates the memory-mapped file and writes a value to it.

2. Process B opens the memory-mapped file and writes a value to it.

3. Process C opens the memory-mapped file and writes a value to it.

4. Process A reads and displays the values from the memory-mapped file.

5. After Process A is finished with the memory-mapped file, the file is 
   immediately reclaimed by garbage collection.

Taken from here

Boost also have implementation of memory-mapped files, it will use native lowlevel API according to compiling target platform. Sample code can be taken here