内存映射文件 - 映射结构而不是文件?
我对内存映射文件非常陌生,我对某些东西有点迷失。
我知道如果我有一个文件,我可以加载它并使用 MMap 立即从各个进程访问它。
但在我的情况下,我正在创建一个附加到进程 A 的 DLL,并且该 DLL 已被赋予一个指向进程 A 准备的 cSurface 的指针。我需要与进程B共享cSurface的数据。我真的不想调用一个空白的MMap并将我的进程A的表面复制到其中,只是在进程B中再次将其复制出来。
是否可以映射我的Surface 就好像它是一个文件一样,因此 MMap 在创建时已经指向表面数据(就像我加载 SomeTextFile.txt 一样)?
理论上,我的计划是在 Proc A 中接收指向表面的指针,告诉窗口以给定名称共享该表面的内存,并使用互斥体来协调访问 - 其想法是两个进程读取相同的物理副本表面没有繁琐的复制。
这可能吗?
I'm very new to Memory Mapped Files, and I'm a little lost on something.
I know that if I had a file, I could load it and access it from various processes at once using MMaps.
But in my situation, I'm creating a DLL attached to Process A, and that DLL has been given a pointer to a cSurface which Process A has prepared. I need to share that cSurface's data with Process B. I really don't want to have to call up a blank MMap and copy my Process A's surface into it, only to copy it out again in process B.
Is it possible to map my surface as if it were a file so the MMap already points to the surface data when it's created (as it would were I loading SomeTextFile.txt)?
My plan, in theory, would be to receive a pointer to the surface in Proc A, tell windows to share that surface's memory with a given name, and use Mutexes to coordinate access - the idea being that both processes read the same physical copy of the surface with no cumbersome copying.
Is that possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这一切都在 CreateFileMapping 的文档中。
您可以为内存映射文件命名。如果另一个进程打开同名的 mmap,它将指向相同的内存。如果 CreateFileMapping 中的句柄被放入 INVALID_HANDLE_VALUE 中,它将纯粹保留在内存中。查看CreateFileMapping
Yes, it's all there in the documentation of CreateFileMapping.
You can give your memory mapped file a name. If another process open a mmap with the same name it will point to the same memory. It the handle in CreateFileMapping is put to INVALID_HANDLE_VALUE it keeps it purely in memory. Check the documentation of CreateFileMapping
感谢您的所有评论。
我做了一些进一步的研究并找到了答案。
您可以使用内存映射文件来共享文件或空白内存空间。如果您想在设置地图之前共享内存中已初始化的数据,则必须创建一个空白地图,然后将数据复制到其中。
Thanks for all your comments.
I did some further research and found the answer.
You can use Memory Mapped Files to share either a file, or a blank memory space. If you want to share data already initialised in memory prior to setting up the map, you have to create a blank map and subsequently copy your data into it.