How can I share the data between two managed processes using shared memory segments? I am using "object" inside C++/CLI code to share the data with some other part of memory in the other process. I am using following code segment.
#define BUFFER_SIZE 32768
#pragma data_seg (".SHAREDMEMORY")
bool _Locked = false;
bool _Initialized = false;
unsigned char[10000] data = NULL;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.SHAREDMEMORY,RWS")
but I need it to be:
#pragma data_seg (".SHAREDMEMORY")
bool _Locked = false;
bool _Initialized = false;
object^ _object = nullptr;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.SHAREDMEMORY,RWS")
It is saying that "global or static variable may not have managed type System::Int32^" and giving other errors like "missing ; before '^'".
I have to copy the .NET "Control" object’s data to this shared segment and I need it to transfer to another process.
You cannot put .NET objects in shared memory.
Pointers are only valid in the process they are created in. So data can only be shared if it has no pointers (or uses based addressing, a concept which is mostly dead in the 32-bit flat memory model).
Sometimes you can get away with C++ objects that have a v-table, as long as the library loads at its preferred base address in all processes. But .NET functions have dynamic addresses because they are compiled at runtime. There’s no hope that metadata pointers will match between different processes.
Also, how would garbage collection work? Garbage collection needs to see all references to know whether an object is reachable, but you wouldn’t be able to see into the non-shared area of other processes. And to which heap would the memory be returned?
Conclusion: You can’t put .NET objects in shared segments, shared memory mapped files, or use bitwise serialization. Instead you need to put plain old data in the shared area, and use raw native pointers (not even C++ smart pointers, see above comments about memory management). You can wrap that pointer in a C++/CLI object in order to make it friendly, but you can’t share the .NET object itself.