1

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.

1
  • object^ is a pointer, not the object itself. Even if you made an object^ global, the object would still be on the private managed heap of just one process.
    – Ben Voigt
    May 23, 2011 at 0:16

3 Answers 3

5

It is best if you describe what you want to do instead of asking how to continue when you hit a wall, the wall may be a dead end.

.Net classes like Windows Forms and WPF implement Windows accessibility and automation APIs as Microsoft's efforts to comply with the Americans with Disabilities Act, a USA law to protect the disabled.

Although the APIs are designed mainly for making software written for Microsoft's platform more accessible to the disabled, the APIs expose the software in such a way that standardized UI automation is now possible. What you need to do for your app to be testable is now simplified to making your app accessible

The APIs are called by Microsoft's Microsoft UI Automation Framework, a framework used by many testing frameworks for managed code. To learn more about Windows's accessibility APIs or find open source projects based on Windows Accessibility and Automation APIs, visit Accessibility Overview .

There are some tips about testing apps in MSDN Magazine's testing and debug column.

4

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.

5
  • All you meant is , that I cannot create shared area where .NET native object(i.e .NET controls objects which inherited from Windows.Forms) even type casted in "object" class can reside. So I cannot make these control objects to be shared in shared memory segments?
    – Usman
    May 23, 2011 at 14:12
  • @Usman: That's correct. .NET objects rely on pointers (both metadata pointers stored in the object, and gc looking for pointers to the object). So the binary data can't be used from another process.
    – Ben Voigt
    May 23, 2011 at 14:28
  • So what would be then possible solution for this problem..? To caryy and extract several controls info form hooked process and to take it inside another process. Do I need to construct my own objects (i.e custom) and then by reading every property's data and copying data into my custom object.?
    – Usman
    May 23, 2011 at 14:46
  • @Usman: You need to serialize the data into a portable format. As yes, that will involve reading every property. Whether you call the result a custom object or a byte stream doesn't really matter. You might see if you can't use the existing .NET serializer classes, instead of writing your own.
    – Ben Voigt
    May 24, 2011 at 1:37
  • Yeah!1 correct , this is I decided and mostly i implemented..:-)
    – Usman
    May 24, 2011 at 10:06
1

You need some form of IPC, for example a memory mapped file.

5
  • I need to share the .NET Controls those are type casted in "object" class variable. They gets type casted well, but when they serialized with the help of BinaryFormattors using shared memory, CLR throws exception that "Sytem.Forms.Controls" are not marked as serializeable.
    – Usman
    May 22, 2011 at 21:57
  • What are you trying to achieve? May 22, 2011 at 22:18
  • I need to pass through .NET control objects from some hooked process to another process. This is why I need to create some shared memory segment and need to pass through these objects for other process. For that suppose I got some List<Control> from that hooked process and now I need to throw these objects list to some other managed process , If I do serialize with the help of binaryForammtors, CLR won't allow as Control object is not marked as serializeable nor I can do it. So Is there any possible way around to pass this List<Control> to other managed process. If some info required, inform
    – Usman
    May 22, 2011 at 22:25
  • why do you need to do this? What problem are you trying to solve? May 22, 2011 at 22:27
  • that would be long story..:-) But this is what needed to me. For building up automated framework for testing .NET apps.
    – Usman
    May 22, 2011 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.