我们如何使用共享内存段与“Object”共享数据?两个托管进程之间?
如何使用共享内存段在两个托管进程之间共享数据?我在 C++/CLI 代码中使用 "object"
与其他进程中的内存的其他部分共享数据。我正在使用以下代码段。
#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")
但我需要它是:
#pragma data_seg (".SHAREDMEMORY")
bool _Locked = false;
bool _Initialized = false;
object^ _object = nullptr;
#pragma data_seg()
#pragma comment(linker,"/SECTION:.SHAREDMEMORY,RWS")
它说 "全局或静态变量可能没有托管类型 System::Int32^"
并给出其他错误,例如 "missing ; before '^'"
。
我必须将 .NET "Control"
对象的数据复制到此共享段,并且我需要将其传输到另一个进程。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最好描述一下你想做什么,而不是在碰壁时问如何继续,因为墙可能是死胡同。
Windows 窗体和 WPF 等 .Net 类实现了Windows 辅助功能和自动化 API,作为 Microsoft 努力遵守《美国残疾人法案》,美国保护残疾人的法律。
尽管 API 的设计主要是为了使为 Microsoft 平台编写的软件更容易被残疾人使用,但 API 公开软件的方式使得标准化 UI 自动化现在成为可能。为了使您的应用程序可测试,您需要做的事情现在简化为使您的应用程序可访问
这些 API 由 Microsoft 的 Microsoft UI 自动化框架调用,许多测试框架使用的托管代码框架。要了解有关 Windows 辅助功能 API 的更多信息或查找基于 Windows 辅助功能和自动化 API 的开源项目,请访问 辅助功能概述。
MSDN 杂志的测试中有一些有关测试应用程序的提示和调试列。
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.
您不能将 .NET 对象放入共享内存中。
指针仅在创建它们的进程中有效。因此,只有在没有指针的情况下(或使用基于寻址的概念,该概念在 32 位平面内存模型中几乎已失效),数据才能共享。
有时,只要库在所有进程中加载到其首选基地址,您就可以摆脱具有 v 表的 C++ 对象。但 .NET 函数具有动态地址,因为它们是在运行时编译的。不同进程之间的元数据指针不可能匹配。
另外,垃圾收集如何进行?垃圾收集需要查看所有引用才能知道对象是否可达,但您无法查看其他进程的非共享区域。内存会返回到哪个堆?
结论:您不能将 .NET 对象放入共享段、共享内存映射文件或使用按位序列化。相反,您需要将普通的旧数据放入共享区域,并使用原始本机指针(甚至不是 C++ 智能指针,请参阅上面有关内存管理的评论)。您可以将该指针包装在 C++/CLI 对象中以使其友好,但不能共享 .NET 对象本身。
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.
您需要某种形式的 IPC,例如 内存映射文件。
You need some form of IPC, for example a memory mapped file.