在Windows下使用共享内存。如何传递不同的数据 [英] Using shared memory under Windows. How to pass different data

查看:796
本文介绍了在Windows下使用共享内存。如何传递不同的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前尝试使用Windows CreateFileMapping 实施一些进程间通信机制。我知道我需要创建一个文件映射对象与CreateFileMapping首先,然后创建一个指针实际数据与MapViewOfFile。该示例然后通过使用CopyMemory将数据放入映射文件。

I currently try to implement some interprocess communication using the Windows CreateFileMapping mechanism. I know that I need to create a file mapping object with CreateFileMapping first and then create a pointer to the actual data with MapViewOfFile. The example then puts data into the mapfile by using CopyMemory.

在我的应用程序中,我有一个图像缓冲区(1 MB大),我想发送到另一个进程。所以现在我查询一个指向图像的指针,然后将整个图像缓冲区复制到mapfile中。但我不知道这是否真的有必要。不可能只是复制指向图像缓冲区数据的共享内存中的实际指针?我尝试了一下但没有成功。

In my application I have an image buffer (1 MB large) which I want to send to another process. So now I inquire a pointer to the image and then copy the whole image buffer into the mapfile. But I wonder if this is really necessary. Isn't it possible to just copy an actual pointer in the shared memory which points to the image buffer data? I tried a bit but didn't succeed.

推荐答案

不同的进程有不同的地址空间。如果你在一个进程中传递一个有效的指针到另一个进程,它可能指向第二个进程中的随机数据。因此,您必须复制所有数据。

Different processes have different address spaces. If you pass a valid pointer in one process to another process, it will probably point to random data in the second process. So you will have to copy all the data.

这篇关于在Windows下使用共享内存。如何传递不同的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆