本文介绍了使用共享内存段与&QUOT我们如何共享数据;对象"两个托管进程之间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以共享使用共享内存段两个托管的进程之间的数据?我使用对象里面的C ++ / CLI code与内存中的其他过程中其他部分共享数据。我使用下面的code段。

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")

这是说,全局或静态变量可能没有管理型系统::的Int32 ^,并给予其他错误,如失踪;前^

It is saying that "global or static variable may not have managed type System::Int32^" and giving other errors like "missing ; before '^'".

我到.NET 控制对象的数据复制到该共享段,我需要它转移到另一个进程。

I have to copy the .NET "Control" object's data to this shared segment and I need it to transfer to another process.

推荐答案

您不能把.NET对象共享内存中。

You cannot put .NET objects in shared memory.

指针是仅在它们被创建的过程中有效。所以数据可以仅当它有没有指针(或采用基于寻址,一个概念,它主要是死在32位平面存储器模型)共享。

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).

有时候,你可以逃脱C ++对象有一个v表,只要库加载在其$ P $中的所有进程pferred基址。但是.NET函数具有动态地址,因为它们在运行时编译。有没有希望,元数据的指针将不同进程之间的匹配。

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?

结论:你不能把.NET对象的共享网段,共享内存映射文件,或使用按位序列。相反,你需要把普通的旧数据在共享区域,使用原始原生指针(甚至不是C ++智能指针,看到有关内存管理上面的注释)。你可以用这个指针在C ++ / CLI对象以使其友好的,但你不能共享.NET对象本身。

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.

这篇关于使用共享内存段与&QUOT我们如何共享数据;对象"两个托管进程之间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:30